Carson McNeil
Carson McNeil

Reputation: 812

How do I specify two alternative requirements with setuptools by pip package name?

I want to write a setup.py file using setuptools. My package depends on tensorflow, but there are two different pip packages that fulfill the requirement, tensorflow and tensorflow-gpu. If I just put tensorflow in my setup(..., install_requires=["tensorflow"]), then installation will fail if the the user has instead installed the tensorflow-gpu pip package on their system.

The imp module can't be used to check (as in this answer: How to check if a python module exists without importing it), because the import name of the module is tensorflow regardless of which pip package the user installed. So how does setuptools (and therefore distutils) detect which pip package was installed? I've dug through the source a bit, but can't find the place that it checks.

*Note, I am not planning on hacking setuptools to accept either. I just want to know what method it is using to detect the package, so I can use that same method in my setup.py to manually set the install_requires param to the correct version. (i.e. like this: Alternative dependencies (fall back) in setup.py)

Upvotes: 5

Views: 1120

Answers (1)

phd
phd

Reputation: 94482

I answered a similar question recently. You need to distinguish one TF from the other. I don't know TF enough to help with the detail but most part of the code should be like this:

kw = {}
try:
    import tensorflow
except ImportError:  # There is no one, declare dependency
    kw['install_requires'] = ['tensorflow']
else:
    if is_gpu(tensorflow):
        kw['install_requires'] = ['tensorflow-gpu']
    else:
        kw['install_requires'] = ['tensorflow']

setup(
    …
    **kw
)

Upvotes: 2

Related Questions