Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative dependencies (fall back) in setup.py

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.

like image 771
Oleksandr Fedorov Avatar asked Apr 14 '14 12:04

Oleksandr Fedorov


1 Answers

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

like image 89
tripleee Avatar answered Oct 19 '22 22:10

tripleee