# Replacement for ./setup.py --version?

**URL:** <https://discuss.python.org/t/replacement-for-setup-py-version/39103>\
**Category:** Python Help\
**Tags:** help, packaging-help\
**Created:** [November 19, 2023, 7:51am UTC](https://discuss.python.org/t/replacement-for-setup-py-version/39103 "2023-11-19T07:51:09Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![sinoroc](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sinoroc/32/1607_2.png) [@sinoroc](https://discuss.python.org/u/sinoroc)\
**Post date:** [November 20, 2023, 7:04pm UTC](https://discuss.python.org/t/replacement-for-setup-py-version/39103/4 "2023-11-20T19:04:42Z")

</div>

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

- [setuptools - How to get `setup.cfg` metadata at the command line (Python) - Stack Overflow](https://stackoverflow.com/a/71276197)

In the case of _setuptools-scm_, there is indeed an easy way, `python -m setuptools_scm`:

- [Usage - setuptools scm](https://setuptools-scm.readthedocs.io/en/latest/usage/#as-cli-tool)

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:

- [How to modernize a setup.py based project? - Python Packaging User Guide](https://packaging.python.org/en/latest/guides/modernize-setup-py-project/#what-if-something-that-can-not-be-changed-expects-a-setup-py-file)

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`_](https://docs.python.org/3/library/tomllib.html) or [_`configparser`_](https://docs.python.org/3/library/configparser.html)

* * *

For an “over-kill” solution that allows getting any meta-data field from any project, with the help of the [_`build`_ project](https://pypi.org/project/build/):

```python
#!/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_”)](https://pypa-build.readthedocs.io/en/latest/api.html#module-build.util) and it was added in [“0.7.0 (16-09-2021)”](https://pypa-build.readthedocs.io/en/latest/changelog.html#id3), in this [change](https://github.com/pypa/build/pull/340).

---

_[View the full topic](https://discuss.python.org/t/replacement-for-setup-py-version/39103)._
