@@ -15,6 +15,7 @@ Unwanted files must be excluded by discarding them via ``MANIFEST.in``.
1515.. image :: https://tidelift.com/badges/package/pypi/setuptools-scm
1616 :target: https://tidelift.com/subscription/pkg/pypi-setuptools-scm?utm_source=pypi-setuptools-scm&utm_medium=readme
1717
18+
1819``pyproject.toml `` usage
1920------------------------
2021
@@ -50,7 +51,7 @@ To enable version inference, add this section to your pyproject.toml:
5051.. code :: toml
5152
5253 # pyproject.toml
53- [tools .setuptools_scm]
54+ [tool .setuptools_scm]
5455
5556 Including this section is comparable to supplying
5657``use_scm_version=True `` in ``setup.py ``. Additionally,
@@ -60,7 +61,8 @@ to be supplied to ``get_version()``. For example:
6061.. code :: toml
6162
6263 # pyproject.toml
63- [tools.setuptools_scm]
64+
65+ [tool.setuptools_scm]
6466 write_to = "pkg/version.py"
6567
6668
@@ -103,20 +105,7 @@ Arguments to ``get_version()`` (see below) may be passed as a dictionary to
103105 ... ,
104106 )
105107
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 ``:
120109
121110.. code-block :: shell
122111
@@ -129,8 +118,8 @@ You can also confirm the version number locally via ``setup.py``:
129118 not defined in ``setup.cfg ``.
130119
131120
132- ``setup.cfg ``
133- -------------
121+ ``setup.cfg `` usage
122+ -------------------
134123
135124If using `setuptools 30.3.0
136125<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:
189178 See `setup.py Usage `_ above for how to use this within ``setup.py ``.
190179
191180
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+
192218Usage from Sphinx
193219-----------------
194220
0 commit comments