5

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)

Carson McNeil
  • 790
  • 9
  • 22

1 Answers1

2

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
)
phd
  • 82,685
  • 13
  • 120
  • 165
  • Yea, in the end I did something like this...but this can't be the method that setuptools is using because it's module-specific. Is it checking the pip database somehow? – Carson McNeil Jun 27 '17 at 20:07
  • There is no 'pip database'. I think setuptools and Co scan *.dist-info directories in site-packages. – phd Jun 27 '17 at 20:14
  • It should be noted that this otherwise awesome answer fails to scale to wheels, which are unable to perform install-time logic of this sort. What can you do, eh? `setuptools`. `setuptools` never changes. – Cecil Curry Aug 02 '17 at 03:28