1
1
# Usage
2
2
3
- ## at build time
3
+ ## At build time
4
4
5
5
The preferred way to configure ` setuptools-scm ` is to author
6
6
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
25
25
[ pep517] ( https://pypi.org/project/pep517/ ) ).
26
26
Tools that still invoke ` setup.py ` must ensure build requirements are installed
27
27
28
- ### version files
28
+ ### Version files
29
+
30
+ Version files can be created with the `` version_file `` directive.
29
31
30
32
``` toml title="pyproject.toml"
31
33
...
@@ -34,15 +36,13 @@ version_file = "pkg/_version.py"
34
36
```
35
37
Where `` pkg `` is the name of your package.
36
38
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.
37
44
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
46
46
47
47
If you need to confirm which version string is being generated
48
48
or debug the configuration, you can install
@@ -65,46 +65,31 @@ $ python -m setuptools_scm ls # output trimmed for brevity
65
65
...
66
66
```
67
67
68
- !!! note "committed files only"
68
+ !!! note "Committed files only"
69
69
70
70
currently only committed files are listed, this might change in the future
71
71
72
72
!!! warning "sdists/archives don't provide file lists"
73
73
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.
98
77
78
+ To explore other options, try
99
79
80
+ ``` commandline
81
+ $ python -m setuptools_scm --help
100
82
83
+ ## At runtime
101
84
102
- ### version at runtime
85
+ ### Python Metadata
103
86
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
106
89
``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:
108
93
109
94
```python title="package_name/__init__.py"
110
95
from importlib.metadata import version, PackageNotFoundError
@@ -116,6 +101,40 @@ except PackageNotFoundError:
116
101
pass
117
102
```
118
103
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
+ ```
119
138
120
139
### Usage from Sphinx
121
140
@@ -132,7 +151,7 @@ the working directory for good reasons and using the installed metadata
132
151
prevents using needless volatile data there.
133
152
134
153
135
- ## with Docker/Podman
154
+ ### With Docker/Podman
136
155
137
156
138
157
In some situations, Docker may not copy the ` .git ` into the container when
0 commit comments