依赖管理:Python2.7 需要 subprocess32

Dependency management: subprocess32 needed for Python2.7

我有一个库(subx) which depends on subprocess32。subprocess32 库是 Python2.7 的后向端口,并提供超时参数。

我的库需要超时 kwarg。

仅当目标平台为 Python2.x 时,我才需要 subprocess32。

我应该如何在我的项目中定义依赖关系?

如果我通过 "install_requires" (setup.py) 定义对 subprocess32 的依赖并且我在 python3 virtualenv 中,我会收到此错误消息:

===> pip install -e git+https://github.com/guettli/subx.git#egg=subx
Obtaining subx from git+https://github.com/guettli/subx.git#egg=subx
  Cloning https://github.com/guettli/subx.git to ./src/subx
Collecting subprocess32 (from subx)
  Using cached subprocess32-3.2.7.tar.gz
    Complete output from command python setup.py egg_info:
    This backport is for Python 2.x only.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-lju3nl1y/subprocess32/
import sys

kw = {}
if sys.version_info[0] == 2:
    kw['install_requires'] = ['subprocess32']

setup(
    …
    **kw
)

有一种声明方式,但 afaik 它需要或多或少最新版本的 setuptools(如果我阅读 release notes correctly, you need at least the 20.2 version). What you will see down below are called environment markers and are specified in PEP 508,通读它以获得可用标记的完整列表和如果您愿意,可以更好地理解标记语法。

对于python版本,我们以你的包为例:你有subprocess32依赖,应该安装在python2.X环境中。像这样增强你的依赖性:

install_requires=[
    'subprocess32; python_version<"3"',
]

安装包 subxpython2.7 现在产生:

Processing ./dist/subx-2017.8.0-py2-none-any.whl
Collecting subprocess32; python_version < "3" (from subx==2017.8.0)
Installing collected packages: subprocess32, subx
Successfully installed subprocess32-3.2.7 subx-2017.8.0

如果你用python3.X安装它,输出将是:

Processing ./dist/subx-2017.8.0-py3-none-any.whl
Installing collected packages: subx
Successfully installed subx-2017.8.0

请注意 subprocess32 的安装将被跳过。


另一个常见的例子是声明特定于平台的依赖项:我有一个项目需要 auditwheel 安装在 Linux 和 delocate MacOS 上。我这样声明依赖关系:

install_requires=[
    ...
    'auditwheel==1.7.0; "linux" in sys_platform',
    'delocate==0.7.1; "darwin" == sys_platform',
]

请注意,如果您没有专门针对任何主要 python 版本,则需要检查 Linux,因为:

$ python2 -c "import sys; print sys.platform"
linux2

但是

$ python3 -c "import sys; print sys.platform"
linux

例如,如果您的包仅适用于 python2.X,您可以使用检查 "linux2" == sys.platform。这将使您的依赖项只能通过 python2.X.

安装