Alternative dependencies (fall back) in setup.py

放肆的年华 提交于 2019-12-07 00:33:00

问题


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.

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: 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.


回答1:


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"'
]

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.

(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.)




回答2:


Your solution is the correct one for this situation. It is the best and more flexible way at the moment to achieve this task.



来源:https://stackoverflow.com/questions/23060421/alternative-dependencies-fall-back-in-setup-py

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