Skip to content

Commit ae5322b

Browse files
Merge pull request #1069 from ianw/version-file
docs: clarify usage of version files
2 parents 1ddd483 + 51822d7 commit ae5322b

File tree

3 files changed

+63
-44
lines changed

3 files changed

+63
-44
lines changed

docs/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Callables or other Python objects have to be passed in `setup.py` (via the `use_
2323
`version_file: Path | PathLike[str] | None = None`
2424
: A path to a file that gets replaced with a file containing the current
2525
version. It is ideal for creating a ``_version.py`` file within the
26-
package, typically used to avoid using `pkg_resources.get_distribution`
26+
package, typically used to avoid using `importlib.metadata`
2727
(which adds some overhead).
2828

2929
!!! warning ""

docs/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ or [configuring Git archive][git-archive-docs].
1212

1313
[git-archive-docs]: usage.md#builtin-mechanisms-for-obtaining-version-numbers
1414

15-
## basic usage
15+
## Basic usage
1616

17-
### with setuptools
17+
### With setuptools
1818

1919
Note: `setuptools-scm>=8` intentionally doesn't depend on setuptools to ease non-setuptools usage.
2020
Please ensure a recent version of setuptools (>=64) is installed.
@@ -37,7 +37,7 @@ dynamic = ["version"]
3737
```
3838

3939

40-
### with hatch
40+
### With hatch
4141

4242
[Hatch-vcs](https://github.com/ofek/hatch-vcs) integrates with setuptools-scm
4343
but provides its own configuration options,

docs/usage.md

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Usage
22

3-
## at build time
3+
## At build time
44

55
The preferred way to configure `setuptools-scm` is to author
66
settings in the `tool.setuptools_scm` section of `pyproject.toml`.
@@ -25,7 +25,9 @@ that support PEP 518 ([pip](https://pypi.org/project/pip) and
2525
[pep517](https://pypi.org/project/pep517/)).
2626
Tools that still invoke `setup.py` must ensure build requirements are installed
2727

28-
### version files
28+
### Version files
29+
30+
Version files can be created with the ``version_file`` directive.
2931

3032
```toml title="pyproject.toml"
3133
...
@@ -34,15 +36,13 @@ version_file = "pkg/_version.py"
3436
```
3537
Where ``pkg`` is the name of your package.
3638

39+
Unless the small overhead of introspecting the version at runtime via
40+
`importlib.metadata` is a concern or you need a version file in an
41+
alternative format such as plain-text (see ``version_file_template``)
42+
you most likely do _not_ need to write a separate version file; see
43+
the runtime discussion below for more details.
3744

38-
```commandline
39-
$ python -m setuptools_scm
40-
41-
# To explore other options, try:
42-
$ python -m setuptools_scm --help
43-
```
44-
45-
## as cli tool
45+
## As cli tool
4646

4747
If you need to confirm which version string is being generated
4848
or debug the configuration, you can install
@@ -65,46 +65,31 @@ $ python -m setuptools_scm ls # output trimmed for brevity
6565
...
6666
```
6767

68-
!!! note "committed files only"
68+
!!! note "Committed files only"
6969

7070
currently only committed files are listed, this might change in the future
7171

7272
!!! warning "sdists/archives don't provide file lists"
7373

74-
currently there is no builtin mechanism
75-
to safely transfer the file lists to sdists or obtaining them from archives
76-
coordination for setuptools and hatch is ongoing
77-
78-
## at runtime (strongly discouraged)
79-
80-
the most simple **looking** way to use `setuptools-scm` at runtime is:
81-
82-
```python
83-
from setuptools_scm import get_version
84-
version = get_version()
85-
```
86-
87-
88-
In order to use `setuptools-scm` from code that is one directory deeper
89-
than the project's root, you can use:
90-
91-
```python
92-
from setuptools_scm import get_version
93-
version = get_version(root='..', relative_to=__file__)
94-
```
95-
96-
97-
## Python package metadata
74+
Currently there is no builtin mechanism
75+
to safely transfer the file lists to sdists or obtaining them from archives.
76+
Coordination for setuptools and hatch is ongoing.
9877

78+
To explore other options, try
9979

80+
```commandline
81+
$ python -m setuptools_scm --help
10082
83+
## At runtime
10184
102-
### version at runtime
85+
### Python Metadata
10386
104-
If you have opted not to hardcode the version number inside the package,
105-
you can retrieve it at runtime from [PEP-0566](https://www.python.org/dev/peps/pep-0566/) metadata using
87+
The standard method to retrieve the version number at runtime is via
88+
[PEP-0566](https://www.python.org/dev/peps/pep-0566/) metadata using
10689
``importlib.metadata`` from the standard library (added in Python 3.8)
107-
or the [`importlib_metadata`](https://pypi.org/project/importlib-metadata/) backport:
90+
or the
91+
[`importlib_metadata`](https://pypi.org/project/importlib-metadata/)
92+
backport for earlier versions:
10893
10994
```python title="package_name/__init__.py"
11095
from importlib.metadata import version, PackageNotFoundError
@@ -116,6 +101,40 @@ except PackageNotFoundError:
116101
pass
117102
```
118103

104+
### Via your version file
105+
106+
If you have opted to create a Python version file via the standard
107+
template, you can import that file, where you will have a ``version``
108+
string and a ``version_tuple`` tuple with elements corresponding to
109+
the version tags.
110+
111+
```python title="Using package_name/_version.py"
112+
import package_name._version as v
113+
114+
print(v.version)
115+
print(v.version_tuple)
116+
```
117+
118+
### Via setuptools_scm (strongly discouraged)
119+
120+
While the most simple **looking** way to use `setuptools_scm` at
121+
runtime is:
122+
123+
```python
124+
from setuptools_scm import get_version
125+
version = get_version()
126+
```
127+
128+
it is strongly discouraged to call directly into `setuptools_scm` over
129+
the standard Python `importlib.metadata`.
130+
131+
In order to use `setuptools_scm` from code that is one directory deeper
132+
than the project's root, you can use:
133+
134+
```python
135+
from setuptools_scm import get_version
136+
version = get_version(root='..', relative_to=__file__)
137+
```
119138

120139
### Usage from Sphinx
121140

@@ -132,7 +151,7 @@ the working directory for good reasons and using the installed metadata
132151
prevents using needless volatile data there.
133152

134153

135-
## with Docker/Podman
154+
### With Docker/Podman
136155

137156

138157
In some situations, Docker may not copy the `.git` into the container when

0 commit comments

Comments
 (0)