How to import classes defined in __init__.py

前端 未结 7 613
你的背包
你的背包 2020-11-29 17:23

I am trying to organize some modules for my own use. I have something like this:

lib/
  __init__.py
  settings.py
  foo/
    __init__.py
    someobject.py
           


        
7条回答
  •  孤街浪徒
    2020-11-29 17:36

    If lib/__init__.py defines the Helper class then in settings.py you can use:

    from . import Helper
    

    This works because . is the current directory, and acts as a synonym for the lib package from the point of view of the settings module. Note that it is not necessary to export Helper via __all__.

    (Confirmed with python 2.7.10, running on Windows.)

提交回复
热议问题