可以让 setuptools extras_require 覆盖 install_requires 的要求吗?
Possible to have setuptools extras_require override requirement from install_requires?
是否可以让 setuptools extras_require
覆盖 install_requires
中设置的要求?我的猜测是不,因为 extras_require
是
...dictionary mapping names of “extras” (optional features of your project)...
所以既然是 "optional",那么 install_requires
总是优先的。我想我会问以防万一。
问这个的原因是下面的用例和下面的例子setup.py
from setuptools import setup, find_packages
setup(
...
install_requires = [
'numpy<=1.14.5,>=1.14.0',
...
],
extras_require = {
...
'tensorflow':[
'tensorflow>=1.10.0',
'numpy<=1.14.5,>=1.13.3',
'setuptools<=39.1.0',
]
},
...
)
您有一个支持多个计算后端的库(例如,NumPy、TensorFlow、PyTorch),但默认安装的后端只是 NumPy,然后可以通过不同的选项安装不同的后端。 TensorFlow 有这样的要求
tensorflow 1.10.1 has requirement numpy<=1.14.5,>=1.13.3
但是如果用户只想要 NumPy 后端,您不想强行限制 NumPy。所以 理想情况下,install_requires
中 numpy>=1.14.0
的用户会
pip install .
然后对于需要 TensorFlow 的用户,他们只需使用
pip install -e .[tensorflow]
然而,这当然行不通,就好像 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.
那么我是否可以在 install_requires
中取消限制 NumPy 版本,然后让 setuptools 检查并在需要时使用 extras_require
中指定的版本?
对于上下文:
$ python --version
Python 3.6.6
$ pip --version
pip 18.0 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
$ easy_install --version
setuptools 40.0.0 from /usr/local/lib/python3.6/site-packages (Python 3.6)
答案(经过更多思考)是期望的结果是可能的,但是不是通过覆盖install_requires
,但在 install_requires
中有一个要求需要所需的库(所以这很老套)。为了说明使用这个 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',
]
},
...
)
现在
$ pip install .
$ pip freeze | grep numpy
numpy==1.15.1
$ pip freeze | grep scipy
scipy==1.1.0
和
$ 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
是否可以让 setuptools extras_require
覆盖 install_requires
中设置的要求?我的猜测是不,因为 extras_require
是
...dictionary mapping names of “extras” (optional features of your project)...
所以既然是 "optional",那么 install_requires
总是优先的。我想我会问以防万一。
问这个的原因是下面的用例和下面的例子setup.py
from setuptools import setup, find_packages
setup(
...
install_requires = [
'numpy<=1.14.5,>=1.14.0',
...
],
extras_require = {
...
'tensorflow':[
'tensorflow>=1.10.0',
'numpy<=1.14.5,>=1.13.3',
'setuptools<=39.1.0',
]
},
...
)
您有一个支持多个计算后端的库(例如,NumPy、TensorFlow、PyTorch),但默认安装的后端只是 NumPy,然后可以通过不同的选项安装不同的后端。 TensorFlow 有这样的要求
tensorflow 1.10.1 has requirement numpy<=1.14.5,>=1.13.3
但是如果用户只想要 NumPy 后端,您不想强行限制 NumPy。所以 理想情况下,install_requires
中 numpy>=1.14.0
的用户会
pip install .
然后对于需要 TensorFlow 的用户,他们只需使用
pip install -e .[tensorflow]
然而,这当然行不通,就好像 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.
那么我是否可以在 install_requires
中取消限制 NumPy 版本,然后让 setuptools 检查并在需要时使用 extras_require
中指定的版本?
对于上下文:
$ python --version
Python 3.6.6
$ pip --version
pip 18.0 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
$ easy_install --version
setuptools 40.0.0 from /usr/local/lib/python3.6/site-packages (Python 3.6)
答案(经过更多思考)是期望的结果是可能的,但是不是通过覆盖install_requires
,但在 install_requires
中有一个要求需要所需的库(所以这很老套)。为了说明使用这个 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',
]
},
...
)
现在
$ pip install .
$ pip freeze | grep numpy
numpy==1.15.1
$ pip freeze | grep scipy
scipy==1.1.0
和
$ 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