简体   繁体   English

打包和构建 python 命名空间包

[英]Packaging and building python namespace packages

What I am trying to do?我想做什么?
Have one large package with several installable parts, which need to work together, but specific parts can be used on their own.拥有一台大型 package 和几个可安装部件,需要一起工作,但特定部件可以单独使用。 I decided to use namespace package, as it seems to do what I want.我决定使用命名空间 package,因为它似乎可以满足我的要求。

Problem问题
How do I actually build a namespace package?我如何实际构建命名空间 package?

My example我的例子
I have a following namespace package, with each of the subpackages containing some modules not listed here.我有一个以下命名空间 package,每个子包都包含一些此处未列出的模块。 I am using setuptools .我正在使用setuptools

.
|-mypkg
  |- core
  |    |- setup.py
  |    |- mynmspc
  |         |- util
  |- compute
  |    |- setup.py
  |    |- mynmspc
  |         |- funcs
  |- plotting
       |- setup.py
       |- mynmspc
            |- graphs
- setup.py

Now I can build a wheel of this package python setup.py bdist_wheel .现在我可以构建这个 package python setup.py bdist_wheel的轮子。 However if I want to have eg core subpackage installable by itself do I have to create another wheel for it?但是,如果我想自己安装例如core子包,我是否必须为它创建另一个轮子? What would be the best practice if I would like to give users option to either install the whole package or any of the listed subpackages?如果我想让用户选择安装整个 package 或任何列出的子包,那么最佳实践是什么? Is there a way to automatically build all nested wheels of the main package?有没有办法自动构建主package的所有嵌套轮子?

If you want to use latest python native namespace packages to allow users to install extensions-like packages ( mypkg-core , mypkg-compute ,...), you should create different folders for each package like so如果你想使用最新的 python 本地命名空间包来允许用户安装类似扩展的包( mypkg-coremypkg-compute ,...),你应该像这样为每个 package 创建不同的文件夹

mypkg-core/
    mypkg/ # no __init__.py here
        core/
            __init__.py
        setup.py

mypkg-compute/
    mypkg/ # no __init__.py here
        compute/
            __init__.py
        setup.py

where each setup.py is like the following每个setup.py如下所示

from setuptools import setup, find_namespace_packages

setup(
    name='mypkg-core',
    ...
    packages=find_namespace_packages(include=['mypkg.*'])
)

And you will need to build a whl for each package.您需要为每个 package 构建一个 whl。

With this configuration, users will be able to通过此配置,用户将能够

pip install mypkg-core
pip install mypkg-compute

And access them via the same mypkg namespace:并通过相同的mypkg命名空间访问它们:

import mypkg.core
import mypkg.compute

Don't forget to use find_namespace_package from setuptools in setup.py for each subpkg.不要忘记在setup.py中为每个 subpkg 使用find_namespace_package中的setuptools

Please always refer to the official documentation about native namespace packages.请始终参考有关本地命名空间包的官方文档。 . .

However, I would suggest an easier way to distribute your package by adding the subpackages dependencies as extras grouped with the name of the subpackage.但是,我会建议一种更简单的方法来分发您的 package,方法是将子包依赖项添加为与子包名称分组的extras项。 This way the users can use the subpackage only if they install the whole package with这样,用户只有在安装整个 package 时才能使用子包

pip install mypkg[compute]

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

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