@@ -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
@@ -103,20 +104,7 @@ Arguments to ``get_version()`` (see below) may be passed as a dictionary to
103
104
... ,
104
105
)
105
106
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 ``:
107
+ You can confirm the version number locally via ``setup.py ``:
120
108
121
109
.. code-block :: shell
122
110
@@ -129,8 +117,8 @@ You can also confirm the version number locally via ``setup.py``:
129
117
not defined in ``setup.cfg ``.
130
118
131
119
132
- ``setup.cfg ``
133
- -------------
120
+ ``setup.cfg `` usage
121
+ -------------------
134
122
135
123
If using `setuptools 30.3.0
136
124
<https://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files> `_
@@ -189,6 +177,43 @@ than the project's root, you can use:
189
177
See `setup.py Usage `_ above for how to use this within ``setup.py ``.
190
178
191
179
180
+ Retrieving package version at runtime
181
+ -------------------------------------
182
+
183
+ If you have opted not to hardcode the version number inside the package,
184
+ you can retrieve it at runtime from PEP-0566 _ metadata using
185
+ ``importlib.metadata `` from the standard library
186
+ or the `importlib_metadata `_ backport:
187
+
188
+ .. code :: python
189
+
190
+ from importlib.metadata import version, PackageNotFoundError
191
+
192
+ try :
193
+ __version__ = version(__name__ )
194
+ except PackageNotFoundError:
195
+ # package is not installed
196
+ pass
197
+
198
+ Alternatively, you can use ``pkg_resources `` which is included in
199
+ ``setuptools ``:
200
+
201
+ .. code :: python
202
+
203
+ from pkg_resources import get_distribution, DistributionNotFound
204
+
205
+ try :
206
+ __version__ = get_distribution(__name__ ).version
207
+ except DistributionNotFound:
208
+ # package is not installed
209
+ pass
210
+
211
+ This does place a runtime dependency on ``setuptools ``.
212
+
213
+ .. _PEP-0566 : https://www.python.org/dev/peps/pep-0566/
214
+ .. _importlib_metadata : https://pypi.org/project/importlib-metadata/
215
+
216
+
192
217
Usage from Sphinx
193
218
-----------------
194
219
0 commit comments