这种结构仅仅是一个例子多个相对进口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'。 –