45

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?

4 Answers 4

32

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.

5
  • 3
    Works like a charm, as documented here.
    – pandichef
    Jul 27, 2020 at 1:54
  • 7
    for reference pip install . from the same directory as setup.py Jun 16, 2021 at 13:07
  • 3
    Now it is even possible to accompany setup.cfg by empty pyproject.toml instead of the minimal setup.py to allow pip install. (pip 21.3.1, Python 3.8.10) Jan 12, 2022 at 10:50
  • 4
    As of March 2022, with setuptools>=61, one can ditch setup.cfg in favor of pyproject.toml (docs). setuptools is still working on letting one ditch the placeholder setup.py file in favor of just a pyproject.toml May 2, 2022 at 4:33
  • It does not work for me: ERROR: You must give at least one requirement to install (see "pip help install") MacOS 12.6.2 Chipset M1 - Python 3.10.9 Jan 10, 2023 at 16:36
15

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.

1
  • 2
    Excellent! Thanks - I'm using packages = find in my project, and pip install -e . worked like a charm. Jan 12, 2022 at 17:35
8

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'])
1
  • This script as-is does not support packages that must be installed via dependency_links. Dec 12, 2017 at 20:30
7

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.

6
  • 3
    Or, maybe you just want the requirements to be installed, but not the actual package? AFAIK, pip doesn't have the ability to read setup.cfg itself, all of the options are read in to setup.py by setuptools.config.read_configuration.
    – rnorris
    Sep 13, 2017 at 21:03
  • The comment by @rnorris is correct. I don't want to install the Python package itself, I want to install its dependencies. I want something like pip install -r requirements.txt, but something like pip install -r setup.cfg. Sep 14, 2017 at 14:25
  • Pip itself does not support that. You will probably need to roll your own solution. Can I ask what you're trying to accomplish by installing the dependencies of a particular module, but not the module itself?
    – rnorris
    Sep 14, 2017 at 17:03
  • 4
    Installing the dependencies in a virtual environment in order to develop the project itself (without necessarily installing it). Sep 14, 2017 at 17:05
  • 4
    I think for the use case argentpepper mentions, pip install --editable . would be an appropriate solution. The intended use case of the --editable flag is to make the development package available in the install environment and have any source changes show up without having to re-install the package.
    – rnorris
    Jan 14, 2021 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.