Importing Deeply Nested Modules In Python
Consider the following case in Python 3.6: basepackage |---__init__.py |---package |---__init__.py |---subpackage |---__init__.p
Solution 1:
For the first option (import basepackage.package.subpackage.module as aliased_module) to work, these conditions have to be met:
basepackage/__init__.pyhas to contain a line similar tofrom . import package(the namepackagehas to be defined inside thisbasepackage/__init__.pyfile)basepackage/package/__init__.pyhas to contain a line similar tofrom . import subpackagebasepackage/package/subpackage/__init__.pyhas to contain a line similar tofrom . import module
Note: the import statements inside the __init__.py files can also be absolute instead of relative paths.
For the second option (from basepackage.package.subpackage import module as aliased_module), it is enough if there are empty __init__.py files at each level, as long as these __init__.py files exist.
Post a Comment for "Importing Deeply Nested Modules In Python"