我有一个库(
subx)依赖于
subprocess32.subsprocess32库是Python2.7的后端,并提供超时kwarg.
我的图书馆需要超时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/
有一种声明性的方式,但是它需要一个或多或少的最新版本的setuptools(如果我正确读取
release notes,你至少需要20.2版本).您将在下面看到的内容称为环境标记,并在
PEP 508中指定,阅读它以获取可用标记的完整列表,并在需要时更好地理解标记语法.
对于python版本,让我们以您的包为例:您应该在python2.X环境中安装subprocess32依赖项.像这样增强你的依赖:
install_requires=[ 'subprocess32; python_version<"3"',]
现在使用python2.7安装软件包subx会产生:
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的安装.
另一个常见的例子是声明特定于平台的依赖项:我有一个项目需要在Linux上安装auditwheel并在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进行安装.