Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between setup.py and setup.cfg in python projects

Need to know what's the difference between setup.py and setup.cfg. Both are used prominently in openstack projects

like image 569
Bharadwaj Avatar asked Sep 14 '16 07:09

Bharadwaj


People also ask

What is setup CFG used for in python?

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.

What is setup py in python project?

The setup.py file may be the most significant file that should be placed at the root of the Python project directory. It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on.

What is the use of setup CFG file?

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.

Is setup py outdated?

py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools. I found a very detailed write-up explaining this issue: "Why you shouldn't invoke setup.py directly" (October 2021).


1 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 . 

That being said, the landscape of Python packaging software is very much in motion at the moment (2019/20/21) and it is not clear which will be the preferred method of defining a Python package in the future. For example, there is PEP 621 which suggests to put package metadata into pyproject.toml.

like image 86
Nico Schlömer Avatar answered Oct 07 '22 15:10

Nico Schlömer