简体   繁体   中英

Creating package extension to other package [Python]

Dears,

I would like to create a Python package that can be extended with other pip-installable packages, that extends the first core package with new features.

The issue I have it to import these "extensions" subpackage in the core library namespace/path. Let me explain it better with an example:

The core library is setup like this:

library/
    setup.py
    library/
        __init.py__
        core/
            core_module1.py
            core_module2.py

Then, I have developed a new package called

library-feature/
    setup.py
    library_feature/
        __init__.py
        feature/
            __init__.py
            feature_module1.py
            feature_module2.py

How can I use the feature modules with importing just the core library?

import library

from library.core.core_module1 import core_function
from library.feature.feature_module1 import feature_function

I solved this by putting these lines of code into library/__init__.py

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

And renaming the subpackage feature-library/feature_library into feature-library/library

So that we can access the feature functionalities from the library namespace.

For more info follow this link to the official documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM