All of the current answers address the issue of getting the version after install. However, as Jonathan Belden points out in a comment on @finswimmer's answer, these methods break in CI pipelines and the like when the package isn't installed. In keeping with @z33k's comment on the question, one solution would be to read the value from pyproject.toml
.
With poetry as your package manager, another solution would be to use the poetry-bumpversion plugin to manage version bumps using poetry
's version
command. For example, say you have a package called widget
, with __version__
defined in widget/__init__.py
, with the same value as pyproject.toml
has for version
. With the poetry-bumpversion
plugin, you would add
[tool.poetry_bumpversion.file."widget/__init__.py"]
to pyproject.toml
, then
% poetry version patch
Bumping version from 0.7.9 to 0.7.10
poetry_bumpversion: processed file widget/__init__.py
% git diff -U0
diff --git a/widget/__init__.py b/widget/__init__.py
index 18c7cbf..9ff9982 100644
--- a/widget/__init__.py
+++ b/widget/__init__.py
@@ -1 +1 @@
-__version__ = "0.7.9"
+__version__ = "0.7.10"
diff --git a/pyproject.toml b/pyproject.toml
index 1b86c6e..5de1ce1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -3 +3 @@ name = "widget"
-version = "0.7.9"
+version = "0.7.10"
@@ -55,0 +56,2 @@ tox = "^4.6.4"
+[tool.poetry_bumpversion.file."widget/__init__.py"]
+