本文介绍了从python中自己的(命名空间)包安装子包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个包中安装一些特殊的子包.

例如,我想用 pkg_a 和 pkg_b 创建包.但我想让用户选择他想要安装的.

我想做什么:

git clone https://github.com/pypa/sample-namespace-packages.gitcd sample-namespace-packages触摸设置.py

设置-py:

导入设置工具设置(名称='本地',版本='1',包=setuptools.find_packages())
# 适用于所有包pip install -e native #本地安装成功# 具体的# 抛出错误:native.pkg_a 不是有效的可编辑要求.# 它应该是本地项目的路径pip install -e native.pkg_a native.pkg_b# 具体的光盘本机pip install -e pkg_a # 安装成功example-pkg-a

我在另一个问题的答案中看到了这个,所以它一定是可能的:Python 安装包中的子包

我还阅读了打包命名空间包文档并尝试使用 repo

来解决这个问题

我已经在本地目录中尝试了一些带有附加 setup.py 的变体,但我无法处理它,非常感谢所有帮助.

更新

除了来自 sinoroc 的回答之外,我还制作了一个自己的 repo.我创建了一个包 Nmspc,带有子包 NmspcPing 和 NmspcPong.但我想让用户选择他想要安装的.还必须可以安装整个包.

我想做的是这样的:

git clone https://github.com/cj-prog/Nmspc.git光碟机# 对于所有包pip 安装 Nmspc# 测试导入python3 -c "导入 nmspc;导入 nmspc.pong"
# 用于特定pip install -e Nmspc.pong # 或pip install -e pong# 测试导入python3 -c "导入乒乓球;"
解决方案

针对您的用例的解决方案似乎与我在此处提供的解决方案类似:https://stackoverflow.com/a/58024830/11138259,以及您在问题中链接的那个:Python从包安装子包.

这是一个例子...

目录树可能如下所示:

.├── Nmspc│ ├── nmspc│ │ └── _nmspc│ │ └── __init__.py│ └── setup.py├── NmspcPing│ ├── nmspc│ │ └── ping│ │ └── __init__.py│ └── setup.py└── NmspcPong├── nmspc│ └── 乒乓│ └── __init__.py└── setup.py

3 个 Python 项目:

  • NmspcPing 提供 nmspc.ping
  • NmspcPong 提供 nmspc.pong
  • Nmspc 依赖于其他两个项目(并且还提供了 nmspc._nmspc 详情见下文)

它们都是命名空间包.它们是使用 Python 打包用户指南中的说明构建的打包命名空间包,本机命名空间包".还有另一个例子这里.

项目Nmspc基本是空的,没有实际代码,但重要的部分是将另外两个NmspcPingNmspcPong添加为安装要求.另一件要注意的事情是,为了方便起见,它也是一个 namespace 包,其中 nmspc._nmspc 是隐藏的(前导下划线是 Python 中隐藏事物的命名约定).

NmspcPing/setup.py(和类似的 NmspcPong/setup.py):

#!/usr/bin/env python3导入设置工具设置工具.setup(name='NmspcPing',版本='1.2.3',包=['nmspc.ping',],)

Nmspc/setup.py:

#!/usr/bin/env python3导入设置工具设置工具.setup(名称='Nmspc',版本='1.2.3',包=['nmspc._nmspc',],install_requires=['NmspcPing', 'NmspcPong',],)

假设你在根目录下,你可以像这样安装这些用于开发:

$ python3 -m pip install -e NmspcPing$ python3 -m pip install -e NmspcPong$ python3 -m pip install -e Nmspc

然后你应该可以像这样使用它们:

$ python3 -c "import nmspc.ping; import nmspc.pong; import nmspc._nmspc;"

更新

这可以简化:

.├── NmspcPing│ ├── nmspc│ │ └── ping│ │ └── __init__.py│ └── setup.py├── NmspcPong│ ├── nmspc│ │ └── 乒乓│ │ └── __init__.py│ └── setup.py└── setup.py

setup.py

#!/usr/bin/env python3导入设置工具设置工具.setup(名称='Nmspc',版本='1.2.3',install_requires=['NmspcPing', 'NmspcPong',],)

像这样使用它:

$ python3 -m pip install ./NmspcPing ./NmspcPong/.$ python3 -c导入nmspc.ping;导入nmspc.pong;"

I'd like to install some special sub-package from a package.

For example, I want to create package with pkg_a and pkg_b. 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:

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

Also I've read the Packaging namespace packages documentation and tried to do the trick with the 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.

Update

In addition to the answer from sinoroc I've made an own repo. I created a package Nmspc, with subpackages NmspcPing and 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.

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.

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:

  • NmspcPing provides nmspc.ping
  • NmspcPong provides nmspc.pong
  • Nmspc depends on the other two projects (and also provides nmspc._nmspc see below for details)

They are all namespace packages. They are built using the instructions from the Python Packaging User Guide on "Packaging namespace packages, Native namespace packages". 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. 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).

NmspcPing/setup.py (and similarly NmspcPong/setup.py):

#!/usr/bin/env python3

import setuptools

setuptools.setup(
    name='NmspcPing',
    version='1.2.3',
    packages=['nmspc.ping',],
)

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;"

这篇关于从python中自己的(命名空间)包安装子包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:39