Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify dependencies that setup.py needs during installation?

In some cases setup.py needs to import some extra modules, e.g.:

from setuptools import setup
import foo

setup(
    # e.g. a keyword uses `foo`
    version=foo.generate_version()
)

If foo is not installed, the execution of setup.py will fail because of ImportError.

I have tried to use setup_requires, e.g. setup_requires=['foo'], but it doesn’t help.

So how to specify this kind of dependencies?

like image 715
TitanSnow Avatar asked Apr 08 '18 08:04

TitanSnow


3 Answers

Dependencies needed in setup.py cannot be specified in the very setup.py. You have to install them before running python setup.py:

pip install -r requirements.txt
python setup.py install
like image 120
phd Avatar answered Oct 07 '22 04:10

phd


pyproject.toml allows to specify custom build tooling:

[build-system]
requires = [..., foo, ...]
build-backend = "setuptools.build_meta"
like image 28
Tosha Avatar answered Oct 07 '22 03:10

Tosha


I have thought out a trick — call pip to install the dependencies in setup.py

import pip
pip.main(['install', 'foo', 'bar'])    # call pip to install them

# Now I can import foo & bar
import foo, bar
from setuptools import setup

setup(
    ...
)
like image 42
TitanSnow Avatar answered Oct 07 '22 04:10

TitanSnow