简体   繁体   English

将 package 扩展创建到其他 package [Python]

[英]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.我想创建一个 Python package 可以使用其他可安装 pip 的软件包进行扩展,它使用新功能扩展了第一个核心 package。

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然后,我开发了一个新的 package,名为

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我通过将这些代码行放入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并将子包feature-library/feature_library重命名为feature-library/library

So that we can access the feature functionalities from the library namespace.这样我们就可以从library命名空间访问feature功能。

For more info follow this link to the official documentation有关更多信息,请点击此链接到官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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