看看
PEP 328的以下信息:
Relative imports use a module’s __name__ attribute to determine that module’s position in the package hierarchy. If the module’s name does not contain any package information (e.g. it is set to '__main__') then relative imports are resolved as if the module were a top level module,regardless of where the module is actually located on the file system.
当您将foo.py作为脚本运行时,该模块的__name__是“__main__”,因此您不能进行相对导入.即使mypackage在sys.path上也是如此.基本上,如果导入了该模块,则只能从模块进行相对导入.
以下是解决这个问题的几个选择:
1)在foo.py中,检查是否__name__ ==’__main__’,并有条件地将mypackage添加到sys.path中:
if __name__ == '__main__':
import os,sys
# get an absolute path to the directory that contains mypackage
foo_dir = os.path.dirname(os.path.join(os.getcwd(),__file__))
sys.path.append(os.path.normpath(os.path.join(foo_dir,'..','..')))
from mypackage import bar
else:
from .. import bar
2)始终使用mypackage导入栏导入栏,并执行foo.py,使得mypackage可以自动显示:
$cd
$python -m mypackage.foo.foo