install_requires based on python version

为君一笑 提交于 2019-12-18 10:33:10

问题


I have a module that works both on python 2 and python 3. In Python<3.2 I would like to install a specific package as a dependency. For Python>=3.2.

Something like:

 install_requires=[
    "threadpool >= 1.2.7 if python_version < 3.2.0",
 ],

How can one make that?


回答1:


setuptools has support for this using environment markers.

install_requires=[
    'enum34;python_version<"3.4"',
    'pywin32 >= 1.0;platform_system=="Windows"'
]

Use of this is detailed in the official documentation. Based on the change log was added in v20.5, but the implementation wasn't stable until v20.8.1 (which was only a gap of 15 days).


Original Answer (still valid, but might be deprecated in the future):

setuptools has support for this using within the extras_require argument.

The format is the following:

extras_require={
    ':python_version=="2.7"': ["mock"],
},

It will support the other comparison operators.


Sadly, it is not mentioned in the documentation. While looking for answers, I found PEP-426 talking about "environment markers". With that phrase I was able to find a setuptools ticket with the following comment:

I've successfully used the markers feature for selectively and declaratively requiring a dependency. See backports.unittest_mock for an example. Through the 'extras', mock will be required, but only on Python 2. When I can rely on Setuptools 17.1, I can change that dependency to python_version < "3.3".




回答2:


This has been discussed here, it would appear the recommend way is to test for the Python version inside your setup.py using sys.version_info;

import sys

if sys.version_info >= (3,2):
    install_requires = ["threadpool >= 1.2.7"]
else:
    install_requires = ["threadpool >= 1.2.3"]

setup(..., install_requires=install_requires)


来源:https://stackoverflow.com/questions/21082091/install-requires-based-on-python-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!