[英]Pip install sub-package from own (namespace) package in python
I'd like to install some special sub-package from a package.我想从 package 安装一些特殊的子包。
For example, I want to create package with pkg_a and pkg_b.例如,我想用 pkg_a 和 pkg_b 创建 package。 But I want to allow the user to choose which he wants to install.
但我想让用户选择他想要安装的。
What I'd like to do:我想做的事:
git clone https://github.com/pypa/sample-namespace-packages.git
cd sample-namespace-packages
touch setup.py
setup-py:设置-py:
import setuptools
setup(
name='native',
version='1',
packages=setuptools.find_packages()
)
# for all packages
pip install -e native #Successfully installed native
# for specific
# Throws ERROR: native.pkg_a is not a valid editable requirement.
# It should either be a path to a local project
pip install -e native.pkg_a native.pkg_b
# for specific
cd native
pip install -e pkg_a # Successfully installed example-pkg-a
I've seen this in another questions answer so it must be possible: Python install sub-package from package我在另一个问题的答案中看到了这一点,所以它一定是可能的: Python install sub-package from package
Also I've read the Packaging namespace packages documentation and tried to do the trick with the repo我还阅读了Packaging namespace packages 文档并尝试使用repo来解决问题
I've tried some variants with an additional setup.py in the native directory, but I can't handle it and I am thankful for all help.我在本机目录中尝试了一些带有额外 setup.py 的变体,但我无法处理它,我感谢所有帮助。
In addition to the answer from sinoroc I've made an own repo.除了来自 sinoroc 的答案之外,我还做了一个自己的回购。 I created a package Nmspc, with subpackages NmspcPing and NmspcPong.
我创建了一个 package Nmspc,带有子包 NmspcPing 和 NmspcPong。 But I want to allow the user to choose which he wants to install.
但我想让用户选择他想要安装的。 Also it must to be possible to install the whole package.
还必须可以安装整个 package。
What I'd like to do is something like that:我想做的是这样的:
git clone https://github.com/cj-prog/Nmspc.git
cd Nmspc
# for all packages
pip install Nmspc
# Test import
python3 -c "import nmspc; import nmspc.pong"
# for specific
pip install -e Nmspc.pong # or
pip install -e pong
# Test import
python3 -c "import pong;"
A solution for your use case seems to be similar to the one I gave here: https://stackoverflow.com/a/58024830/11138259 , as well as the one you linked in your question: Python install sub-package from package . A solution for your use case seems to be similar to the one I gave here: https://stackoverflow.com/a/58024830/11138259 , as well as the one you linked in your question: Python install sub-package from package .
Here is an example...这是一个例子......
The directory tree might look like this:目录树可能如下所示:
.
├── Nmspc
│ ├── nmspc
│ │ └── _nmspc
│ │ └── __init__.py
│ └── setup.py
├── NmspcPing
│ ├── nmspc
│ │ └── ping
│ │ └── __init__.py
│ └── setup.py
└── NmspcPong
├── nmspc
│ └── pong
│ └── __init__.py
└── setup.py
3 Python projects: 3 Python 项目:
nmspc.ping
nmspc.ping
nmspc.pong
nmspc.pong
nmspc._nmspc
see below for details) nmspc._nmspc
详见下文) They are all namespace packages .它们都是命名空间包。 They are built using the instructions from the Python Packaging User Guide on "Packaging namespace packages, Native namespace packages" .
它们是使用Python 打包用户指南“打包命名空间包,本地命名空间包”中的说明构建的。 There is another example here .
这里还有另一个例子。
The project Nmspc is basically empty, no actual code, but the important part is to add the other two NmspcPing and NmspcPong as installation requirements .项目Nmspc基本是空的,没有实际代码,但重要的是添加另外两个NmspcPing和NmspcPong作为安装要求。 Another thing to note, is that for convenience it is also a namespace package with
nmspc._nmspc
being kind of hidden (the leading underscore is the naming convention for hidden things in Python).另一件需要注意的是,为方便起见,它也是一个命名空间 package ,其中
nmspc._nmspc
有点隐藏(前导下划线是 Python 中隐藏事物的命名约定)。
NmspcPing/setup.py
(and similarly NmspcPong/setup.py
): NmspcPing/setup.py
(和类似NmspcPong/setup.py
):
#!/usr/bin/env python3
import setuptools
setuptools.setup(
name='NmspcPing',
version='1.2.3',
packages=['nmspc.ping',],
)
Nmspc/setup.py
: Nmspc/setup.py
:
#!/usr/bin/env python3
import setuptools
setuptools.setup(
name='Nmspc',
version='1.2.3',
packages=['nmspc._nmspc',],
install_requires=['NmspcPing', 'NmspcPong',],
)
Assuming you are in the root directory, you can install these for development like this:假设您在根目录中,您可以像这样安装这些用于开发:
$ python3 -m pip install -e NmspcPing
$ python3 -m pip install -e NmspcPong
$ python3 -m pip install -e Nmspc
And then you should be able to use them like this:然后你应该能够像这样使用它们:
$ python3 -c "import nmspc.ping; import nmspc.pong; import nmspc._nmspc;"
Update更新
This can be simplified:这可以简化:
.
├── NmspcPing
│ ├── nmspc
│ │ └── ping
│ │ └── __init__.py
│ └── setup.py
├── NmspcPong
│ ├── nmspc
│ │ └── pong
│ │ └── __init__.py
│ └── setup.py
└── setup.py
setup.py
#!/usr/bin/env python3
import setuptools
setuptools.setup(
name='Nmspc',
version='1.2.3',
install_requires=['NmspcPing', 'NmspcPong',],
)
Use it like this:像这样使用它:
$ python3 -m pip install ./NmspcPing ./NmspcPong/ .
$ python3 -c "import nmspc.ping; import nmspc.pong;"
If the projects are not installed from an index such as PyPI , it is not possible to take advantage of the install_requires
feature.如果项目不是从PyPI等索引安装的,则无法利用
install_requires
功能。 Something like this could be done instead:可以这样做:
.
├── NmspcPing
│ ├── nmspc.ping
│ │ └── __init__.py
│ └── setup.py
├── NmspcPong
│ ├── nmspc.pong
│ │ └── __init__.py
│ └── setup.py
└── setup.py
NmspcPing/setup.py
(and similarly NmspcPong/setup.py
) NmspcPing/setup.py
(和类似NmspcPong/setup.py
)
import setuptools
setuptools.setup(
name='NmspcPing',
version='1.2.3',
package_dir={'nmspc.ping': 'nmspc.ping'},
packages=['nmspc.ping'],
)
setup.py
import setuptools
setuptools.setup(
name='Nmspc',
version='1.2.3',
package_dir={
'nmspc.ping': 'NmspcPing/nmspc.ping',
'nmspc.pong': 'NmspcPong/nmspc.pong',
},
packages=['nmspc.ping', 'nmspc.pong'],
)
This allows to install from the root folder in any of the following combinations:这允许以以下任何组合从根文件夹安装:
$ python3 -m pip install .
$ python3 -m pip install ./NmspcPing
$ python3 -m pip install ./NmspcPong
$ python3 -m pip install ./NmspcPing ./NmspcPong
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.