Need to know what's the difference between setup.py and setup.cfg. Both are used prominently in openstack projects
-
Also see [PEP-621](https://peps.python.org/pep-0621/) for `pyproject.toml`. – djvg Apr 19 '22 at 16:14
-
and a useful comparison in `setuptools` [documentation](https://setuptools.pypa.io/en/latest/userguide/dependency_management.html) – djvg Apr 19 '22 at 16:29
3 Answers
Traditionally, setup.py
has been used to build a Python package, i.e.,
python setup.py build
Like any old Python file, setup.py
can contain lots of code. In most cases, however, it is purely declarative and simply lists package properties, e.g.,
from setuptools import setup
setup(
name="foobar",
version="0.1.0",
author="John Doe",
# ...
)
In fact, some consider it bad style to put much logic in setup.py
. To reflect that, setup.cfg
(which is declarative by design) has become more popular for packaging:
[metadata]
name = foobar
version = 0.1.0
author = John Doe
# ...
This has the advantage that the packaging software (e.g., setuptools) doesn't need to evaluate a Python file to get the meta data, but can simply parse a config file.
You can add a dummy setup.py
to that,
from setuptools import setup
if __name__ == "__main__":
setup()
or go full PEP 517/518 and instead add a pyproject.toml
.
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
You can then build your projects using pypa-build (pip install build
) with
python3 -m build --sdist --wheel .
Since 2020 pyproject.toml
can contain metadata too, so you don't need setup.cfg
anymore. The data layout looks more or less like setup.cfg
but follows the standardized TOML format:
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"
[project]
name = "foobar"
version = "0.1.0"
authors = [{name = "John Doe", email = "john@doe.com"}]
# [...]
One design goal is that multiple build systems will support it (setuptools, flit, Poetry, ...). As of 2022, setuptools will still give you an "experimental" warning when installing that package, but that might go away soon.

- 53,797
- 27
- 201
- 249
-
6Package without `setup.py` ? `pip install --editable .` can raise "ERROR: File "setup.py" not found ... (A "pyproject.toml" file was found, but editable mode currently requires a setup.py based build.)" for example on `meshplex`. – denis Jul 24 '20 at 09:13
-
@denis You'll probably need `-m pep517.build`. (See the Makefile in meshplex.) – Nico Schlömer Jul 24 '20 at 09:23
-
1[This stackoverflow answer](https://stackoverflow.com/a/42932451/75517) explain how to create a package with a c library extension. It provides an example with code added to setup.py. Can the equivalent be done with `setup.cfg` ? If yes, could you please give an example ? – chmike Feb 08 '21 at 13:25
-
2@chmike For C extensions, you'll still have to keep a `setup.py` around. The meta data can go into `setup.cfg`, though. Check out https://github.com/nschloe/pygalmesh/blob/main/setup.py as an example. – Nico Schlömer Feb 08 '21 at 13:45
-
4I suppose this is a good answer but honestly it mostly made me more confused about what files I'm supposed to create in a new package. – Per Johansson Jun 27 '22 at 12:41
-
"has become more popular for packaging" pretty suspicious there. I think it's more... what you are meant to do. – Att Righ Nov 01 '22 at 11:53
setup.py
is the file with the actual instructions how to build your software. These instructions might have some configuration options, e.g. for unit tests you might be able to indicate whether test coverage should be computed or not, or the install prefix etc.
setup.cfg
is a file which might be used to specify such options in addition to reading the command line when calling python setup.py <somecommand>
.
The documentation for setup.cfg
states:
Often, it’s not possible to write down everything needed to build a distribution a priori: you may need to get some information from the user, or from the user’s system, in order to proceed. As long as that information is fairly simple—a list of directories to search for C header files or libraries, for example—then providing a configuration file, setup.cfg, for users to edit is a cheap and easy way to solicit it. Configuration files also let you provide default values for any command option, which the installer can then override either on the command-line or by editing the config file.

- 6,554
- 2
- 37
- 62
setup.py
is an integral part of a python package which includes details or information about the files that should be a package. This includes the required dependencies for installation and functioning of your Python package, entry points, license, etc.
setup.cfg
on the other hand is more about the settings for any plug-ins or the type of distribution you wish to create. bdist/sdist and further classification of universal or core-python wheel. It can also be used to configure some meta-data of the setup.py
.

- 9,416
- 5
- 32
- 57

- 391
- 3
- 7