Python: import the containing package

后端 未结 5 1428
夕颜
夕颜 2020-12-13 12:00

In a module residing inside a package, i have the need to use a function defined within the __init__.py of that package. how can i import the package within the

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 12:52

    Also, starting in Python 2.5, relative imports are possible. e.g.:

    from . import foo
    

    Quoting from http://docs.python.org/tutorial/modules.html#intra-package-references:


    Starting with Python 2.5, in addition to the implicit relative imports described above, you can write explicit relative imports with the from module import name form of import statement. These explicit relative imports use leading dots to indicate the current and parent packages involved in the relative import. From the surrounding module for example, you might use:

    from . import echo
    from .. import formats
    from ..filters import equalizer
    

提交回复
热议问题