简体   繁体   English

依赖管理:Python2.7需要subprocess32

[英]Dependency management: subprocess32 needed for Python2.7

I have a library ( subx ) which depends on subprocess32 . 我有一个库( SUBX ),这取决于subprocess32 The subprocess32 library is a backport for Python2.7 and provides the timeout kwarg. subprocess32库是Python2.7的后端,提供超时kwarg。

My library needs the timeout kwarg. 我的图书馆需要超时kwarg。

I need subprocess32 only if the target platform is Python2.x. 只有目标平台是Python2.x时我才需要subprocess32。

How should I define the dependency in my project? 我该如何在项目中定义依赖项?

I get this error message, if I define a dependency to subprocess32 via "install_requires" (setup.py) and I am inside a python3 virtualenv: 我收到此错误消息,如果我通过“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/

There is a declarative way, but afaik it requires a more or less recent version of setuptools (if I read the release notes correctly, you need at least the 20.2 version). 有一种声明性的方式,但是它需要一个或多或少的最新版本的setuptools (如果我正确阅读发行说明 ,你至少需要20.2版本)。 What you will see down below are called environment markers and are specified in PEP 508 , read it through for a full list of available markers and a better understanding of the marker syntax if you wish. 您将在下面看到的内容称为环境标记,并在PEP 508中指定,阅读它以获取可用标记的完整列表,并在需要时更好地理解标记语法。

For python version, let's take your package as an example: you have subprocess32 dependency that should be installed in python2.X environment. 对于python版本,让我们以您的包为例:您应该在python2.X环境中安装subprocess32依赖项。 Enhance your dependency like this: 像这样增强你的依赖:

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

Installing the package subx with python2.7 now yields: 现在使用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

If you install it with python3.X , the output will be: 如果你用python3.X安装它,输出将是:

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

Notice that the installation of subprocess32 will be skipped. 请注意,将跳过subprocess32的安装。


Another common example is to declare platform-specific dependencies: I have a project that requires auditwheel to be installed on Linux and delocate on MacOS. 另一个常见的例子是声明特定于平台的依赖性:我有一个要求项目auditwheel要在Linux上安装和delocate在MacOS。 I declare the dependencies like this: 我声明这样的依赖:

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

Note that this check for Linux is needed if you do not specifically target any major python version because: 请注意,如果您没有专门针对任何主要的python版本,则需要对Linux进行此检查,因为:

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

but

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

so if for example your package works only with python2.X , you can use the check "linux2" == sys.platform . 因此,如果您的包仅适用于python2.X ,则可以使用检查"linux2" == sys.platform This will make your dependency installable only with python2.X . 这将使您的依赖项仅可以使用python2.X进行安装。

import sys

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

setup(
    …
    **kw
)

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

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