简体   繁体   English

Python名称空间包

[英]Python namespace packages

I'm following this instructions to create multiple small (independent) Python2 packages from a big one - https://packaging.python.org/guides/packaging-namespace-packages/#pkg-resources-style-namespace-packages 我下面这说明,从一个大的创建多个小(独立)Python2包- https://packaging.python.org/guides/packaging-namespace-packages/#pkg-resources-style-namespace-packages

Now I have multiple sub(packages) with such a structure: https://github.com/pypa/sample-namespace-packages/tree/master/pkg_resources 现在我有多个具有这样结构的子(包): https : //github.com/pypa/sample-namespace-packages/tree/master/pkg_resources

I can install and use them independently, it works just perfect. 我可以独立安装和使用它们,效果很好。 But since I have 12 packages under the same namespace, I would like to be able to install them by running one command (eg pip install ). 但是由于我在同一个命名空间下有12个软件包,所以我希望能够通过运行一个命令(例如pip install )来安装它们。 For example. 例如。 some future projects will need them all, so I prefer to serve them as (regular) monolith (one dependency), but sometimes I need only one of them (that's why I play with namespaces). 一些将来的项目将全部需要它们,因此我更喜欢将它们作为(常规)整体(一个依赖项)来使用,但有时我只需要其中一个(这就是为什么要使用名称空间)。 so I need some setup.py file in the root directory of my namespace that contains all subpackages in install_requires or what? 所以我需要在我的命名空间的根目录中包含一些setup.py文件,该文件包含install_requires中的所有子包,或者什么? I can't figure out how can I have an option to install all my small packages at once under the root namespace like in any regular project structure (when we don't split them and don't use namespaces at all), but having an option to install them separately? 我无法弄清楚如何像在任何常规项目结构中一样,将所有小软件包一次安装在根名称空间下(当我们不拆分它们并且根本不使用名称空间时),但是是否可以单独安装?

This sounds like a general problem of how to manage dependencies correctly, which is not related to your shared namespace feature. 这听起来像是一个如何正确管理依赖项的普遍问题,这与您的共享名称空间功能无关。 For your special problem, I would recommend the following: 对于您的特殊问题,我建议以下内容:

install_requires is only for the absolute necessary dependencies (those which needs to be present or the program cannot be executed), so it does not fit for your case. install_requires仅用于绝对必要的依赖项(需要存在的依赖项或无法执行程序的依赖项),因此它不适合您的情况。

It is better to use the extras_require directive , which allows you to specify additional dependencies in certain cases. 最好使用extras_require指令 ,该指令允许您在某些情况下指定其他依赖关系。 Common extras are dev – packages needed during development – or doc – packages required for building docs. 常见的额外功能包括dev –开发期间所需的软件包–或doc –构建文档所需的软件包。

Assuming your packages which are under the same namespace are called nsp1, nsp2, and nsp3, you could specify in any of your packages in your setup.py : 假设位于同一名称空间下的软件包分别称为nsp1,nsp2和nsp3,则可以在setup.py中的任何软件包中指定:

setup(
    name="Project-A",
    ...
    extras_require={
        'all':  ["nsp1", "nsp2", "nsp3"],
        'set1': ["nsp1", "nsp2"],
    }
)

Afterwards, you can install them with pip3 install nsp1[all] (assuming you have extended the setup.py for nsp1) or pip3 install .[all] for local install. 之后,您可以使用pip3 install nsp1[all] (假设您已经扩展了nsp1的setup.py )或pip3 install .[all]进行本地安装。

If any of your “some future projects” needs exactly those dependencies, you can add those extras to its dependencies, eg in its setup.py : 如果您的“将来的某些项目”中的任何一个都需要这些依赖项,则可以将这些额外功能添加到其依赖项中,例如在setup.py

install_requires=['nsp1[all]', …]

An alternative would be to use a requirement.txt : 一种替代方法是使用requirement.txt

nsp1
nsp2
nsp3

And install them with pip install -rrequirement.txt 并使用pip install -rrequirement.txt安装它们

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

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