简体   繁体   English

python命名空间包的当前状态

[英]Current state of python namespace packages

I would like to have several python sub modules inside a main module, but I want to distribute them as separated python packages. 我想在主模块中有几个python子模块,但我想将它们作为单独的python包分发。 So package A should provide 'my_data.source_a', package B should provide 'my_data.source_b', ... and so on. 所以包A应该提供'my_data.source_a',包B应该提供'my_data.source_b',......等等。

I found out that I have to use a namespace package for this, but trying to figuring out the details, I found multiple PEPs covering that problem. 我发现我必须使用命名空间包,但是试图找出细节,我找到了多个PEP来解决这个问题。 PEP 420 seems to be the latest one, which builds upon PEP 402 and PEP 382. PEP 420似乎是最新的,它建立在PEP 402和PEP 382之上。

To me it's not clear what the status of the different PEPs an implementations is. 对我来说,不清楚实现的不同PEP的状态是什么。 So my question is: Is http://pythonhosted.org/distribute/setuptools.html#namespace-packages still the way to go or how should I build my namespace package? 所以我的问题是: http//pythonhosted.org/distribute/setuptools.html#namespace-packages仍然是要走的路,或者我应该如何构建我的命名空间包?

The Python documentation has a good description of the three ways of creating namespace packages in Python, including guidelines for when to use each of the three methods. Python文档很好地描述了在Python中创建命名空间包的三种方法,包括何时使用这三种方法中的每一种的指南。 Furthermore, this topic is discussed in great depth in a different StackOverflow thread which has a good accepted answer . 此外,本主题在不同的StackOverflow 线程中进行了深入讨论,该线程具有良好的接受答案 Finally, if you are someone who would rather read code than documentation, the sample-namespace-packages repo contains examples of namespace packages created using each of the three available methods. 最后,如果您是一个宁愿阅读代码而非文档的人,那么sample-namespace-packages repo包含使用三种可用方法中的每一种创建的命名空间包的示例。

In brief, if you intend your packages to work with Python versions 3.3 and above, you should use the native namespace packages method. 简而言之,如果您打算使用Python 3.3及更高版本的软件包,则应使用本机命名空间软件包方法。 If you intend your packages to work with older versions of Python, you should use the pkgutil method . 如果您打算使用旧版本的Python,则应使用pkgutil方法 If you intend to add a namespace package to a namespace that is already using the pkg_resources method , you should continue to use method. 如果要将命名空间包添加到已使用pkg_resources方法的命名空间,则应继续使用方法。


With native namespace packages , we can remove __init__.py from both packages and modify our setup.py files to look as follows: 使用本机命名空间包 ,我们可以从两个包中删除__init__.py并修改我们的setup.py文件,如下所示:

# setup.py file for my_data.source_a
from setuptools import setup, find_namespace_packages

setup(
    name="my_data.source_a",
    version="0.1",
    packages=find_namespace_packages(include=['my_data.*'])
)
# setup.py file for my_data.source_b
from setuptools import setup, find_namespace_packages

setup(
    name="my_data.source_b",
    version="0.1",
    packages=find_namespace_packages(include=['my_data.*'])
)

We need to add the include=['my_data.*'] argument because, by default find_namespace_packages() is rather lenient in the folders that it includes as namespace packages, as described here . 我们需要添加include=['my_data.*']的说法,因为在默认情况下find_namespace_packages()是,它包括作为命名的包,如所描述的文件夹,而宽松这里

This is the recommended approach for packages supporting Python 3.3 and above. 这是支持Python 3.3及更高版本的软件包的推荐方法。


With pkgutil -style namespace packages , we need to add the following line to the my_data.__init__.py files in each of our packages : 使用pkgutil style命名空间包 ,我们需要pkgutil添加到每个包中my_data.__init__.py文件中:

__path__ = __import__('pkgutil').extend_path(__path__, __name__)

This is the approach used by the backports namespace, and by different packages in the google-cloud-python repo, and it is the recommended approach for supporting older versions of Python. 这是backports命名空间使用的方法,以及google-cloud-python repo中的不同包,它是支持旧版Python的推荐方法。

The latest version of Python which is Python 3.7 uses native namespace packages approach to create namespace packages which is defined in PEP 420 . Python 3.7的最新版本使用本机命名空间包方法来创建在PEP 420中定义的命名空间包。

There are currently three different approaches to creating namespace packages: 目前有三种不同的方法来创建命名空间包:

  1. Use native namespace packages . 使用本机命名空间包 This type of namespace package is defined in PEP 420 and is available in Python 3.3 and later. 这种类型的命名空间包在PEP 420中定义,可在Python 3.3及更高版本中使用。 This is recommended if packages in your namespace only ever need to support Python 3 and installation via pip . 如果命名空间中的包只需要支持Python 3并通过pip安装,则建议使用此方法。
  2. Use pkgutil-style namespace packages . 使用pkgutil样式的命名空间包 This is recommended for new packages that need to support Python 2 and 3 and installation via both pip and python setup.py install . 对于需要支持Python 2和3以及通过pippython setup.py install新软件包,建议使用此方法。
  3. Use pkg_resources-style namespace packages . 使用pkg_resources样式的命名空间包 This method is recommended if you need compatibility with packages already using this method or if your package needs to be zip-safe. 如果您需要与已使用此方法的软件包兼容,或者您​​的软件包需要是zip安全的,则建议使用此方法。

Reference: Packaging namespace packages 参考: 包装命名空间包

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

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