@@ -410,6 +410,20 @@ Distributions
410
410
Raises :exc: `PackageNotFoundError ` if the named distribution
411
411
package is not installed in the current Python environment.
412
412
413
+ .. function :: distributions(**kwargs)
414
+
415
+ Get all :class: `Distribution ` instances in the current environment.
416
+
417
+ Any keyword arguments are passed to the ``find_distributions() `` method
418
+ of the registered distribution finders (see :class: `DistributionFinder `).
419
+
420
+ Common keyword arguments include:
421
+
422
+ * ``name ``: Filter to distributions matching this package name
423
+ * ``path ``: Search these path segments (defaults to :data: `sys.path `)
424
+
425
+ :return: An iterable of :class: `Distribution ` instances.
426
+
413
427
.. class :: Distribution
414
428
415
429
Details of an installed distribution package.
@@ -418,6 +432,47 @@ Distributions
418
432
equal, even if they relate to the same installed distribution and
419
433
accordingly have the same attributes.
420
434
435
+ .. classmethod :: discover(*, context=None, **kwargs)
436
+
437
+ Return an iterable of Distribution objects for all packages.
438
+
439
+ Pass a ``context `` or pass keyword arguments for constructing
440
+ a context.
441
+
442
+ :param context: A :class: `DistributionFinder.Context ` object.
443
+ :param kwargs: Context parameters used to construct a new context if one
444
+ is not supplied.
445
+ :return: Iterable of Distribution objects for packages matching
446
+ the context.
447
+
448
+ .. class :: DistributionFinder
449
+
450
+ An abstract base class subclass of :class: `importlib.abc.MetaPathFinder `
451
+ capable of discovering installed distributions.
452
+
453
+ Custom providers should implement this interface to supply metadata
454
+ for distributions that cannot be discovered through the file system
455
+ or other built-in mechanisms.
456
+
457
+ See the section on :ref: `implementing-custom-providers ` for more details.
458
+
459
+ .. class :: DistributionFinder.Context
460
+
461
+ Context object used to provide parameters when discovering distributions.
462
+
463
+ Keyword arguments supplied to :func: `distributions ` or
464
+ :meth: `Distribution.discover ` are stored as attributes in the context
465
+ object and can be used by distribution finders to filter or customize
466
+ their search results.
467
+
468
+ The context provides these attributes:
469
+
470
+ * ``name ``: A package name to match, or ``None `` to match all distributions
471
+ * ``path ``: A list of directories to search (defaults to :data: `sys.path `)
472
+
473
+ Custom distribution finders can accept other keyword parameters through
474
+ this context.
475
+
421
476
While the module level API described above is the most common and convenient usage,
422
477
you can get all of that information from the :class: `!Distribution ` class.
423
478
:class: `!Distribution ` is an abstract object that represents the metadata for
@@ -467,6 +522,8 @@ This metadata finder search defaults to ``sys.path``, but varies slightly in how
467
522
- ``importlib.metadata `` will incidentally honor :py:class: `pathlib.Path ` objects on ``sys.path `` even though such values will be ignored for imports.
468
523
469
524
525
+ .. _implementing-custom-providers :
526
+
470
527
Implementing Custom Providers
471
528
=============================
472
529
0 commit comments