@@ -178,42 +178,47 @@ Accessing version information at runtime
178178
179179Version information for all :term: `distribution packages <Distribution Package> `
180180that are locally available in the current environment can be obtained at runtime
181- using the standard library's `` importlib.metadata.version ` ` function::
181+ using the standard library's :func: ` importlib.metadata.version ` function::
182182
183183 >>> importlib.metadata.version("pip")
184184 '23.3.2'
185185
186- Many libraries also choose to version their top level
186+ Many projects also choose to version their top level
187187:term: `import packages <Import Package> ` by providing a package level
188188``__version__ `` attribute::
189189
190- >>> import pip
191- >>> pip .__version__
192- '23.3.2 '
190+ >>> import cryptography
191+ >>> cryptography .__version__
192+ '41.0.7 '
193193
194- Import packages are *not * required to be versioned independently of their
195- distribution package version information (see the rejected proposal in
196- :pep: `PEP 396 <396 >`), so this approach to retrieving runtime version
197- information should only be used with libraries that are known to provide it.
194+ This technique can be particularly valuable for CLI applications which want
195+ to ensure that version query invocations (such as ``pip -V ``) run as quickly
196+ as possible.
198197
199- Library publishers wishing to ensure their reported distribution package and
198+ Package publishers wishing to ensure their reported distribution package and
200199import package versions are consistent with each other can review the
201200:ref: `single-source-version ` discussion for potential approaches to doing so.
202201
203- Some libraries may need to publish version information for external APIs
202+ However, as import packages and modules are not *required * to publish runtime
203+ version information in this way (see the rejected proposal in
204+ :pep: `PEP 396 <396 >`), the ``__version__ `` attribute should either only be
205+ queried with interfaces that are known to provide it, or else the querying
206+ code should be designed to handle the case where the attribute is missing
207+ [#fallback-to-dist-version ]_.
208+
209+ Some projects may need to publish version information for external APIs
204210that don't meet the requirements for Python distribution package
205- :ref: `version specifiers <version-specifiers >`. Such libraries should
206- define their own library -specific ways of obtaining the relevant information
211+ :ref: `version specifiers <version-specifiers >`. Such projects should
212+ define their own project -specific ways of obtaining the relevant information
207213at runtime. For example, the standard library's :mod: `ssl ` module offers
208214multiple ways to access the underlying OpenSSL library version::
209215
210216 >>> ssl.OPENSSL_VERSION
211217 'OpenSSL 3.2.2 4 Jun 2024'
212218 >>> ssl.OPENSSL_VERSION_INFO
213219 (3, 2, 0, 2, 0)
214- >>> ssl.OPENSSL_VERSION_NUMBER
215- 807403552
216-
220+ >>> hex(ssl.OPENSSL_VERSION_NUMBER)
221+ '0x30200020'
217222
218223--------------------------------------------------------------------------------
219224
@@ -226,6 +231,15 @@ multiple ways to access the underlying OpenSSL library version::
226231 Brett Cannon <semver-brett-cannon_> `_. For a humoristic take, read about
227232 ZeroVer _.
228233
234+ .. [#fallback-to-dist-version ] A full list mapping the top level names available
235+ for import to the distribution packages that provide those import packages and
236+ modules may be obtained through the standard library's
237+ :func: `importlib.metadata.packages_distributions ` function. This means that
238+ even code that is attempting to infer a version to report for all importable
239+ top-level names has a means to fall back to reporting the distribution
240+ version information if no ``__version__ `` attribute is defined. Only standard
241+ library modules, and modules added via means other than Python package
242+ installation would fail to have version information reported in that case.
229243
230244
231245 .. _zerover : https://0ver.org
0 commit comments