当前位置: 动力学知识库 > 问答 > 编程问答 >

python import module from parent package

问题描述:

I have the following directory structure

foo/

__init__.py

settings.py

bar/

__init__.py

myfile.py

In myfile.py I have:

import settings

I get the following error: ImportError: No module named settings, why? How can I efectively import the settings file from myfile.py

网友答案:

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

from .. import settings

Hope it helps

网友答案:

I searched also to access to parent package for other purpose, and I found this other way that seems more clear :

in foo.init.py :

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

in myfile.py :

# instead of "from .. import..." 
  from foo import settings 
  print settings.theThing
网友答案:

I searched also to access to parent package for other purpose, and I found this other way :

in foo.init.py :

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

in myfile.py : # instead of "from .. import..." from foo import settings print settings.theThing

分享给朋友:
您可能感兴趣的文章:
随机阅读: