[英]python import module from parent package
我有以下目錄結構
foo/
__init__.py
settings.py
bar/
__init__.py
myfile.py
在myfile.py我有:導入設置
我收到以下錯誤: ImportError: No module named settings
,為什么? 如何從myfile.py
有效導入settings
文件
來自http://docs.python.org/2/tutorial/modules.html#intra-package-references :
from .. import settings
希望能幫助到你
這是另一種看起來更清晰的方法:
在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.