来自父包的python导入模块

时间:2013-01-10 02:45:39

标签: python import module package parent

我有以下目录结构

foo/
    __init__.py
    settings.py
    bar/
        __init__.py
        myfile.py

在myfile.py中我有:     导入设置

我收到以下错误:ImportError: No module named settings,为什么?如何从settings

有效地导入myfile.py文件

2 个答案:

答案 0 :(得分:17)

来自http://docs.python.org/2/tutorial/modules.html#intra-package-references

from .. import settings

希望有所帮助

答案 1 :(得分:2)

这是另一种看起来更清晰的方法:

foo.__init__.py

  __all__ = ['settings', ..(all other modules at 'foo' level you want to show)...]

myfile.py

# instead of "from .. import..." 
  from foo import settings 
  print settings.theThing
相关问题