Skip to content

Commit afb69f3

Browse files
authored
Merge pull request #1580 from ChrisBarker-NOAA/simplify_single_source
Simplify single source
2 parents bcbb476 + 4d7e33a commit afb69f3

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

source/discussions/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ specific topic. If you're just trying to get stuff done, see
1616
package-formats
1717
src-layout-vs-flat-layout
1818
setup-py-deprecated
19+
single-source-version
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. _`Single sourcing the version discussion`:
2+
3+
===================================
4+
Single-sourcing the Project Version
5+
===================================
6+
7+
:Page Status: Complete
8+
:Last Reviewed: 2024-08-24
9+
10+
One of the challenges in building packages is that the version string can be required in multiple places.
11+
12+
* It needs to be specified when building the package (e.g. in :file:`pyproject.toml`)
13+
This will make it available in the installed package’s metadata, from where it will be accessible at runtime using ``importlib.metadata.version("distribution_name")``.
14+
15+
* A package may set a module attribute (e.g., ``__version__``) to provide an alternative means of runtime access to the version of the imported package. If this is done, the value of the attribute and that used by the build system to set the distribution's version should be kept in sync in :ref:`the build systems's recommended way <Build system version handling>`.
16+
17+
* If the code is in in a version control system (VCS), e.g. Git, the version may appear in a *tag* such as ``v1.2.3``.
18+
19+
To ensure that version numbers do not get out of sync, it is recommended that there is a single source of truth for the version number.
20+
21+
In general, the options are:
22+
23+
1) If the code is in a version control system (VCS), e.g. Git, then the version can be extracted from the VCS.
24+
25+
2) The version can be hard-coded into the :file:`pyproject.toml` file -- and the build system can copy it into other locations it may be required.
26+
27+
3) The version string can be hard-coded into the source code -- either in a special purpose file, such as :file:`_version.txt`, or as a attribute in a module, such as :file:`__init__.py`, and the build system can extract it at build time.
28+
29+
30+
Consult your build system's documentation for their recommended method.
31+
32+
.. _Build system version handling:
33+
34+
Build System Version Handling
35+
-----------------------------
36+
37+
The following are links to some build system's documentation for handling version strings.
38+
39+
* `Flit <https://flit.pypa.io/en/stable/>`_
40+
41+
* `Hatchling <https://hatch.pypa.io/1.9/version/>`_
42+
43+
* `PDM <https://pdm-project.org/en/latest/reference/pep621/#__tabbed_1_2>`_
44+
45+
* `Setuptools <https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html#dynamic-metadata>`_
46+
47+
- `setuptools_scm <https://setuptools-scm.readthedocs.io/en/latest/>`_

0 commit comments

Comments
 (0)