YoavKlein
YoavKlein

Reputation: 2703

Obtain the version of a setup.cfg package

When using setup.py in a Python project, we can simply run

$ python3 setup.py --version

And this will give us the version field that is set in the setup.py file. This saves us using sed or something alike to read the version when we need to do so in a script.

I was wondering, is there an equivalent way in a setup.cfg project?

Upvotes: 3

Views: 921

Answers (2)

wim
wim

Reputation: 362716

When you have only a setup.cfg, and no setup.py file, you can do this:

$ cat setup.cfg
[metadata]
name = mypkg
version = "0.1.2.3"  # or `file: VERSION.txt` or `attr: mypkg.__version__` etc

$ python3 -c 'import setuptools; setuptools.setup()' --version
"0.1.2.3"

If the build backend is setuptools, then this approach will also work for a source tree with a pyproject.toml file.

Upvotes: 4

Brandt
Brandt

Reputation: 5639

Allow me to deviate from your actual question to offer what I thing is a better solution.

If you have to get the package version while developing (from an outside script) then you should split such information from your setup.cfg/setup.py.

You want setup.cfg (i.e, setuptools) to get that information from a file, for instance. Then, you can have the version just by reading the corresponding file.

Check the docs for details: https://setuptools.pypa.io/en/latest/userguide/declarative_config.html. Notice the #meta-1 anchor/note in that page.

Basically, you'll use attr or file attributes (in your setup.cfg metadata section) to get the version from another place:

version = file: VERSION.txt

Upvotes: -2

Related Questions