File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -86,8 +86,38 @@ number of your project:
8686 installation metadata, which is not necessarily the code that's currently
8787 imported.
8888
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+
8993 Example using this technique: `setuptools <https://github.com/pypa/setuptools/blob/master/setuptools/version.py >`_.
9094
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::
99+
100+ try:
101+ from importlib import metadata
102+ except ImportError:
103+ import importlib_metadata as metadata
104+
105+ assert metadata.version('pip') == '1.2.0'
106+
107+ If a project uses this method to fetch its version at runtime, then its
108+ ``install_requires `` value needs to be edited to install
109+ ``importlib-metadata `` on pre-3.8 versions of Python like so::
110+
111+ setup(
112+ ...
113+ install_requires=[
114+ ...
115+ 'importlib-metadata ~= 1.0 ; python_version < "3.8"',
116+ ...
117+ ],
118+ ...
119+ )
120+
91121
92122#. Set the value to ``__version__ `` in ``sample/__init__.py `` and import
93123 ``sample `` in :file: `setup.py `.
You can’t perform that action at this time.
0 commit comments