Can pip install from setup.cfg, as if installing from a requirements file?
Question:
According to the setuptools
documentation, setuptools
version 30.3.0 (December 8, 2016) “allows using configuration files (usually setup.cfg) to define package’s metadata and other options which are normally supplied to setup()
function”. Similar to running pip install -r requirements.txt
to install Python packages from a requirements file, is there a way to ask pip
to install the packages listed in the install_requires
option of a setup.cfg
configuration file?
Answers:
No, pip does not currently have facilities for parsing requirements from setup.cfg
. It will only install dependencies along with the main package(s) provided in setup.py
.
Here is my workaround. I use the following command to parse the install_requires
element from the setup.cfg
file and install the packages using pip
.
python3 -c "import configparser; c = configparser.ConfigParser(); c.read('setup.cfg'); print(c['options']['install_requires'])" | xargs pip install
Here is a more readable version of the Python script before the pipe in the above command line.
import configparser
c = configparser.ConfigParser()
c.read('setup.cfg')
print(c['options']['install_requires'])
If you have all your dependencies and other metadata defined in setup.cfg
, just create a minimal setup.py
file in the same directory that looks like this:
from setuptools import setup
setup()
From now on you can run pip install
and it will install all the dependencies defined in setup.cfg
as if they were declared in setup.py
.
If your setup.cfg
belongs to a well-formed package, you can do e.g.:
pip install -e .[tests,dev]
(install this package in place, with given extras)
afterwards you can pip uninstall
that package by name, leaving deps in place.
According to the setuptools
documentation, setuptools
version 30.3.0 (December 8, 2016) “allows using configuration files (usually setup.cfg) to define package’s metadata and other options which are normally supplied to setup()
function”. Similar to running pip install -r requirements.txt
to install Python packages from a requirements file, is there a way to ask pip
to install the packages listed in the install_requires
option of a setup.cfg
configuration file?
No, pip does not currently have facilities for parsing requirements from setup.cfg
. It will only install dependencies along with the main package(s) provided in setup.py
.
Here is my workaround. I use the following command to parse the install_requires
element from the setup.cfg
file and install the packages using pip
.
python3 -c "import configparser; c = configparser.ConfigParser(); c.read('setup.cfg'); print(c['options']['install_requires'])" | xargs pip install
Here is a more readable version of the Python script before the pipe in the above command line.
import configparser
c = configparser.ConfigParser()
c.read('setup.cfg')
print(c['options']['install_requires'])
If you have all your dependencies and other metadata defined in setup.cfg
, just create a minimal setup.py
file in the same directory that looks like this:
from setuptools import setup
setup()
From now on you can run pip install
and it will install all the dependencies defined in setup.cfg
as if they were declared in setup.py
.
If your setup.cfg
belongs to a well-formed package, you can do e.g.:
pip install -e .[tests,dev]
(install this package in place, with given extras)
afterwards you can pip uninstall
that package by name, leaving deps in place.