如何从内部目录导入python包?

时间:2019-06-24 09:34:19

标签: python python-2.7 pythonpath

我其中有2个软件包和主脚本:

$ ls package1
hello.py  __init__.py

$ cat package1/hello.py
def runme():
    print "Hello world"

$ ls package2
__init__.py  someprogram.py

$ cat package2/someprogram.py
from package1.hello import runme

if __name__ == '__main__':
    runme()

$ python package2/someprogram.py 
Traceback (most recent call last):
  File "package2/someprogram.py", line 1, in <module>
    from package1.hello import runme
ImportError: No module named package1.hello

这是为什么?不应将当前目录添加到PYTHONPATH并因此使package1可以访问吗?

0 个答案:

没有答案