Skip to content

Commit c12f847

Browse files
committed
Move importlib-metadata section before pkg_resources section
1 parent 3c7c07f commit c12f847

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

source/guides/single-sourcing-package-version.rst

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,35 +75,23 @@ number of your project:
7575
:file:`MANIFEST.in`).
7676

7777
#. Set the value in :file:`setup.py`, and have the project code use the
78-
``pkg_resources`` API.
79-
80-
::
81-
82-
import pkg_resources
83-
assert pkg_resources.get_distribution('pip').version == '1.2.0'
84-
85-
Be aware that the ``pkg_resources`` API only knows about what's in the
86-
installation metadata, which is not necessarily the code that's currently
87-
imported.
88-
89-
Note that if the project uses ``pkg_resources`` to fetch its own version at
90-
runtime, then ``setuptools`` (the project that provides ``pkg_resources``)
91-
must be added to the project's ``install_requires`` list.
92-
93-
Example using this technique: `setuptools <https://github.com/pypa/setuptools/blob/master/setuptools/version.py>`_.
94-
95-
A more efficient alternative to ``pkg_resources`` is the
96-
``importlib.metadata`` package introduced in Python 3.8 and available to
97-
older versions as the ``importlib-metadata`` project. An installed
98-
project's version can be fetched with it as follows::
78+
``importlib.metadata`` API to fetch the value at runtime.
79+
(``importlib.metadata`` was introduced in Python 3.8 and is available to
80+
older versions as the ``importlib-metadata`` project.) An installed
81+
project's version can be fetched with the API as follows::
9982

10083
try:
10184
from importlib import metadata
10285
except ImportError:
86+
# Running on pre-3.8 Python; use importlib-metadata package
10387
import importlib_metadata as metadata
10488

10589
assert metadata.version('pip') == '1.2.0'
10690

91+
Be aware that the ``importlib.metadata`` API only knows about what's in the
92+
installation metadata, which is not necessarily the code that's currently
93+
imported.
94+
10795
If a project uses this method to fetch its version at runtime, then its
10896
``install_requires`` value needs to be edited to install
10997
``importlib-metadata`` on pre-3.8 versions of Python like so::
@@ -118,6 +106,18 @@ number of your project:
118106
...
119107
)
120108

109+
An older (and less efficient) alternative to ``importlib.metadata`` is the
110+
``pkg_resources`` API provided by ``setuptools``::
111+
112+
import pkg_resources
113+
assert pkg_resources.get_distribution('pip').version == '1.2.0'
114+
115+
If a project uses ``pkg_resources`` to fetch its own version at runtime,
116+
then ``setuptools`` must be added to the project's ``install_requires``
117+
list.
118+
119+
Example using this technique: `setuptools <https://github.com/pypa/setuptools/blob/master/setuptools/version.py>`_.
120+
121121

122122
#. Set the value to ``__version__`` in ``sample/__init__.py`` and import
123123
``sample`` in :file:`setup.py`.

0 commit comments

Comments
 (0)