You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/single_source_version.rst
+9-13Lines changed: 9 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,15 +9,13 @@ Single-sourcing the Project Version
9
9
10
10
One of the challenges in building packages is that the version string can be required in multiple places.
11
11
12
-
* It needs to be specified when building the package (e.g. in :file:`pyproject.toml`)
12
+
* It needs to be specified when building the package (e.g. in :file:``pyproject.toml``)
13
13
- That will assure that it is properly assigned in the distribution file name, and in the installed package.
14
14
15
-
* Some projects prefer that there be a version string available as an attribute in the importable module, e.g::
15
+
* A package may, if the maintainers think it useful, to be able to determine the version of the package from Python code without relying on a package manager's metadata, set a top level ``__version__`` attribute.
16
+
The value of ``__version__`` attribute and that passed to the build system can (and should) be kept in sync in :ref:`the build systems's recommended way <how_to_set_version_links>`.
16
17
17
-
import a_package
18
-
print(a_package.__version__)
19
-
20
-
* In the metadata of the artifacts for each of the packaging ecosystems
18
+
Should a package not set a top level ``__version__`` attribute, the version can still be accessed using ``importlib.metadata.version("distribution_name")``.
21
19
22
20
While different projects have different needs, it's important to make sure that there is a single source of truth for the version number.
23
21
@@ -27,17 +25,15 @@ In general, the options are:
27
25
28
26
2) The version can be hard-coded into the `pyproject.toml` file -- and the build system can copy it into other locations it may be required.
29
27
30
-
3) The version string can be hard-coded into the source code -- either in a special purpose file, such as `_version.txt`, or as a attribute in the `__init__.py`, and the build system can extract it at build time.
31
-
32
-
If the version string is not in the source, it can be extracted at runtime with code in `__init__.py`, such as::
3) The version string can be hard-coded into the source code -- either in a special purpose file, such as ``_version.txt``, or as a attribute in the ``__init__.py``, and the build system can extract it at build time.
36
29
37
30
38
31
Consult your build system documentation for how to implement your preferred method.
0 commit comments