Reputation: 3138
If designing a setup.py and there are requirements that can be fulfilled through two different modules, i.e. only one of the two needs to be installed, how do I express that in the install_requires
line?
E.g. the setup.py
looks like this:
setup(
name='my_module',
version='0.1.2',
url='...',
...,
install_requires=['numpy', 'tensorflow']
)
When doing pip install my_module
this will install tensorflow (CPU) as a requirement, even though tensorflow-gpu is installed (which would meet the requirement as well, but doesn't, since it's named differently).
Can I do something like:
install_requires=['numpy', 'tensorflow' or 'tensorflow-gpu']
Upvotes: 4
Views: 1348
Reputation: 66201
The setup.py
is just another python script, so you can put any logic in it to determine the correct setup args. For example, you can check whether tensorflow
or tensorflow_gpu
are installed and modify the list of install deps on the fly:
from pkg_resources import DistributionNotFound, get_distribution
from setuptools import setup
def get_dist(pkgname):
try:
return get_distribution(pkgname)
except DistributionNotFound:
return None
install_deps = []
if get_dist('tensorflow') is None and get_dist('tensorflow_gpu') is None:
install_deps.append('tensorflow')
setup(
...
install_requires=install_deps,
)
Note, however, that once you start putting code in your setup script that performs any stuff at install time, you can't distribute wheels anymore because wheels don't ship the setup.py
, instead executing them once at build time. Source distributions will serve just fine:
$ python setup.py sdist
running sdist
running egg_info
...
creating dist
Creating tar archive
removing 'mypkg-0.0.0' (and everything under it)
Installing the resulting source tar will pull tensorflow
if not installed:
$ pip install dist/mypkg-0.0.0.tar.gz
Processing ./dist/mypkg-0.0.0.tar.gz
Collecting tensorflow (from mypkg==0.0.0)
...
Installing collected packages: tensorflow, mypkg
Successfully installed mypkg-0.0.0 tensorflow-1.6.0
Upvotes: 7
Reputation: 858
You need an extras_require
in your setup function:
extras_require = {
'gpu': ['tensorflow-gpu'],
'cpu': ['tensorflow']
},
Which then can be installed with:
pip install your_package[gpu]
or pip install your_package[cpu]
Upvotes: 6