這種結構僅僅是一個例子多個相對進口3.5
pkg\
test\
__init__.py
test.py
__init__.py
source.py
another_source.py
another_source.py
class Bar():
def __init__(self):
self.name = "bar"
source.py
from another_source import Bar
class Foo():
def __init__(self):
self.name = "foo"
b = Bar()
test.py
from ..source import Foo
if __name__== "__main__":
f = Foo()
print(f.name)
現在我想運行test.py. 因爲它已被接受爲answer我得走了我目前的包裝上面並運行
python -m pkg.test.test
但是,這並不工作,蟒蛇給了我一個回溯
Traceback (most recent call last):
File "-\Python35\lib\runpy.py", line 170, in _run_module_as_main
"__main__", mod_spec)
File "-\Python35\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "~\test\test.py", line 1, in <module>
from ..source import Foo
File "~\source.py", line 1, in <module>
from another_source import Bar
ImportError: No module named 'another_source'
如果我刪除所有another_source - 它會工作,但這不是一個解決方案。
現在是否有一種理智的方式從上方的目錄中導入類?
'如果我沒有記錯,從pkg.source import Foo'。 –