python import module from parent package

后端 未结 2 616
有刺的猬
有刺的猬 2020-12-06 17:10

I have the following directory structure

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

In myfile.py I

相关标签:
2条回答
  • 2020-12-06 17:28

    Here is another method 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
    
    0 讨论(0)
  • 2020-12-06 17:44

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

    from .. import settings
    

    Hope it helps

    0 讨论(0)
提交回复
热议问题