@@ -100,6 +100,13 @@ You can also get a :ref:`distribution's version number <version>`, list its
100
100
:ref: `requirements `.
101
101
102
102
103
+ .. exception :: PackageNotFoundError
104
+
105
+ Subclass of :class: `ModuleNotFoundError ` raised by several functions in this
106
+ module when queried for a distribution package which is not installed in the
107
+ current Python environment.
108
+
109
+
103
110
Functional API
104
111
==============
105
112
@@ -111,31 +118,53 @@ This package provides the following functionality via its public API.
111
118
Entry points
112
119
------------
113
120
114
- The ``entry_points() `` function returns a collection of entry points.
115
- Entry points are represented by ``EntryPoint `` instances;
116
- each ``EntryPoint `` has a ``.name ``, ``.group ``, and ``.value `` attributes and
117
- a ``.load() `` method to resolve the value. There are also ``.module ``,
118
- ``.attr ``, and ``.extras `` attributes for getting the components of the
119
- ``.value `` attribute.
121
+ .. function :: entry_points(**select_params)
122
+
123
+ Returns a :class: `EntryPoints ` instance describing entry points for the
124
+ current environment. Any given keyword parameters are passed to the
125
+ :meth: `!select ` method for comparison to the attributes of
126
+ the individual entry point definitions.
127
+
128
+ Note: it is not currently possible to query for entry points based on
129
+ their :attr: `!EntryPoint.dist ` attribute (as different :class: `!Distribution `
130
+ instances do not currently compare equal, even if they have the same attributes)
131
+
132
+ .. class :: EntryPoints
133
+
134
+ Details of a collection of installed entry points.
135
+
136
+ Also provides a ``.groups `` attribute that reports all identifed entry
137
+ point groups, and a ``.names `` attribute that reports all identified entry
138
+ point names.
139
+
140
+ .. class :: EntryPoint
141
+
142
+ Details of an installed entry point.
143
+
144
+ Each :class: `!EntryPoint ` instance has ``.name ``, ``.group ``, and ``.value ``
145
+ attributes and a ``.load() `` method to resolve the value. There are also
146
+ ``.module ``, ``.attr ``, and ``.extras `` attributes for getting the
147
+ components of the ``.value `` attribute, and ``.dist `` for obtaining
148
+ information regarding the distribution package that provides the entry point.
120
149
121
150
Query all entry points::
122
151
123
152
>>> eps = entry_points() # doctest: +SKIP
124
153
125
- The `` entry_points() `` function returns an `` EntryPoints ` ` object,
126
- a collection of all `` EntryPoint ` ` objects with ``names `` and ``groups ``
154
+ The :func: ` ! entry_points` function returns a :class: ` ! EntryPoints ` object,
155
+ a collection of all :class: ` ! EntryPoint ` objects with ``names `` and ``groups ``
127
156
attributes for convenience::
128
157
129
158
>>> sorted(eps.groups) # doctest: +SKIP
130
159
['console_scripts', 'distutils.commands', 'distutils.setup_keywords', 'egg_info.writers', 'setuptools.installation']
131
160
132
- `` EntryPoints `` has a `` select ` ` method to select entry points
161
+ :class: ` ! EntryPoints ` has a :meth: ` ! select ` method to select entry points
133
162
matching specific properties. Select entry points in the
134
163
``console_scripts `` group::
135
164
136
165
>>> scripts = eps.select(group='console_scripts') # doctest: +SKIP
137
166
138
- Equivalently, since `` entry_points ` ` passes keyword arguments
167
+ Equivalently, since :func: ` ! entry_points ` passes keyword arguments
139
168
through to select::
140
169
141
170
>>> scripts = entry_points(group='console_scripts') # doctest: +SKIP
@@ -189,31 +218,41 @@ for more information on entry points, their definition, and usage.
189
218
Distribution metadata
190
219
---------------------
191
220
192
- Every `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_ includes some metadata,
193
- which you can extract using the
194
- ``metadata() `` function::
221
+ .. function :: metadata(distribution_name)
222
+
223
+ Return the distribution metadata corresponding to the named
224
+ distribution package as a :class: `PackageMetadata ` instance.
225
+
226
+ Raises :exc: `PackageNotFoundError ` if the named distribution
227
+ package is not installed in the current Python environment.
228
+
229
+ .. class :: PackageMetadata
230
+
231
+ A concrete implementation of the
232
+ `PackageMetadata protocol <https://importlib-metadata.readthedocs.io/en/latest/api.html#importlib_metadata.PackageMetadata >`_.
233
+
234
+ In addition to providing the defined protocol methods and attributes, subscripting
235
+ the instance is equivalent to calling the :meth: `!get ` method.
236
+
237
+ Every `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_
238
+ includes some metadata, which you can extract using the :func: `!metadata ` function::
195
239
196
240
>>> wheel_metadata = metadata('wheel') # doctest: +SKIP
197
241
198
- The keys of the returned data structure, a ``PackageMetadata ``,
199
- name the metadata keywords, and
242
+ The keys of the returned data structure name the metadata keywords, and
200
243
the values are returned unparsed from the distribution metadata::
201
244
202
245
>>> wheel_metadata['Requires-Python'] # doctest: +SKIP
203
246
'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
204
247
205
- `` PackageMetadata `` also presents a `` json ` ` attribute that returns
248
+ :class: ` PackageMetadata ` also presents a :attr: ` ! json ` attribute that returns
206
249
all the metadata in a JSON-compatible form per :PEP: `566 `::
207
250
208
251
>>> wheel_metadata.json['requires_python']
209
252
'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
210
253
211
- .. note ::
212
-
213
- The actual type of the object returned by ``metadata() `` is an
214
- implementation detail and should be accessed only through the interface
215
- described by the
216
- `PackageMetadata protocol <https://importlib-metadata.readthedocs.io/en/latest/api.html#importlib_metadata.PackageMetadata >`_.
254
+ The full set of available metadata is not described here.
255
+ See the PyPA `Core metadata specification <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata >`_ for additional details.
217
256
218
257
.. versionchanged :: 3.10
219
258
The ``Description `` is now included in the metadata when presented
@@ -227,7 +266,15 @@ all the metadata in a JSON-compatible form per :PEP:`566`::
227
266
Distribution versions
228
267
---------------------
229
268
230
- The ``version() `` function is the quickest way to get a
269
+ .. function :: version(distribution_name)
270
+
271
+ Return the installed distribution package version for the named
272
+ distribution package.
273
+
274
+ Raises :exc: `PackageNotFoundError ` if the named distribution
275
+ package is not installed in the current Python environment.
276
+
277
+ The :func: `!version ` function is the quickest way to get a
231
278
`Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_'s version
232
279
number, as a string::
233
280
@@ -240,12 +287,28 @@ number, as a string::
240
287
Distribution files
241
288
------------------
242
289
243
- You can also get the full set of files contained within a distribution. The
244
- ``files() `` function takes a `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_ name
245
- and returns all of the
246
- files installed by this distribution. Each file object returned is a
247
- ``PackagePath ``, a :class: `pathlib.PurePath ` derived object with additional ``dist ``,
248
- ``size ``, and ``hash `` properties as indicated by the metadata. For example::
290
+ .. function :: files(distribution_name)
291
+
292
+ Return the full set of files contained within the named
293
+ distribution package.
294
+
295
+ Raises :exc: `PackageNotFoundError ` if the named distribution
296
+ package is not installed in the current Python environment.
297
+
298
+ Returns :const: `None ` if the distribution is found but the installation
299
+ database records reporting the files associated with the distribuion package
300
+ are missing.
301
+
302
+ .. class :: PackagePath
303
+
304
+ A :class: `pathlib.PurePath ` derived object with additional ``dist ``,
305
+ ``size ``, and ``hash `` properties corresponding to the distribution
306
+ package's installation metadata for that file.
307
+
308
+ The :func: `!files ` function takes a
309
+ `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_
310
+ name and returns all of the files installed by this distribution. Each file is reported
311
+ as a :class: `PackagePath ` instance. For example::
249
312
250
313
>>> util = [p for p in files('wheel') if 'util.py' in str(p)][0] # doctest: +SKIP
251
314
>>> util # doctest: +SKIP
@@ -268,16 +331,16 @@ Once you have the file, you can also read its contents::
268
331
return s.encode('utf-8')
269
332
return s
270
333
271
- You can also use the `` locate `` method to get a the absolute path to the
272
- file::
334
+ You can also use the :meth: ` ! locate ` method to get the absolute
335
+ path to the file::
273
336
274
337
>>> util.locate() # doctest: +SKIP
275
338
PosixPath('/home/gustav/example/lib/site-packages/wheel/util.py')
276
339
277
340
In the case where the metadata file listing files
278
- (RECORD or SOURCES.txt) is missing, `` files() ` ` will
279
- return `` None ` `. The caller may wish to wrap calls to
280
- `` files() ` ` in `always_iterable
341
+ (`` RECORD `` or `` SOURCES.txt `` ) is missing, :func: ` ! files ` will
342
+ return :const: ` None `. The caller may wish to wrap calls to
343
+ :func: ` ! files ` in `always_iterable
281
344
<https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_iterable> `_
282
345
or otherwise guard against this condition if the target
283
346
distribution is not known to have the metadata present.
@@ -287,8 +350,16 @@ distribution is not known to have the metadata present.
287
350
Distribution requirements
288
351
-------------------------
289
352
353
+ .. function :: requires(distribution_name)
354
+
355
+ Return the declared dependency specifiers for the named
356
+ distribution package.
357
+
358
+ Raises :exc: `PackageNotFoundError ` if the named distribution
359
+ package is not installed in the current Python environment.
360
+
290
361
To get the full set of requirements for a `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_,
291
- use the `` requires() ` `
362
+ use the :func: ` ! requires `
292
363
function::
293
364
294
365
>>> requires('wheel') # doctest: +SKIP
@@ -301,6 +372,16 @@ function::
301
372
Mapping import to distribution packages
302
373
---------------------------------------
303
374
375
+ .. function :: packages_distributions()
376
+
377
+ Return a mapping from the top level module and import package
378
+ names found via :attr: `sys.meta_path ` to the names of the distribution
379
+ packages (if any) that provide the corresponding files.
380
+
381
+ To allow for namespace packages (which may have members provided by
382
+ multiple distribution packages), each top level import name maps to a
383
+ list of distribution names rather than mapping directly to a single name.
384
+
304
385
A convenience method to resolve the `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_
305
386
name (or names, in the case of a namespace package)
306
387
that provide each importable top-level
@@ -320,23 +401,42 @@ function is not reliable with such installs.
320
401
Distributions
321
402
=============
322
403
323
- While the above API is the most common and convenient usage, you can get all
324
- of that information from the ``Distribution `` class. A ``Distribution `` is an
325
- abstract object that represents the metadata for
326
- a Python `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_. You can
327
- get the ``Distribution `` instance::
404
+ .. function :: distribution(distribution_name)
405
+
406
+ Return a :class: `Distribution ` instance describing the named
407
+ distribution package.
408
+
409
+ Raises :exc: `PackageNotFoundError ` if the named distribution
410
+ package is not installed in the current Python environment.
411
+
412
+ .. class :: Distribution
413
+
414
+ Details of an installed distribution package.
415
+
416
+ Note: different :class: `!Distribution ` instances do not currently compare
417
+ equal, even if they relate to the same installed distribution and
418
+ accordingly have the same attributes.
419
+
420
+ While the module level API described above is the most common and convenient usage,
421
+ you can get all of that information from the :class: `!Distribution ` class.
422
+ :class: `!Distribution ` is an abstract object that represents the metadata for
423
+ a Python `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_.
424
+ You can get the concreate :class: `!Distribution ` subclass instance for an installed
425
+ distribution package by calling the :func: `distribution ` function::
328
426
329
427
>>> from importlib.metadata import distribution # doctest: +SKIP
330
428
>>> dist = distribution('wheel') # doctest: +SKIP
429
+ >>> type(dist) # doctest: +SKIP
430
+ <class 'importlib.metadata.PathDistribution'>
331
431
332
432
Thus, an alternative way to get the version number is through the
333
- `` Distribution ` ` instance::
433
+ :class: ` ! Distribution ` instance::
334
434
335
435
>>> dist.version # doctest: +SKIP
336
436
'0.32.3'
337
437
338
- There are all kinds of additional metadata available on the `` Distribution ` `
339
- instance ::
438
+ There are all kinds of additional metadata available on :class: ` ! Distribution `
439
+ instances ::
340
440
341
441
>>> dist.metadata['Requires-Python'] # doctest: +SKIP
342
442
'>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*'
@@ -350,7 +450,7 @@ metadata::
350
450
'file:///path/to/wheel-0.32.3.editable-py3-none-any.whl'
351
451
352
452
The full set of available metadata is not described here.
353
- See the `Core metadata specifications <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata >`_ for additional details.
453
+ See the PyPA `Core metadata specification <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata >`_ for additional details.
354
454
355
455
.. versionadded :: 3.13
356
456
The ``.origin `` property was added.
0 commit comments