问题描述
说我要安装pyodbc
.它不能在某些Windows机器上构建,但是还有一种选择-pypyodbc
,它是pyobdc
的纯python实现.
Say I want to install pyodbc
. It can't be build on some Windows machines but there's an alternative - pypyodbc
which is pure python implementation of pyobdc
.
是否有一种方法可以为setuptools.setup
指定install_requires=["pyobdc"]
并在未安装前一个软件包的情况下退回到pypyodbc
?
Is there a way to specify install_requires=["pyobdc"]
for setuptools.setup
with falling back to pypyodbc
if the former package wasn't installed?
UPD:针对这种情况的解决方案:
UPD: My solution for this particular situation:
import sys
from setuptools import setup
if sys.platform.startswith("win"):
pyodbc = "pypyodbc>=1.2.0"
else:
pyodbc = "pyodbc>=3.0.7"
...
setup(
...
install_requires=[pyobdc]
)
但是我仍然在寻找更通用的解决方案.
But I still look for a more general solution.
推荐答案
做你已经在做的事情似乎是一个常见的建议,但是由于这个问题是此类问题在Google网站上的热门话题,因此我要指出install_requires
支持在 PEP 508 中指定的相当复杂的迷你语言. :
Doing what you are already doing seems like a common recommendation, but since this question is the top google hit for this sort of question, I'll point out that install_requires
supports a fairly intricate mini-language which is specified in PEP 508:
install_requires = [
'pypyodbc>=1.2.0;platform_system=="Windows"',
'pyodbc>=3.0.7;platform_system!="Windows"'
]
在对相关问题的评论中,用户指出,用代码计算您的install_requires
可能会产生一些不利影响,因此应优先选择上述内容以避免该问题.
In a comment to a related question, user Marius Gedminas notes that having your install_requires
computed in code could have some adverse effects, so the above should be preferred to avoid that problem.
(另一方面, https://hynek.me/articles/conditional-python -dependencies/如果您必须支持setuptools
的旧版本,则会对一些非常严重的可移植性问题感到遗憾.)
(On the other hand, https://hynek.me/articles/conditional-python-dependencies/ laments some pretty serious portability problems if you have to support old versions of setuptools
.)
这篇关于setup.py中的替代依赖项(回退)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!