My package version is defined in two places:
__version__ = 1.2.3
in mypackage/__init__.py
version = "1.2.3"
in pyproject.toml
(I am using Poetry)I have to update both whenever I bump the version which is annoying and not DRY. Is there a way to make Python read the version from the TOML, or to make the TOML read the version from Python?
After you have installed your project - either in editable mode by poetry install
or from the wheel - you can access several metadata via importlib.metadata
(importlib_metadata
for python < 3.8).
So keep the version only in the pyproject.toml
and use this in your python code:
import importlib.metadata
__version__ = importlib.metadata.version("mypackage")