Replacement for ./setup.py --version?

That is a good question. One that I have seen asked more than once. And the “correct” solution is not that simple I am afraid (see below):

In the case of setuptools-scm, there is indeed an easy way, python -m setuptools_scm:

Personally I think it is fine to use python setup.py --version, as long as you are fully aware that it is not supported, that it might break, and that you should really avoid it. But if there is no other solution, then maybe it is okay-ish:

Then there is of course the cases where the metadata (here the version string) is clearly specified in some easy to parse file such as pyproject.toml or setup.cfg. In such cases it should be straightfoward to extract the value with tomllib or configparser


For an “over-kill” solution that allows getting any meta-data field from any project, with the help of the build project:

#!/usr/bin/env python

import argparse
import pathlib

import build.util

def _main():
    args_parser = argparse.ArgumentParser()
    args_parser.add_argument('path')
    args = args_parser.parse_args()
    path_name = getattr(args, 'path')
    path = pathlib.Path(path_name)
    #
    metadata = build.util.project_wheel_metadata(path)
    print(metadata)

if __name__ == '__main__':
    _main()

This will actually call the build back-end (setuptools, poetry, flit, pdm, or watever), so this might take some seconds.

The build.util API is documented here (on “latest”) and it was added in “0.7.0 (16-09-2021)”, in this change.