Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "setup.cfg" instead of setup.py with Python 2.7

It seemed to me, that instead of the whole plethora of named keyword-arguments for the distutils.core.setup function, one can use a setup.cfg file in the same directory as the setup.py file and all these keywords will be read from the setup.cfg file.

I thought i could simply create a setup.cfg with Python 2.7, but a minimal testing version just does not work. I did test it with setup.py --name, which just returns: UNKNOWN.

And as usual with python-packaging the documentation is confusing as hell, as it is never clear, which version they relate to or at least how old the documentation is.

My two setup files:

setup.py:

from distutils.core import setup setup() 

setup.cfg:

[metadata] name = foo version = 0.1 

I looked into the distutils package and found that (besides being fugly as hell) it seems to use the mail.message_from_file factory to read the setup.cfg.

As i am quite ok with a setup.py-only approach i would not bother much longer with such nonsense, but i am still curious how to do it right, if it is possible at all.

Neither the official packaging doc nor the Packaging-Authority seems to be a big help here.


Almost every time i feel compelled to look into python's 2.x stdlib i am wondering if they try to showcase how not to program. On the other hand the C-Code seems quite beautiful.

like image 697
Don Question Avatar asked Nov 22 '14 12:11

Don Question


People also ask

Do you need setup py If you have setup CFG?

you must have a valid setup.py file apart from setup. cfg and pyproject. toml . You can use the same dummy setup file I shared in the previous section that makes just a single call to the setup() method.

What is setup CFG python?

setup. cfg is a cheekily named Python package which supports providing all of a Python distribution's metadata and build configuration via the setup. cfg file at the base of the distribution's source tree, rather than in the setup.py script. The standard setup.py script is reduced to a stub which uses the setup.

Is setup CFG deprecated?

Nope, setup. cfg it is not deprecated and the documentation you mention is misleading. There were serious reasons like security related to the fact that setup.py needed execution and that's the main reason of moving away from it.

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

What is the purpose of setup Py config?

The setup.py and setup.cfg files are artefacts of the setuptools module which is designed to help with the packaging process. It is used by pip whose purpose is to install a package either locally or remotely. If we do not configure setup.py/setup.cfg correctly then pip will not work.

How do I setup a Python project with Setuptools?

Structure your Python project like this with setup.py, setup.cfg and pyproject.toml in the top level with a tests directory and a src directory with a package subdirectory inside that: The minimal setup.py file simply contains an invocation of the setuptools setup function:

How to create a dummy setup in Python?

You can pretty much specify every keyword we used in the setup.py file in the new setup.cfg file and simply use the setup.py file as the command line interface. Now assuming that you have moved all the options into a setup.cfg file as described in the previous section, you can now create a dummy setup.py that will simply call the setup () method:

How to add flake8 to setup_requires in Python?

Instead, you can add it to setup_requires: Now you can simply run python setup.py flake8. Of course you can also pin the version of flake8 (or any other package) in setup_requires. your setup.cfg. For example: Sometimes you may want to include some non-python files in your package. These may for example be schema files or a small lookup table.


1 Answers

Note that, as of December 2016 and setuptools version 30.3.0, it is possible to put package metadata in setup.cfg, per idle sign's answer.


The problem is that the setup.cfg file does not do what you want. It does not provide parameters to the setup function. It is used to supply parameters to the commands that setup.py makes available. You can list the supported commands with setup.py --help-commands. You should see something like:

(env) gondolin/zender% ./setup.py --help-commands Standard commands:   build             build everything needed to install   build_py          "build" pure Python modules (copy to build directory)   .....   install_data      install data files   sdist             create a source distribution (tarball, zip file, etc.) 

This is the list of sections that you can put in a setup.cfg file. You can list the options that a command supports using setup.py --help command. For example, the sdist command supports the following options:

(env) gondolin/zender% ./setup.py --help sdist Common commands: (see '--help-commands' for more) .... Options for 'sdist' command:   --formats         formats for source distribution (comma-separated list)   --keep-temp (-k)  keep the distribution tree around after creating archive                     file(s)   --dist-dir (-d)   directory to put the source distribution archive(s) in                     [default: dist]   --help-formats    list available distribution formats 

You can control what happens when a user runs ./setup.py sdist in your project by adding a setup.cfg file like the following.

[sdist] keep-temp = 1 dist-dir = dist/source 

So... setup.cfg simply configures the behavior of the various setup commands for your project. The setup function really needs to have the metadata supplied to it as keyword parameters. You could write your own version of the distutils.dist.Distribution class that pulls metadata from setup.cfg and provide it as the distclass= keyword parameter to setup.

The missing piece to the puzzle is that the standard Distribution class does not provide a way to pass the path parameter to the distutils.dist.DistributionMetadata initializer which does pretty much what you want - it reads the package information using the email parsing stuff that you mentioned. What you found is the code that is used to process a PEP-314/PEP-345 metadata file. This is not used by the setup function. Instead, it is used to parse the metadata embedded in a distributed package.

like image 61
D.Shawley Avatar answered Sep 24 '22 01:09

D.Shawley