@@ -15,6 +15,7 @@ Unwanted files must be excluded by discarding them via ``MANIFEST.in``.
15
15
.. image :: https://tidelift.com/badges/package/pypi/setuptools-scm
16
16
:target: https://tidelift.com/subscription/pkg/pypi-setuptools-scm?utm_source=pypi-setuptools-scm&utm_medium=readme
17
17
18
+
18
19
``pyproject.toml `` usage
19
20
------------------------
20
21
@@ -50,7 +51,7 @@ To enable version inference, add this section to your pyproject.toml:
50
51
.. code :: toml
51
52
52
53
# pyproject.toml
53
- [tools .setuptools_scm]
54
+ [tool .setuptools_scm]
54
55
55
56
Including this section is comparable to supplying
56
57
``use_scm_version=True `` in ``setup.py ``. Additionally,
@@ -60,7 +61,8 @@ to be supplied to ``get_version()``. For example:
60
61
.. code :: toml
61
62
62
63
# pyproject.toml
63
- [tools.setuptools_scm]
64
+
65
+ [tool.setuptools_scm]
64
66
write_to = "pkg/version.py"
65
67
66
68
@@ -103,20 +105,7 @@ Arguments to ``get_version()`` (see below) may be passed as a dictionary to
103
105
... ,
104
106
)
105
107
106
- Once configured, you can access the version number in your package via
107
- ``pkg_resources `` (`PEP-0396 <https://www.python.org/dev/peps/pep-0396 >`_). For
108
- example:
109
-
110
- .. code :: python
111
-
112
- from pkg_resources import get_distribution, DistributionNotFound
113
- try :
114
- __version__ = get_distribution(__name__ ).version
115
- except DistributionNotFound:
116
- # package is not installed
117
- pass
118
-
119
- You can also confirm the version number locally via ``setup.py ``:
108
+ You can confirm the version number locally via ``setup.py ``:
120
109
121
110
.. code-block :: shell
122
111
@@ -129,8 +118,8 @@ You can also confirm the version number locally via ``setup.py``:
129
118
not defined in ``setup.cfg ``.
130
119
131
120
132
- ``setup.cfg ``
133
- -------------
121
+ ``setup.cfg `` usage
122
+ -------------------
134
123
135
124
If using `setuptools 30.3.0
136
125
<https://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files> `_
@@ -189,6 +178,43 @@ than the project's root, you can use:
189
178
See `setup.py Usage `_ above for how to use this within ``setup.py ``.
190
179
191
180
181
+ Retrieving package version at runtime
182
+ -------------------------------------
183
+
184
+ If you have opted not to hardcode the version number inside the package,
185
+ you can retrieve it at runtime from PEP-0566 _ metadata using
186
+ ``importlib.metadata `` from the standard library
187
+ or the `importlib_metadata `_ backport:
188
+
189
+ .. code :: python
190
+
191
+ from importlib.metadata import version, PackageNotFoundError
192
+
193
+ try :
194
+ __version__ = version(__name__ )
195
+ except PackageNotFoundError:
196
+ # package is not installed
197
+ pass
198
+
199
+ Alternatively, you can use ``pkg_resources `` which is included in
200
+ ``setuptools ``:
201
+
202
+ .. code :: python
203
+
204
+ from pkg_resources import get_distribution, DistributionNotFound
205
+
206
+ try :
207
+ __version__ = get_distribution(__name__ ).version
208
+ except DistributionNotFound:
209
+ # package is not installed
210
+ pass
211
+
212
+ This does place a runtime dependency on ``setuptools ``.
213
+
214
+ .. _PEP-0566 : https://www.python.org/dev/peps/pep-0566/
215
+ .. _importlib_metadata : https://pypi.org/project/importlib-metadata/
216
+
217
+
192
218
Usage from Sphinx
193
219
-----------------
194
220
0 commit comments