Mat*_*ert 5 python pip setuptools
是否可以让 setuptoolsextras_require覆盖中设置的要求install_requires?我的猜测是否定的,extras_require就像
\n \n\n\n
因此,由于它是“可选的”,因此install_requires永远优先。我想我会问以防万一。
提出这个问题的原因是以下用例和以下示例setup.py
from setuptools import setup, find_packages\nsetup(\n ...\n install_requires = [\n \'numpy<=1.14.5,>=1.14.0\',\n ...\n ],\n extras_require = {\n ...\n \'tensorflow\':[\n \'tensorflow>=1.10.0\',\n \'numpy<=1.14.5,>=1.13.3\',\n \'setuptools<=39.1.0\',\n ]\n },\n ...\n)\nRun Code Online (Sandbox Code Playgroud)\n\n您有一个支持多个计算后端的库(例如,NumPy、TensorFlow、PyTorch),但安装的默认后端只是 NumPy,然后可以通过不同的选项安装不同的后端。TensorFlow 的要求是
\n\ntensorflow 1.10.1 has requirement numpy<=1.14.5,>=1.13.3\nRun Code Online (Sandbox Code Playgroud)\n\n但如果用户只想要 NumPy 后端,你不想强制限制 NumPy。因此理想情况下,应该install_requires只numpy>=1.14.0为那些愿意这样做的用户提供服务
pip install .\nRun Code Online (Sandbox Code Playgroud)\n\n然后对于需要 TensorFlow 的用户,他们只需使用
\n\npip install -e .[tensorflow]\nRun Code Online (Sandbox Code Playgroud)\n\n但是,这当然不起作用,就好像install_requires刚刚numpy>=1.14.0安装了 NumPy 的最新 PyPI 版本(此时1.15.1),并且您在安装过程中收到警告
tensorflow 1.10.1 has requirement numpy<=1.14.5,>=1.13.3, but you\'ll have numpy 1.15.1 which is incompatible.\nRun Code Online (Sandbox Code Playgroud)\n\n那么,无论如何,我是否可以取消限制 NumPy 版本install_requires,然后让 setuptools 检查并使用中指定的版本extras_require(如果需要)?
对于上下文:
\n\n$ python --version\nPython 3.6.6\n$ pip --version\npip 18.0 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)\n$ easy_install --version\nsetuptools 40.0.0 from /usr/local/lib/python3.6/site-packages (Python 3.6)\nRun Code Online (Sandbox Code Playgroud)\n
答案(经过更多思考)是,期望的结果是可能的,但不是通过覆盖install_requires,而是有一个要求,install_requires需要所需的库(所以这是相当 hacky 的)。为了说明使用这个setup.py
from setuptools import setup, find_packages
setup(
...
install_requires = [
'scipy', # scipy requires numpy, and so will get the latest release from PyPI
...
],
extras_require = {
...
'tensorflow':[
'tensorflow>=1.10.0',
'numpy<=1.14.5,>=1.13.3',
'setuptools<=39.1.0',
]
},
...
)
Run Code Online (Sandbox Code Playgroud)
现在
$ pip install .
$ pip freeze | grep numpy
numpy==1.15.1
$ pip freeze | grep scipy
scipy==1.1.0
Run Code Online (Sandbox Code Playgroud)
和
$ pip freeze | xargs pip uninstall -y
$ pip install .[tensorflow]
$ pip freeze | grep numpy
numpy==1.14.5
$ pip freeze | grep scipy
scipy==1.1.0
Run Code Online (Sandbox Code Playgroud)