Skip to content

Commit d9bc1ad

Browse files
Merge pull request #387 from layday/mention-importlib-metadata
Mention importlib.metadata as alternative to pkg_resources
2 parents fbaeaa9 + 147857c commit d9bc1ad

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

README.rst

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -103,20 +104,7 @@ Arguments to ``get_version()`` (see below) may be passed as a dictionary to
103104
...,
104105
)
105106
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``:
120108

121109
.. code-block:: shell
122110
@@ -129,8 +117,8 @@ You can also confirm the version number locally via ``setup.py``:
129117
not defined in ``setup.cfg``.
130118

131119

132-
``setup.cfg``
133-
-------------
120+
``setup.cfg`` usage
121+
-------------------
134122

135123
If using `setuptools 30.3.0
136124
<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:
189177
See `setup.py Usage`_ above for how to use this within ``setup.py``.
190178

191179

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+
192217
Usage from Sphinx
193218
-----------------
194219

0 commit comments

Comments
 (0)