簡體   English   中英

來自父包的python導入模塊

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM