1============================================================= 2Package Discovery and Resource Access using ``pkg_resources`` 3============================================================= 4 5The ``pkg_resources`` module distributed with ``setuptools`` provides an API 6for Python libraries to access their resource files, and for extensible 7applications and frameworks to automatically discover plugins. It also 8provides runtime support for using C extensions that are inside zipfile-format 9eggs, support for merging packages that have separately-distributed modules or 10subpackages, and APIs for managing Python's current "working set" of active 11packages. 12 13Use of ``pkg_resources`` is discouraged in favor of 14`importlib.resources <https://docs.python.org/3/library/importlib.html#module-importlib.resources>`_, 15`importlib.metadata <https://docs.python.org/3/library/importlib.metadata.html>`_, 16and their backports (:pypi:`importlib_resources`, 17:pypi:`importlib_metadata`). 18Please consider using those libraries instead of pkg_resources. 19 20 21-------- 22Overview 23-------- 24 25The ``pkg_resources`` module provides runtime facilities for finding, 26introspecting, activating and using installed Python distributions. Some 27of the more advanced features (notably the support for parallel installation 28of multiple versions) rely specifically on the "egg" format (either as a 29zip archive or subdirectory), while others (such as plugin discovery) will 30work correctly so long as "egg-info" metadata directories are available for 31relevant distributions. 32 33Eggs are a distribution format for Python modules, similar in concept to 34Java's "jars" or Ruby's "gems", or the "wheel" format defined in PEP 427. 35However, unlike a pure distribution format, eggs can also be installed and 36added directly to ``sys.path`` as an import location. When installed in 37this way, eggs are *discoverable*, meaning that they carry metadata that 38unambiguously identifies their contents and dependencies. This means that 39an installed egg can be *automatically* found and added to ``sys.path`` in 40response to simple requests of the form, "get me everything I need to use 41docutils' PDF support". This feature allows mutually conflicting versions of 42a distribution to co-exist in the same Python installation, with individual 43applications activating the desired version at runtime by manipulating the 44contents of ``sys.path`` (this differs from the virtual environment 45approach, which involves creating isolated environments for each 46application). 47 48The following terms are needed in order to explain the capabilities offered 49by this module: 50 51project 52 A library, framework, script, plugin, application, or collection of data 53 or other resources, or some combination thereof. Projects are assumed to 54 have "relatively unique" names, e.g. names registered with PyPI. 55 56release 57 A snapshot of a project at a particular point in time, denoted by a version 58 identifier. 59 60distribution 61 A file or files that represent a particular release. 62 63importable distribution 64 A file or directory that, if placed on ``sys.path``, allows Python to 65 import any modules contained within it. 66 67pluggable distribution 68 An importable distribution whose filename unambiguously identifies its 69 release (i.e. project and version), and whose contents unambiguously 70 specify what releases of other projects will satisfy its runtime 71 requirements. 72 73extra 74 An "extra" is an optional feature of a release, that may impose additional 75 runtime requirements. For example, if docutils PDF support required a 76 PDF support library to be present, docutils could define its PDF support as 77 an "extra", and list what other project releases need to be available in 78 order to provide it. 79 80environment 81 A collection of distributions potentially available for importing, but not 82 necessarily active. More than one distribution (i.e. release version) for 83 a given project may be present in an environment. 84 85working set 86 A collection of distributions actually available for importing, as on 87 ``sys.path``. At most one distribution (release version) of a given 88 project may be present in a working set, as otherwise there would be 89 ambiguity as to what to import. 90 91eggs 92 Eggs are pluggable distributions in one of the three formats currently 93 supported by ``pkg_resources``. There are built eggs, development eggs, 94 and egg links. Built eggs are directories or zipfiles whose name ends 95 with ``.egg`` and follows the egg naming conventions, and contain an 96 ``EGG-INFO`` subdirectory (zipped or otherwise). Development eggs are 97 normal directories of Python code with one or more ``ProjectName.egg-info`` 98 subdirectories. The development egg format is also used to provide a 99 default version of a distribution that is available to software that 100 doesn't use ``pkg_resources`` to request specific versions. Egg links 101 are ``*.egg-link`` files that contain the name of a built or 102 development egg, to support symbolic linking on platforms that do not 103 have native symbolic links (or where the symbolic link support is 104 limited). 105 106(For more information about these terms and concepts, see also this 107`architectural overview`_ of ``pkg_resources`` and Python Eggs in general.) 108 109.. _architectural overview: http://mail.python.org/pipermail/distutils-sig/2005-June/004652.html 110 111 112.. ----------------- 113.. Developer's Guide 114.. ----------------- 115 116.. This section isn't written yet. Currently planned topics include 117 Accessing Resources 118 Finding and Activating Package Distributions 119 get_provider() 120 require() 121 WorkingSet 122 iter_distributions 123 Running Scripts 124 Configuration 125 Namespace Packages 126 Extensible Applications and Frameworks 127 Locating entry points 128 Activation listeners 129 Metadata access 130 Extended Discovery and Installation 131 Supporting Custom PEP 302 Implementations 132.. For now, please check out the extensive `API Reference`_ below. 133 134 135------------- 136API Reference 137------------- 138 139Namespace Package Support 140========================= 141 142A namespace package is a package that only contains other packages and modules, 143with no direct contents of its own. Such packages can be split across 144multiple, separately-packaged distributions. They are normally used to split 145up large packages produced by a single organization, such as in the ``zope`` 146namespace package for Zope Corporation packages, and the ``peak`` namespace 147package for the Python Enterprise Application Kit. 148 149To create a namespace package, you list it in the ``namespace_packages`` 150argument to ``setup()``, in your project's ``setup.py``. (See the 151:ref:`setuptools documentation on namespace packages <Namespace Packages>` for 152more information on this.) Also, you must add a ``declare_namespace()`` call 153in the package's ``__init__.py`` file(s): 154 155``declare_namespace(name)`` 156 Declare that the dotted package name ``name`` is a "namespace package" whose 157 contained packages and modules may be spread across multiple distributions. 158 The named package's ``__path__`` will be extended to include the 159 corresponding package in all distributions on ``sys.path`` that contain a 160 package of that name. (More precisely, if an importer's 161 ``find_module(name)`` returns a loader, then it will also be searched for 162 the package's contents.) Whenever a Distribution's ``activate()`` method 163 is invoked, it checks for the presence of namespace packages and updates 164 their ``__path__`` contents accordingly. 165 166Applications that manipulate namespace packages or directly alter ``sys.path`` 167at runtime may also need to use this API function: 168 169``fixup_namespace_packages(path_item)`` 170 Declare that ``path_item`` is a newly added item on ``sys.path`` that may 171 need to be used to update existing namespace packages. Ordinarily, this is 172 called for you when an egg is automatically added to ``sys.path``, but if 173 your application modifies ``sys.path`` to include locations that may 174 contain portions of a namespace package, you will need to call this 175 function to ensure they are added to the existing namespace packages. 176 177Although by default ``pkg_resources`` only supports namespace packages for 178filesystem and zip importers, you can extend its support to other "importers" 179compatible with PEP 302 using the ``register_namespace_handler()`` function. 180See the section below on `Supporting Custom Importers`_ for details. 181 182 183``WorkingSet`` Objects 184====================== 185 186The ``WorkingSet`` class provides access to a collection of "active" 187distributions. In general, there is only one meaningful ``WorkingSet`` 188instance: the one that represents the distributions that are currently active 189on ``sys.path``. This global instance is available under the name 190``working_set`` in the ``pkg_resources`` module. However, specialized 191tools may wish to manipulate working sets that don't correspond to 192``sys.path``, and therefore may wish to create other ``WorkingSet`` instances. 193 194It's important to note that the global ``working_set`` object is initialized 195from ``sys.path`` when ``pkg_resources`` is first imported, but is only updated 196if you do all future ``sys.path`` manipulation via ``pkg_resources`` APIs. If 197you manually modify ``sys.path``, you must invoke the appropriate methods on 198the ``working_set`` instance to keep it in sync. Unfortunately, Python does 199not provide any way to detect arbitrary changes to a list object like 200``sys.path``, so ``pkg_resources`` cannot automatically update the 201``working_set`` based on changes to ``sys.path``. 202 203``WorkingSet(entries=None)`` 204 Create a ``WorkingSet`` from an iterable of path entries. If ``entries`` 205 is not supplied, it defaults to the value of ``sys.path`` at the time 206 the constructor is called. 207 208 Note that you will not normally construct ``WorkingSet`` instances 209 yourself, but instead you will implicitly or explicitly use the global 210 ``working_set`` instance. For the most part, the ``pkg_resources`` API 211 is designed so that the ``working_set`` is used by default, such that you 212 don't have to explicitly refer to it most of the time. 213 214All distributions available directly on ``sys.path`` will be activated 215automatically when ``pkg_resources`` is imported. This behaviour can cause 216version conflicts for applications which require non-default versions of 217those distributions. To handle this situation, ``pkg_resources`` checks for a 218``__requires__`` attribute in the ``__main__`` module when initializing the 219default working set, and uses this to ensure a suitable version of each 220affected distribution is activated. For example:: 221 222 __requires__ = ["CherryPy < 3"] # Must be set before pkg_resources import 223 import pkg_resources 224 225 226Basic ``WorkingSet`` Methods 227---------------------------- 228 229The following methods of ``WorkingSet`` objects are also available as module- 230level functions in ``pkg_resources`` that apply to the default ``working_set`` 231instance. Thus, you can use e.g. ``pkg_resources.require()`` as an 232abbreviation for ``pkg_resources.working_set.require()``: 233 234 235``require(*requirements)`` 236 Ensure that distributions matching ``requirements`` are activated 237 238 ``requirements`` must be a string or a (possibly-nested) sequence 239 thereof, specifying the distributions and versions required. The 240 return value is a sequence of the distributions that needed to be 241 activated to fulfill the requirements; all relevant distributions are 242 included, even if they were already activated in this working set. 243 244 For the syntax of requirement specifiers, see the section below on 245 `Requirements Parsing`_. 246 247 In general, it should not be necessary for you to call this method 248 directly. It's intended more for use in quick-and-dirty scripting and 249 interactive interpreter hacking than for production use. If you're creating 250 an actual library or application, it's strongly recommended that you create 251 a "setup.py" script using ``setuptools``, and declare all your requirements 252 there. That way, tools like pip can automatically detect what requirements 253 your package has, and deal with them accordingly. 254 255 Note that calling ``require('SomePackage')`` will not install 256 ``SomePackage`` if it isn't already present. If you need to do this, you 257 should use the ``resolve()`` method instead, which allows you to pass an 258 ``installer`` callback that will be invoked when a needed distribution 259 can't be found on the local machine. You can then have this callback 260 display a dialog, automatically download the needed distribution, or 261 whatever else is appropriate for your application. See the documentation 262 below on the ``resolve()`` method for more information, and also on the 263 ``obtain()`` method of ``Environment`` objects. 264 265``run_script(requires, script_name)`` 266 Locate distribution specified by ``requires`` and run its ``script_name`` 267 script. ``requires`` must be a string containing a requirement specifier. 268 (See `Requirements Parsing`_ below for the syntax.) 269 270 The script, if found, will be executed in *the caller's globals*. That's 271 because this method is intended to be called from wrapper scripts that 272 act as a proxy for the "real" scripts in a distribution. A wrapper script 273 usually doesn't need to do anything but invoke this function with the 274 correct arguments. 275 276 If you need more control over the script execution environment, you 277 probably want to use the ``run_script()`` method of a ``Distribution`` 278 object's `Metadata API`_ instead. 279 280``iter_entry_points(group, name=None)`` 281 Yield entry point objects from ``group`` matching ``name`` 282 283 If ``name`` is None, yields all entry points in ``group`` from all 284 distributions in the working set, otherwise only ones matching both 285 ``group`` and ``name`` are yielded. Entry points are yielded from the active 286 distributions in the order that the distributions appear in the working 287 set. (For the global ``working_set``, this should be the same as the order 288 that they are listed in ``sys.path``.) Note that within the entry points 289 advertised by an individual distribution, there is no particular ordering. 290 291 Please see the section below on `Entry Points`_ for more information. 292 293 294``WorkingSet`` Methods and Attributes 295------------------------------------- 296 297These methods are used to query or manipulate the contents of a specific 298working set, so they must be explicitly invoked on a particular ``WorkingSet`` 299instance: 300 301``add_entry(entry)`` 302 Add a path item to the ``entries``, finding any distributions on it. You 303 should use this when you add additional items to ``sys.path`` and you want 304 the global ``working_set`` to reflect the change. This method is also 305 called by the ``WorkingSet()`` constructor during initialization. 306 307 This method uses ``find_distributions(entry,True)`` to find distributions 308 corresponding to the path entry, and then ``add()`` them. ``entry`` is 309 always appended to the ``entries`` attribute, even if it is already 310 present, however. (This is because ``sys.path`` can contain the same value 311 more than once, and the ``entries`` attribute should be able to reflect 312 this.) 313 314``__contains__(dist)`` 315 True if ``dist`` is active in this ``WorkingSet``. Note that only one 316 distribution for a given project can be active in a given ``WorkingSet``. 317 318``__iter__()`` 319 Yield distributions for non-duplicate projects in the working set. 320 The yield order is the order in which the items' path entries were 321 added to the working set. 322 323``find(req)`` 324 Find a distribution matching ``req`` (a ``Requirement`` instance). 325 If there is an active distribution for the requested project, this 326 returns it, as long as it meets the version requirement specified by 327 ``req``. But, if there is an active distribution for the project and it 328 does *not* meet the ``req`` requirement, ``VersionConflict`` is raised. 329 If there is no active distribution for the requested project, ``None`` 330 is returned. 331 332``resolve(requirements, env=None, installer=None)`` 333 List all distributions needed to (recursively) meet ``requirements`` 334 335 ``requirements`` must be a sequence of ``Requirement`` objects. ``env``, 336 if supplied, should be an ``Environment`` instance. If 337 not supplied, an ``Environment`` is created from the working set's 338 ``entries``. ``installer``, if supplied, will be invoked with each 339 requirement that cannot be met by an already-installed distribution; it 340 should return a ``Distribution`` or ``None``. (See the ``obtain()`` method 341 of `Environment Objects`_, below, for more information on the ``installer`` 342 argument.) 343 344``add(dist, entry=None)`` 345 Add ``dist`` to working set, associated with ``entry`` 346 347 If ``entry`` is unspecified, it defaults to ``dist.location``. On exit from 348 this routine, ``entry`` is added to the end of the working set's ``.entries`` 349 (if it wasn't already present). 350 351 ``dist`` is only added to the working set if it's for a project that 352 doesn't already have a distribution active in the set. If it's 353 successfully added, any callbacks registered with the ``subscribe()`` 354 method will be called. (See `Receiving Change Notifications`_, below.) 355 356 Note: ``add()`` is automatically called for you by the ``require()`` 357 method, so you don't normally need to use this method directly. 358 359``entries`` 360 This attribute represents a "shadow" ``sys.path``, primarily useful for 361 debugging. If you are experiencing import problems, you should check 362 the global ``working_set`` object's ``entries`` against ``sys.path``, to 363 ensure that they match. If they do not, then some part of your program 364 is manipulating ``sys.path`` without updating the ``working_set`` 365 accordingly. IMPORTANT NOTE: do not directly manipulate this attribute! 366 Setting it equal to ``sys.path`` will not fix your problem, any more than 367 putting black tape over an "engine warning" light will fix your car! If 368 this attribute is out of sync with ``sys.path``, it's merely an *indicator* 369 of the problem, not the cause of it. 370 371 372Receiving Change Notifications 373------------------------------ 374 375Extensible applications and frameworks may need to receive notification when 376a new distribution (such as a plug-in component) has been added to a working 377set. This is what the ``subscribe()`` method and ``add_activation_listener()`` 378function are for. 379 380``subscribe(callback)`` 381 Invoke ``callback(distribution)`` once for each active distribution that is 382 in the set now, or gets added later. Because the callback is invoked for 383 already-active distributions, you do not need to loop over the working set 384 yourself to deal with the existing items; just register the callback and 385 be prepared for the fact that it will be called immediately by this method. 386 387 Note that callbacks *must not* allow exceptions to propagate, or they will 388 interfere with the operation of other callbacks and possibly result in an 389 inconsistent working set state. Callbacks should use a try/except block 390 to ignore, log, or otherwise process any errors, especially since the code 391 that caused the callback to be invoked is unlikely to be able to handle 392 the errors any better than the callback itself. 393 394``pkg_resources.add_activation_listener()`` is an alternate spelling of 395``pkg_resources.working_set.subscribe()``. 396 397 398Locating Plugins 399---------------- 400 401Extensible applications will sometimes have a "plugin directory" or a set of 402plugin directories, from which they want to load entry points or other 403metadata. The ``find_plugins()`` method allows you to do this, by scanning an 404environment for the newest version of each project that can be safely loaded 405without conflicts or missing requirements. 406 407``find_plugins(plugin_env, full_env=None, fallback=True)`` 408 Scan ``plugin_env`` and identify which distributions could be added to this 409 working set without version conflicts or missing requirements. 410 411 Example usage:: 412 413 distributions, errors = working_set.find_plugins( 414 Environment(plugin_dirlist) 415 ) 416 map(working_set.add, distributions) # add plugins+libs to sys.path 417 print "Couldn't load", errors # display errors 418 419 The ``plugin_env`` should be an ``Environment`` instance that contains only 420 distributions that are in the project's "plugin directory" or directories. 421 The ``full_env``, if supplied, should be an ``Environment`` instance that 422 contains all currently-available distributions. 423 424 If ``full_env`` is not supplied, one is created automatically from the 425 ``WorkingSet`` this method is called on, which will typically mean that 426 every directory on ``sys.path`` will be scanned for distributions. 427 428 This method returns a 2-tuple: (``distributions``, ``error_info``), where 429 ``distributions`` is a list of the distributions found in ``plugin_env`` that 430 were loadable, along with any other distributions that are needed to resolve 431 their dependencies. ``error_info`` is a dictionary mapping unloadable plugin 432 distributions to an exception instance describing the error that occurred. 433 Usually this will be a ``DistributionNotFound`` or ``VersionConflict`` 434 instance. 435 436 Most applications will use this method mainly on the master ``working_set`` 437 instance in ``pkg_resources``, and then immediately add the returned 438 distributions to the working set so that they are available on sys.path. 439 This will make it possible to find any entry points, and allow any other 440 metadata tracking and hooks to be activated. 441 442 The resolution algorithm used by ``find_plugins()`` is as follows. First, 443 the project names of the distributions present in ``plugin_env`` are sorted. 444 Then, each project's eggs are tried in descending version order (i.e., 445 newest version first). 446 447 An attempt is made to resolve each egg's dependencies. If the attempt is 448 successful, the egg and its dependencies are added to the output list and to 449 a temporary copy of the working set. The resolution process continues with 450 the next project name, and no older eggs for that project are tried. 451 452 If the resolution attempt fails, however, the error is added to the error 453 dictionary. If the ``fallback`` flag is true, the next older version of the 454 plugin is tried, until a working version is found. If false, the resolution 455 process continues with the next plugin project name. 456 457 Some applications may have stricter fallback requirements than others. For 458 example, an application that has a database schema or persistent objects 459 may not be able to safely downgrade a version of a package. Others may want 460 to ensure that a new plugin configuration is either 100% good or else 461 revert to a known-good configuration. (That is, they may wish to revert to 462 a known configuration if the ``error_info`` return value is non-empty.) 463 464 Note that this algorithm gives precedence to satisfying the dependencies of 465 alphabetically prior project names in case of version conflicts. If two 466 projects named "AaronsPlugin" and "ZekesPlugin" both need different versions 467 of "TomsLibrary", then "AaronsPlugin" will win and "ZekesPlugin" will be 468 disabled due to version conflict. 469 470 471``Environment`` Objects 472======================= 473 474An "environment" is a collection of ``Distribution`` objects, usually ones 475that are present and potentially importable on the current platform. 476``Environment`` objects are used by ``pkg_resources`` to index available 477distributions during dependency resolution. 478 479``Environment(search_path=None, platform=get_supported_platform(), python=PY_MAJOR)`` 480 Create an environment snapshot by scanning ``search_path`` for distributions 481 compatible with ``platform`` and ``python``. ``search_path`` should be a 482 sequence of strings such as might be used on ``sys.path``. If a 483 ``search_path`` isn't supplied, ``sys.path`` is used. 484 485 ``platform`` is an optional string specifying the name of the platform 486 that platform-specific distributions must be compatible with. If 487 unspecified, it defaults to the current platform. ``python`` is an 488 optional string naming the desired version of Python (e.g. ``'2.4'``); 489 it defaults to the currently-running version. 490 491 You may explicitly set ``platform`` (and/or ``python``) to ``None`` if you 492 wish to include *all* distributions, not just those compatible with the 493 running platform or Python version. 494 495 Note that ``search_path`` is scanned immediately for distributions, and the 496 resulting ``Environment`` is a snapshot of the found distributions. It 497 is not automatically updated if the system's state changes due to e.g. 498 installation or removal of distributions. 499 500``__getitem__(project_name)`` 501 Returns a list of distributions for the given project name, ordered 502 from newest to oldest version. (And highest to lowest format precedence 503 for distributions that contain the same version of the project.) If there 504 are no distributions for the project, returns an empty list. 505 506``__iter__()`` 507 Yield the unique project names of the distributions in this environment. 508 The yielded names are always in lower case. 509 510``add(dist)`` 511 Add ``dist`` to the environment if it matches the platform and python version 512 specified at creation time, and only if the distribution hasn't already 513 been added. (i.e., adding the same distribution more than once is a no-op.) 514 515``remove(dist)`` 516 Remove ``dist`` from the environment. 517 518``can_add(dist)`` 519 Is distribution ``dist`` acceptable for this environment? If it's not 520 compatible with the ``platform`` and ``python`` version values specified 521 when the environment was created, a false value is returned. 522 523``__add__(dist_or_env)`` (``+`` operator) 524 Add a distribution or environment to an ``Environment`` instance, returning 525 a *new* environment object that contains all the distributions previously 526 contained by both. The new environment will have a ``platform`` and 527 ``python`` of ``None``, meaning that it will not reject any distributions 528 from being added to it; it will simply accept whatever is added. If you 529 want the added items to be filtered for platform and Python version, or 530 you want to add them to the *same* environment instance, you should use 531 in-place addition (``+=``) instead. 532 533``__iadd__(dist_or_env)`` (``+=`` operator) 534 Add a distribution or environment to an ``Environment`` instance 535 *in-place*, updating the existing instance and returning it. The 536 ``platform`` and ``python`` filter attributes take effect, so distributions 537 in the source that do not have a suitable platform string or Python version 538 are silently ignored. 539 540``best_match(req, working_set, installer=None)`` 541 Find distribution best matching ``req`` and usable on ``working_set`` 542 543 This calls the ``find(req)`` method of the ``working_set`` to see if a 544 suitable distribution is already active. (This may raise 545 ``VersionConflict`` if an unsuitable version of the project is already 546 active in the specified ``working_set``.) If a suitable distribution isn't 547 active, this method returns the newest distribution in the environment 548 that meets the ``Requirement`` in ``req``. If no suitable distribution is 549 found, and ``installer`` is supplied, then the result of calling 550 the environment's ``obtain(req, installer)`` method will be returned. 551 552``obtain(requirement, installer=None)`` 553 Obtain a distro that matches requirement (e.g. via download). In the 554 base ``Environment`` class, this routine just returns 555 ``installer(requirement)``, unless ``installer`` is None, in which case 556 None is returned instead. This method is a hook that allows subclasses 557 to attempt other ways of obtaining a distribution before falling back 558 to the ``installer`` argument. 559 560``scan(search_path=None)`` 561 Scan ``search_path`` for distributions usable on ``platform`` 562 563 Any distributions found are added to the environment. ``search_path`` should 564 be a sequence of strings such as might be used on ``sys.path``. If not 565 supplied, ``sys.path`` is used. Only distributions conforming to 566 the platform/python version defined at initialization are added. This 567 method is a shortcut for using the ``find_distributions()`` function to 568 find the distributions from each item in ``search_path``, and then calling 569 ``add()`` to add each one to the environment. 570 571 572``Requirement`` Objects 573======================= 574 575``Requirement`` objects express what versions of a project are suitable for 576some purpose. These objects (or their string form) are used by various 577``pkg_resources`` APIs in order to find distributions that a script or 578distribution needs. 579 580 581Requirements Parsing 582-------------------- 583 584``parse_requirements(s)`` 585 Yield ``Requirement`` objects for a string or iterable of lines. Each 586 requirement must start on a new line. See below for syntax. 587 588``Requirement.parse(s)`` 589 Create a ``Requirement`` object from a string or iterable of lines. A 590 ``ValueError`` is raised if the string or lines do not contain a valid 591 requirement specifier, or if they contain more than one specifier. (To 592 parse multiple specifiers from a string or iterable of strings, use 593 ``parse_requirements()`` instead.) 594 595 The syntax of a requirement specifier is defined in full in PEP 508. 596 597 Some examples of valid requirement specifiers:: 598 599 FooProject >= 1.2 600 Fizzy [foo, bar] 601 PickyThing>1.6,<=1.9,!=1.8.6 602 SomethingWhoseVersionIDontCareAbout 603 SomethingWithMarker[foo]>1.0;python_version<"2.7" 604 605 The project name is the only required portion of a requirement string, and 606 if it's the only thing supplied, the requirement will accept any version 607 of that project. 608 609 The "extras" in a requirement are used to request optional features of a 610 project, that may require additional project distributions in order to 611 function. For example, if the hypothetical "Report-O-Rama" project offered 612 optional PDF support, it might require an additional library in order to 613 provide that support. Thus, a project needing Report-O-Rama's PDF features 614 could use a requirement of ``Report-O-Rama[PDF]`` to request installation 615 or activation of both Report-O-Rama and any libraries it needs in order to 616 provide PDF support. For example, you could use:: 617 618 pip install Report-O-Rama[PDF] 619 620 To install the necessary packages using pip, or call 621 ``pkg_resources.require('Report-O-Rama[PDF]')`` to add the necessary 622 distributions to sys.path at runtime. 623 624 The "markers" in a requirement are used to specify when a requirement 625 should be installed -- the requirement will be installed if the marker 626 evaluates as true in the current environment. For example, specifying 627 ``argparse;python_version<"3.0"`` will not install in an Python 3 628 environment, but will in a Python 2 environment. 629 630``Requirement`` Methods and Attributes 631-------------------------------------- 632 633``__contains__(dist_or_version)`` 634 Return true if ``dist_or_version`` fits the criteria for this requirement. 635 If ``dist_or_version`` is a ``Distribution`` object, its project name must 636 match the requirement's project name, and its version must meet the 637 requirement's version criteria. If ``dist_or_version`` is a string, it is 638 parsed using the ``parse_version()`` utility function. Otherwise, it is 639 assumed to be an already-parsed version. 640 641 The ``Requirement`` object's version specifiers (``.specs``) are internally 642 sorted into ascending version order, and used to establish what ranges of 643 versions are acceptable. Adjacent redundant conditions are effectively 644 consolidated (e.g. ``">1, >2"`` produces the same results as ``">2"``, and 645 ``"<2,<3"`` produces the same results as ``"<2"``). ``"!="`` versions are 646 excised from the ranges they fall within. The version being tested for 647 acceptability is then checked for membership in the resulting ranges. 648 649``__eq__(other_requirement)`` 650 A requirement compares equal to another requirement if they have 651 case-insensitively equal project names, version specifiers, and "extras". 652 (The order that extras and version specifiers are in is also ignored.) 653 Equal requirements also have equal hashes, so that requirements can be 654 used in sets or as dictionary keys. 655 656``__str__()`` 657 The string form of a ``Requirement`` is a string that, if passed to 658 ``Requirement.parse()``, would return an equal ``Requirement`` object. 659 660``project_name`` 661 The name of the required project 662 663``key`` 664 An all-lowercase version of the ``project_name``, useful for comparison 665 or indexing. 666 667``extras`` 668 A tuple of names of "extras" that this requirement calls for. (These will 669 be all-lowercase and normalized using the ``safe_extra()`` parsing utility 670 function, so they may not exactly equal the extras the requirement was 671 created with.) 672 673``specs`` 674 A list of ``(op,version)`` tuples, sorted in ascending parsed-version 675 order. The ``op`` in each tuple is a comparison operator, represented as 676 a string. The ``version`` is the (unparsed) version number. 677 678``marker`` 679 An instance of ``packaging.markers.Marker`` that allows evaluation 680 against the current environment. May be None if no marker specified. 681 682``url`` 683 The location to download the requirement from if specified. 684 685Entry Points 686============ 687 688Entry points are a simple way for distributions to "advertise" Python objects 689(such as functions or classes) for use by other distributions. Extensible 690applications and frameworks can search for entry points with a particular name 691or group, either from a specific distribution or from all active distributions 692on sys.path, and then inspect or load the advertised objects at will. 693 694Entry points belong to "groups" which are named with a dotted name similar to 695a Python package or module name. For example, the ``setuptools`` package uses 696an entry point named ``distutils.commands`` in order to find commands defined 697by distutils extensions. ``setuptools`` treats the names of entry points 698defined in that group as the acceptable commands for a setup script. 699 700In a similar way, other packages can define their own entry point groups, 701either using dynamic names within the group (like ``distutils.commands``), or 702possibly using predefined names within the group. For example, a blogging 703framework that offers various pre- or post-publishing hooks might define an 704entry point group and look for entry points named "pre_process" and 705"post_process" within that group. 706 707To advertise an entry point, a project needs to use ``setuptools`` and provide 708an ``entry_points`` argument to ``setup()`` in its setup script, so that the 709entry points will be included in the distribution's metadata. For more 710details, see :ref:`Advertising Behavior<dynamic discovery of services and plugins>`. 711 712Each project distribution can advertise at most one entry point of a given 713name within the same entry point group. For example, a distutils extension 714could advertise two different ``distutils.commands`` entry points, as long as 715they had different names. However, there is nothing that prevents *different* 716projects from advertising entry points of the same name in the same group. In 717some cases, this is a desirable thing, since the application or framework that 718uses the entry points may be calling them as hooks, or in some other way 719combining them. It is up to the application or framework to decide what to do 720if multiple distributions advertise an entry point; some possibilities include 721using both entry points, displaying an error message, using the first one found 722in sys.path order, etc. 723 724 725Convenience API 726--------------- 727 728In the following functions, the ``dist`` argument can be a ``Distribution`` 729instance, a ``Requirement`` instance, or a string specifying a requirement 730(i.e. project name, version, etc.). If the argument is a string or 731``Requirement``, the specified distribution is located (and added to sys.path 732if not already present). An error will be raised if a matching distribution is 733not available. 734 735The ``group`` argument should be a string containing a dotted identifier, 736identifying an entry point group. If you are defining an entry point group, 737you should include some portion of your package's name in the group name so as 738to avoid collision with other packages' entry point groups. 739 740``load_entry_point(dist, group, name)`` 741 Load the named entry point from the specified distribution, or raise 742 ``ImportError``. 743 744``get_entry_info(dist, group, name)`` 745 Return an ``EntryPoint`` object for the given ``group`` and ``name`` from 746 the specified distribution. Returns ``None`` if the distribution has not 747 advertised a matching entry point. 748 749``get_entry_map(dist, group=None)`` 750 Return the distribution's entry point map for ``group``, or the full entry 751 map for the distribution. This function always returns a dictionary, 752 even if the distribution advertises no entry points. If ``group`` is given, 753 the dictionary maps entry point names to the corresponding ``EntryPoint`` 754 object. If ``group`` is None, the dictionary maps group names to 755 dictionaries that then map entry point names to the corresponding 756 ``EntryPoint`` instance in that group. 757 758``iter_entry_points(group, name=None)`` 759 Yield entry point objects from ``group`` matching ``name``. 760 761 If ``name`` is None, yields all entry points in ``group`` from all 762 distributions in the working set on sys.path, otherwise only ones matching 763 both ``group`` and ``name`` are yielded. Entry points are yielded from 764 the active distributions in the order that the distributions appear on 765 sys.path. (Within entry points for a particular distribution, however, 766 there is no particular ordering.) 767 768 (This API is actually a method of the global ``working_set`` object; see 769 the section above on `Basic WorkingSet Methods`_ for more information.) 770 771 772Creating and Parsing 773-------------------- 774 775``EntryPoint(name, module_name, attrs=(), extras=(), dist=None)`` 776 Create an ``EntryPoint`` instance. ``name`` is the entry point name. The 777 ``module_name`` is the (dotted) name of the module containing the advertised 778 object. ``attrs`` is an optional tuple of names to look up from the 779 module to obtain the advertised object. For example, an ``attrs`` of 780 ``("foo","bar")`` and a ``module_name`` of ``"baz"`` would mean that the 781 advertised object could be obtained by the following code:: 782 783 import baz 784 advertised_object = baz.foo.bar 785 786 The ``extras`` are an optional tuple of "extra feature" names that the 787 distribution needs in order to provide this entry point. When the 788 entry point is loaded, these extra features are looked up in the ``dist`` 789 argument to find out what other distributions may need to be activated 790 on sys.path; see the ``load()`` method for more details. The ``extras`` 791 argument is only meaningful if ``dist`` is specified. ``dist`` must be 792 a ``Distribution`` instance. 793 794``EntryPoint.parse(src, dist=None)`` (classmethod) 795 Parse a single entry point from string ``src`` 796 797 Entry point syntax follows the form:: 798 799 name = some.module:some.attr [extra1,extra2] 800 801 The entry name and module name are required, but the ``:attrs`` and 802 ``[extras]`` parts are optional, as is the whitespace shown between 803 some of the items. The ``dist`` argument is passed through to the 804 ``EntryPoint()`` constructor, along with the other values parsed from 805 ``src``. 806 807``EntryPoint.parse_group(group, lines, dist=None)`` (classmethod) 808 Parse ``lines`` (a string or sequence of lines) to create a dictionary 809 mapping entry point names to ``EntryPoint`` objects. ``ValueError`` is 810 raised if entry point names are duplicated, if ``group`` is not a valid 811 entry point group name, or if there are any syntax errors. (Note: the 812 ``group`` parameter is used only for validation and to create more 813 informative error messages.) If ``dist`` is provided, it will be used to 814 set the ``dist`` attribute of the created ``EntryPoint`` objects. 815 816``EntryPoint.parse_map(data, dist=None)`` (classmethod) 817 Parse ``data`` into a dictionary mapping group names to dictionaries mapping 818 entry point names to ``EntryPoint`` objects. If ``data`` is a dictionary, 819 then the keys are used as group names and the values are passed to 820 ``parse_group()`` as the ``lines`` argument. If ``data`` is a string or 821 sequence of lines, it is first split into .ini-style sections (using 822 the ``split_sections()`` utility function) and the section names are used 823 as group names. In either case, the ``dist`` argument is passed through to 824 ``parse_group()`` so that the entry points will be linked to the specified 825 distribution. 826 827 828``EntryPoint`` Objects 829---------------------- 830 831For simple introspection, ``EntryPoint`` objects have attributes that 832correspond exactly to the constructor argument names: ``name``, 833``module_name``, ``attrs``, ``extras``, and ``dist`` are all available. In 834addition, the following methods are provided: 835 836``load()`` 837 Load the entry point, returning the advertised Python object. Effectively 838 calls ``self.require()`` then returns ``self.resolve()``. 839 840``require(env=None, installer=None)`` 841 Ensure that any "extras" needed by the entry point are available on 842 sys.path. ``UnknownExtra`` is raised if the ``EntryPoint`` has ``extras``, 843 but no ``dist``, or if the named extras are not defined by the 844 distribution. If ``env`` is supplied, it must be an ``Environment``, and it 845 will be used to search for needed distributions if they are not already 846 present on sys.path. If ``installer`` is supplied, it must be a callable 847 taking a ``Requirement`` instance and returning a matching importable 848 ``Distribution`` instance or None. 849 850``resolve()`` 851 Resolve the entry point from its module and attrs, returning the advertised 852 Python object. Raises ``ImportError`` if it cannot be obtained. 853 854``__str__()`` 855 The string form of an ``EntryPoint`` is a string that could be passed to 856 ``EntryPoint.parse()`` to produce an equivalent ``EntryPoint``. 857 858 859``Distribution`` Objects 860======================== 861 862``Distribution`` objects represent collections of Python code that may or may 863not be importable, and may or may not have metadata and resources associated 864with them. Their metadata may include information such as what other projects 865the distribution depends on, what entry points the distribution advertises, and 866so on. 867 868 869Getting or Creating Distributions 870--------------------------------- 871 872Most commonly, you'll obtain ``Distribution`` objects from a ``WorkingSet`` or 873an ``Environment``. (See the sections above on `WorkingSet Objects`_ and 874`Environment Objects`_, which are containers for active distributions and 875available distributions, respectively.) You can also obtain ``Distribution`` 876objects from one of these high-level APIs: 877 878``find_distributions(path_item, only=False)`` 879 Yield distributions accessible via ``path_item``. If ``only`` is true, yield 880 only distributions whose ``location`` is equal to ``path_item``. In other 881 words, if ``only`` is true, this yields any distributions that would be 882 importable if ``path_item`` were on ``sys.path``. If ``only`` is false, this 883 also yields distributions that are "in" or "under" ``path_item``, but would 884 not be importable unless their locations were also added to ``sys.path``. 885 886``get_distribution(dist_spec)`` 887 Return a ``Distribution`` object for a given ``Requirement`` or string. 888 If ``dist_spec`` is already a ``Distribution`` instance, it is returned. 889 If it is a ``Requirement`` object or a string that can be parsed into one, 890 it is used to locate and activate a matching distribution, which is then 891 returned. 892 893However, if you're creating specialized tools for working with distributions, 894or creating a new distribution format, you may also need to create 895``Distribution`` objects directly, using one of the three constructors below. 896 897These constructors all take an optional ``metadata`` argument, which is used to 898access any resources or metadata associated with the distribution. ``metadata`` 899must be an object that implements the ``IResourceProvider`` interface, or None. 900If it is None, an ``EmptyProvider`` is used instead. ``Distribution`` objects 901implement both the `IResourceProvider`_ and `IMetadataProvider Methods`_ by 902delegating them to the ``metadata`` object. 903 904``Distribution.from_location(location, basename, metadata=None, **kw)`` (classmethod) 905 Create a distribution for ``location``, which must be a string such as a 906 URL, filename, or other string that might be used on ``sys.path``. 907 ``basename`` is a string naming the distribution, like ``Foo-1.2-py2.4.egg``. 908 If ``basename`` ends with ``.egg``, then the project's name, version, python 909 version and platform are extracted from the filename and used to set those 910 properties of the created distribution. Any additional keyword arguments 911 are forwarded to the ``Distribution()`` constructor. 912 913``Distribution.from_filename(filename, metadata=None**kw)`` (classmethod) 914 Create a distribution by parsing a local filename. This is a shorter way 915 of saying ``Distribution.from_location(normalize_path(filename), 916 os.path.basename(filename), metadata)``. In other words, it creates a 917 distribution whose location is the normalize form of the filename, parsing 918 name and version information from the base portion of the filename. Any 919 additional keyword arguments are forwarded to the ``Distribution()`` 920 constructor. 921 922``Distribution(location,metadata,project_name,version,py_version,platform,precedence)`` 923 Create a distribution by setting its properties. All arguments are 924 optional and default to None, except for ``py_version`` (which defaults to 925 the current Python version) and ``precedence`` (which defaults to 926 ``EGG_DIST``; for more details see ``precedence`` under `Distribution 927 Attributes`_ below). Note that it's usually easier to use the 928 ``from_filename()`` or ``from_location()`` constructors than to specify 929 all these arguments individually. 930 931 932``Distribution`` Attributes 933--------------------------- 934 935location 936 A string indicating the distribution's location. For an importable 937 distribution, this is the string that would be added to ``sys.path`` to 938 make it actively importable. For non-importable distributions, this is 939 simply a filename, URL, or other way of locating the distribution. 940 941project_name 942 A string, naming the project that this distribution is for. Project names 943 are defined by a project's setup script, and they are used to identify 944 projects on PyPI. When a ``Distribution`` is constructed, the 945 ``project_name`` argument is passed through the ``safe_name()`` utility 946 function to filter out any unacceptable characters. 947 948key 949 ``dist.key`` is short for ``dist.project_name.lower()``. It's used for 950 case-insensitive comparison and indexing of distributions by project name. 951 952extras 953 A list of strings, giving the names of extra features defined by the 954 project's dependency list (the ``extras_require`` argument specified in 955 the project's setup script). 956 957version 958 A string denoting what release of the project this distribution contains. 959 When a ``Distribution`` is constructed, the ``version`` argument is passed 960 through the ``safe_version()`` utility function to filter out any 961 unacceptable characters. If no ``version`` is specified at construction 962 time, then attempting to access this attribute later will cause the 963 ``Distribution`` to try to discover its version by reading its ``PKG-INFO`` 964 metadata file. If ``PKG-INFO`` is unavailable or can't be parsed, 965 ``ValueError`` is raised. 966 967parsed_version 968 The ``parsed_version`` is an object representing a "parsed" form of the 969 distribution's ``version``. ``dist.parsed_version`` is a shortcut for 970 calling ``parse_version(dist.version)``. It is used to compare or sort 971 distributions by version. (See the `Parsing Utilities`_ section below for 972 more information on the ``parse_version()`` function.) Note that accessing 973 ``parsed_version`` may result in a ``ValueError`` if the ``Distribution`` 974 was constructed without a ``version`` and without ``metadata`` capable of 975 supplying the missing version info. 976 977py_version 978 The major/minor Python version the distribution supports, as a string. 979 For example, "2.7" or "3.4". The default is the current version of Python. 980 981platform 982 A string representing the platform the distribution is intended for, or 983 ``None`` if the distribution is "pure Python" and therefore cross-platform. 984 See `Platform Utilities`_ below for more information on platform strings. 985 986precedence 987 A distribution's ``precedence`` is used to determine the relative order of 988 two distributions that have the same ``project_name`` and 989 ``parsed_version``. The default precedence is ``pkg_resources.EGG_DIST``, 990 which is the highest (i.e. most preferred) precedence. The full list 991 of predefined precedences, from most preferred to least preferred, is: 992 ``EGG_DIST``, ``BINARY_DIST``, ``SOURCE_DIST``, ``CHECKOUT_DIST``, and 993 ``DEVELOP_DIST``. Normally, precedences other than ``EGG_DIST`` are used 994 only by the ``setuptools.package_index`` module, when sorting distributions 995 found in a package index to determine their suitability for installation. 996 "System" and "Development" eggs (i.e., ones that use the ``.egg-info`` 997 format), however, are automatically given a precedence of ``DEVELOP_DIST``. 998 999 1000 1001``Distribution`` Methods 1002------------------------ 1003 1004``activate(path=None)`` 1005 Ensure distribution is importable on ``path``. If ``path`` is None, 1006 ``sys.path`` is used instead. This ensures that the distribution's 1007 ``location`` is in the ``path`` list, and it also performs any necessary 1008 namespace package fixups or declarations. (That is, if the distribution 1009 contains namespace packages, this method ensures that they are declared, 1010 and that the distribution's contents for those namespace packages are 1011 merged with the contents provided by any other active distributions. See 1012 the section above on `Namespace Package Support`_ for more information.) 1013 1014 ``pkg_resources`` adds a notification callback to the global ``working_set`` 1015 that ensures this method is called whenever a distribution is added to it. 1016 Therefore, you should not normally need to explicitly call this method. 1017 (Note that this means that namespace packages on ``sys.path`` are always 1018 imported as soon as ``pkg_resources`` is, which is another reason why 1019 namespace packages should not contain any code or import statements.) 1020 1021``as_requirement()`` 1022 Return a ``Requirement`` instance that matches this distribution's project 1023 name and version. 1024 1025``requires(extras=())`` 1026 List the ``Requirement`` objects that specify this distribution's 1027 dependencies. If ``extras`` is specified, it should be a sequence of names 1028 of "extras" defined by the distribution, and the list returned will then 1029 include any dependencies needed to support the named "extras". 1030 1031``clone(**kw)`` 1032 Create a copy of the distribution. Any supplied keyword arguments override 1033 the corresponding argument to the ``Distribution()`` constructor, allowing 1034 you to change some of the copied distribution's attributes. 1035 1036``egg_name()`` 1037 Return what this distribution's standard filename should be, not including 1038 the ".egg" extension. For example, a distribution for project "Foo" 1039 version 1.2 that runs on Python 2.3 for Windows would have an ``egg_name()`` 1040 of ``Foo-1.2-py2.3-win32``. Any dashes in the name or version are 1041 converted to underscores. (``Distribution.from_location()`` will convert 1042 them back when parsing a ".egg" file name.) 1043 1044``__cmp__(other)``, ``__hash__()`` 1045 Distribution objects are hashed and compared on the basis of their parsed 1046 version and precedence, followed by their key (lowercase project name), 1047 location, Python version, and platform. 1048 1049The following methods are used to access ``EntryPoint`` objects advertised 1050by the distribution. See the section above on `Entry Points`_ for more 1051detailed information about these operations: 1052 1053``get_entry_info(group, name)`` 1054 Return the ``EntryPoint`` object for ``group`` and ``name``, or None if no 1055 such point is advertised by this distribution. 1056 1057``get_entry_map(group=None)`` 1058 Return the entry point map for ``group``. If ``group`` is None, return 1059 a dictionary mapping group names to entry point maps for all groups. 1060 (An entry point map is a dictionary of entry point names to ``EntryPoint`` 1061 objects.) 1062 1063``load_entry_point(group, name)`` 1064 Short for ``get_entry_info(group, name).load()``. Returns the object 1065 advertised by the named entry point, or raises ``ImportError`` if 1066 the entry point isn't advertised by this distribution, or there is some 1067 other import problem. 1068 1069In addition to the above methods, ``Distribution`` objects also implement all 1070of the `IResourceProvider`_ and `IMetadataProvider Methods`_ (which are 1071documented in later sections): 1072 1073* ``has_metadata(name)`` 1074* ``metadata_isdir(name)`` 1075* ``metadata_listdir(name)`` 1076* ``get_metadata(name)`` 1077* ``get_metadata_lines(name)`` 1078* ``run_script(script_name, namespace)`` 1079* ``get_resource_filename(manager, resource_name)`` 1080* ``get_resource_stream(manager, resource_name)`` 1081* ``get_resource_string(manager, resource_name)`` 1082* ``has_resource(resource_name)`` 1083* ``resource_isdir(resource_name)`` 1084* ``resource_listdir(resource_name)`` 1085 1086If the distribution was created with a ``metadata`` argument, these resource and 1087metadata access methods are all delegated to that ``metadata`` provider. 1088Otherwise, they are delegated to an ``EmptyProvider``, so that the distribution 1089will appear to have no resources or metadata. This delegation approach is used 1090so that supporting custom importers or new distribution formats can be done 1091simply by creating an appropriate `IResourceProvider`_ implementation; see the 1092section below on `Supporting Custom Importers`_ for more details. 1093 1094.. _ResourceManager API: 1095 1096``ResourceManager`` API 1097======================= 1098 1099The ``ResourceManager`` class provides uniform access to package resources, 1100whether those resources exist as files and directories or are compressed in 1101an archive of some kind. 1102 1103Normally, you do not need to create or explicitly manage ``ResourceManager`` 1104instances, as the ``pkg_resources`` module creates a global instance for you, 1105and makes most of its methods available as top-level names in the 1106``pkg_resources`` module namespace. So, for example, this code actually 1107calls the ``resource_string()`` method of the global ``ResourceManager``:: 1108 1109 import pkg_resources 1110 my_data = pkg_resources.resource_string(__name__, "foo.dat") 1111 1112Thus, you can use the APIs below without needing an explicit 1113``ResourceManager`` instance; just import and use them as needed. 1114 1115 1116Basic Resource Access 1117--------------------- 1118 1119In the following methods, the ``package_or_requirement`` argument may be either 1120a Python package/module name (e.g. ``foo.bar``) or a ``Requirement`` instance. 1121If it is a package or module name, the named module or package must be 1122importable (i.e., be in a distribution or directory on ``sys.path``), and the 1123``resource_name`` argument is interpreted relative to the named package. (Note 1124that if a module name is used, then the resource name is relative to the 1125package immediately containing the named module. Also, you should not use use 1126a namespace package name, because a namespace package can be spread across 1127multiple distributions, and is therefore ambiguous as to which distribution 1128should be searched for the resource.) 1129 1130If it is a ``Requirement``, then the requirement is automatically resolved 1131(searching the current ``Environment`` if necessary) and a matching 1132distribution is added to the ``WorkingSet`` and ``sys.path`` if one was not 1133already present. (Unless the ``Requirement`` can't be satisfied, in which 1134case an exception is raised.) The ``resource_name`` argument is then interpreted 1135relative to the root of the identified distribution; i.e. its first path 1136segment will be treated as a peer of the top-level modules or packages in the 1137distribution. 1138 1139Note that resource names must be ``/``-separated paths rooted at the package, 1140cannot contain relative names like ``".."``, and cannot be absolute. Do *not* use 1141``os.path`` routines to manipulate resource paths, as they are *not* filesystem 1142paths. 1143 1144``resource_exists(package_or_requirement, resource_name)`` 1145 Does the named resource exist? Return ``True`` or ``False`` accordingly. 1146 1147``resource_stream(package_or_requirement, resource_name)`` 1148 Return a readable file-like object for the specified resource; it may be 1149 an actual file, a ``StringIO``, or some similar object. The stream is 1150 in "binary mode", in the sense that whatever bytes are in the resource 1151 will be read as-is. 1152 1153``resource_string(package_or_requirement, resource_name)`` 1154 Return the specified resource as ``bytes``. The resource is read in 1155 binary fashion, such that the returned string contains exactly the bytes 1156 that are stored in the resource. 1157 1158``resource_isdir(package_or_requirement, resource_name)`` 1159 Is the named resource a directory? Return ``True`` or ``False`` 1160 accordingly. 1161 1162``resource_listdir(package_or_requirement, resource_name)`` 1163 List the contents of the named resource directory, just like ``os.listdir`` 1164 except that it works even if the resource is in a zipfile. 1165 1166Note that only ``resource_exists()`` and ``resource_isdir()`` are insensitive 1167as to the resource type. You cannot use ``resource_listdir()`` on a file 1168resource, and you can't use ``resource_string()`` or ``resource_stream()`` on 1169directory resources. Using an inappropriate method for the resource type may 1170result in an exception or undefined behavior, depending on the platform and 1171distribution format involved. 1172 1173 1174Resource Extraction 1175------------------- 1176 1177``resource_filename(package_or_requirement, resource_name)`` 1178 Sometimes, it is not sufficient to access a resource in string or stream 1179 form, and a true filesystem filename is needed. In such cases, you can 1180 use this method (or module-level function) to obtain a filename for a 1181 resource. If the resource is in an archive distribution (such as a zipped 1182 egg), it will be extracted to a cache directory, and the filename within 1183 the cache will be returned. If the named resource is a directory, then 1184 all resources within that directory (including subdirectories) are also 1185 extracted. If the named resource is a C extension or "eager resource" 1186 (see the ``setuptools`` documentation for details), then all C extensions 1187 and eager resources are extracted at the same time. 1188 1189 Archived resources are extracted to a cache location that can be managed by 1190 the following two methods: 1191 1192``set_extraction_path(path)`` 1193 Set the base path where resources will be extracted to, if needed. 1194 1195 If you do not call this routine before any extractions take place, the 1196 path defaults to the return value of ``get_default_cache()``. (Which is 1197 based on the ``PYTHON_EGG_CACHE`` environment variable, with various 1198 platform-specific fallbacks. See that routine's documentation for more 1199 details.) 1200 1201 Resources are extracted to subdirectories of this path based upon 1202 information given by the resource provider. You may set this to a 1203 temporary directory, but then you must call ``cleanup_resources()`` to 1204 delete the extracted files when done. There is no guarantee that 1205 ``cleanup_resources()`` will be able to remove all extracted files. (On 1206 Windows, for example, you can't unlink .pyd or .dll files that are still 1207 in use.) 1208 1209 Note that you may not change the extraction path for a given resource 1210 manager once resources have been extracted, unless you first call 1211 ``cleanup_resources()``. 1212 1213``cleanup_resources(force=False)`` 1214 Delete all extracted resource files and directories, returning a list 1215 of the file and directory names that could not be successfully removed. 1216 This function does not have any concurrency protection, so it should 1217 generally only be called when the extraction path is a temporary 1218 directory exclusive to a single process. This method is not 1219 automatically called; you must call it explicitly or register it as an 1220 ``atexit`` function if you wish to ensure cleanup of a temporary 1221 directory used for extractions. 1222 1223 1224"Provider" Interface 1225-------------------- 1226 1227If you are implementing an ``IResourceProvider`` and/or ``IMetadataProvider`` 1228for a new distribution archive format, you may need to use the following 1229``IResourceManager`` methods to coordinate extraction of resources to the 1230filesystem. If you're not implementing an archive format, however, you have 1231no need to use these methods. Unlike the other methods listed above, they are 1232*not* available as top-level functions tied to the global ``ResourceManager``; 1233you must therefore have an explicit ``ResourceManager`` instance to use them. 1234 1235``get_cache_path(archive_name, names=())`` 1236 Return absolute location in cache for ``archive_name`` and ``names`` 1237 1238 The parent directory of the resulting path will be created if it does 1239 not already exist. ``archive_name`` should be the base filename of the 1240 enclosing egg (which may not be the name of the enclosing zipfile!), 1241 including its ".egg" extension. ``names``, if provided, should be a 1242 sequence of path name parts "under" the egg's extraction location. 1243 1244 This method should only be called by resource providers that need to 1245 obtain an extraction location, and only for names they intend to 1246 extract, as it tracks the generated names for possible cleanup later. 1247 1248``extraction_error()`` 1249 Raise an ``ExtractionError`` describing the active exception as interfering 1250 with the extraction process. You should call this if you encounter any 1251 OS errors extracting the file to the cache path; it will format the 1252 operating system exception for you, and add other information to the 1253 ``ExtractionError`` instance that may be needed by programs that want to 1254 wrap or handle extraction errors themselves. 1255 1256``postprocess(tempname, filename)`` 1257 Perform any platform-specific postprocessing of ``tempname``. 1258 Resource providers should call this method ONLY after successfully 1259 extracting a compressed resource. They must NOT call it on resources 1260 that are already in the filesystem. 1261 1262 ``tempname`` is the current (temporary) name of the file, and ``filename`` 1263 is the name it will be renamed to by the caller after this routine 1264 returns. 1265 1266 1267Metadata API 1268============ 1269 1270The metadata API is used to access metadata resources bundled in a pluggable 1271distribution. Metadata resources are virtual files or directories containing 1272information about the distribution, such as might be used by an extensible 1273application or framework to connect "plugins". Like other kinds of resources, 1274metadata resource names are ``/``-separated and should not contain ``..`` or 1275begin with a ``/``. You should not use ``os.path`` routines to manipulate 1276resource paths. 1277 1278The metadata API is provided by objects implementing the ``IMetadataProvider`` 1279or ``IResourceProvider`` interfaces. ``Distribution`` objects implement this 1280interface, as do objects returned by the ``get_provider()`` function: 1281 1282``get_provider(package_or_requirement)`` 1283 If a package name is supplied, return an ``IResourceProvider`` for the 1284 package. If a ``Requirement`` is supplied, resolve it by returning a 1285 ``Distribution`` from the current working set (searching the current 1286 ``Environment`` if necessary and adding the newly found ``Distribution`` 1287 to the working set). If the named package can't be imported, or the 1288 ``Requirement`` can't be satisfied, an exception is raised. 1289 1290 NOTE: if you use a package name rather than a ``Requirement``, the object 1291 you get back may not be a pluggable distribution, depending on the method 1292 by which the package was installed. In particular, "development" packages 1293 and "single-version externally-managed" packages do not have any way to 1294 map from a package name to the corresponding project's metadata. Do not 1295 write code that passes a package name to ``get_provider()`` and then tries 1296 to retrieve project metadata from the returned object. It may appear to 1297 work when the named package is in an ``.egg`` file or directory, but 1298 it will fail in other installation scenarios. If you want project 1299 metadata, you need to ask for a *project*, not a package. 1300 1301 1302``IMetadataProvider`` Methods 1303----------------------------- 1304 1305The methods provided by objects (such as ``Distribution`` instances) that 1306implement the ``IMetadataProvider`` or ``IResourceProvider`` interfaces are: 1307 1308``has_metadata(name)`` 1309 Does the named metadata resource exist? 1310 1311``metadata_isdir(name)`` 1312 Is the named metadata resource a directory? 1313 1314``metadata_listdir(name)`` 1315 List of metadata names in the directory (like ``os.listdir()``) 1316 1317``get_metadata(name)`` 1318 Return the named metadata resource as a string. The data is read in binary 1319 mode; i.e., the exact bytes of the resource file are returned. 1320 1321``get_metadata_lines(name)`` 1322 Yield named metadata resource as list of non-blank non-comment lines. This 1323 is short for calling ``yield_lines(provider.get_metadata(name))``. See the 1324 section on `yield_lines()`_ below for more information on the syntax it 1325 recognizes. 1326 1327``run_script(script_name, namespace)`` 1328 Execute the named script in the supplied namespace dictionary. Raises 1329 ``ResolutionError`` if there is no script by that name in the ``scripts`` 1330 metadata directory. ``namespace`` should be a Python dictionary, usually 1331 a module dictionary if the script is being run as a module. 1332 1333 1334Exceptions 1335========== 1336 1337``pkg_resources`` provides a simple exception hierarchy for problems that may 1338occur when processing requests to locate and activate packages:: 1339 1340 ResolutionError 1341 DistributionNotFound 1342 VersionConflict 1343 UnknownExtra 1344 1345 ExtractionError 1346 1347``ResolutionError`` 1348 This class is used as a base class for the other three exceptions, so that 1349 you can catch all of them with a single "except" clause. It is also raised 1350 directly for miscellaneous requirement-resolution problems like trying to 1351 run a script that doesn't exist in the distribution it was requested from. 1352 1353``DistributionNotFound`` 1354 A distribution needed to fulfill a requirement could not be found. 1355 1356``VersionConflict`` 1357 The requested version of a project conflicts with an already-activated 1358 version of the same project. 1359 1360``UnknownExtra`` 1361 One of the "extras" requested was not recognized by the distribution it 1362 was requested from. 1363 1364``ExtractionError`` 1365 A problem occurred extracting a resource to the Python Egg cache. The 1366 following attributes are available on instances of this exception: 1367 1368 manager 1369 The resource manager that raised this exception 1370 1371 cache_path 1372 The base directory for resource extraction 1373 1374 original_error 1375 The exception instance that caused extraction to fail 1376 1377 1378Supporting Custom Importers 1379=========================== 1380 1381By default, ``pkg_resources`` supports normal filesystem imports, and 1382``zipimport`` importers. If you wish to use the ``pkg_resources`` features 1383with other (PEP 302-compatible) importers or module loaders, you may need to 1384register various handlers and support functions using these APIs: 1385 1386``register_finder(importer_type, distribution_finder)`` 1387 Register ``distribution_finder`` to find distributions in ``sys.path`` items. 1388 ``importer_type`` is the type or class of a PEP 302 "Importer" (``sys.path`` 1389 item handler), and ``distribution_finder`` is a callable that, when passed a 1390 path item, the importer instance, and an ``only`` flag, yields 1391 ``Distribution`` instances found under that path item. (The ``only`` flag, 1392 if true, means the finder should yield only ``Distribution`` objects whose 1393 ``location`` is equal to the path item provided.) 1394 1395 See the source of the ``pkg_resources.find_on_path`` function for an 1396 example finder function. 1397 1398``register_loader_type(loader_type, provider_factory)`` 1399 Register ``provider_factory`` to make ``IResourceProvider`` objects for 1400 ``loader_type``. ``loader_type`` is the type or class of a PEP 302 1401 ``module.__loader__``, and ``provider_factory`` is a function that, when 1402 passed a module object, returns an `IResourceProvider`_ for that module, 1403 allowing it to be used with the `ResourceManager API`_. 1404 1405``register_namespace_handler(importer_type, namespace_handler)`` 1406 Register ``namespace_handler`` to declare namespace packages for the given 1407 ``importer_type``. ``importer_type`` is the type or class of a PEP 302 1408 "importer" (sys.path item handler), and ``namespace_handler`` is a callable 1409 with a signature like this:: 1410 1411 def namespace_handler(importer, path_entry, moduleName, module): 1412 # return a path_entry to use for child packages 1413 1414 Namespace handlers are only called if the relevant importer object has 1415 already agreed that it can handle the relevant path item. The handler 1416 should only return a subpath if the module ``__path__`` does not already 1417 contain an equivalent subpath. Otherwise, it should return None. 1418 1419 For an example namespace handler, see the source of the 1420 ``pkg_resources.file_ns_handler`` function, which is used for both zipfile 1421 importing and regular importing. 1422 1423 1424IResourceProvider 1425----------------- 1426 1427``IResourceProvider`` is an abstract class that documents what methods are 1428required of objects returned by a ``provider_factory`` registered with 1429``register_loader_type()``. ``IResourceProvider`` is a subclass of 1430``IMetadataProvider``, so objects that implement this interface must also 1431implement all of the `IMetadataProvider Methods`_ as well as the methods 1432shown here. The ``manager`` argument to the methods below must be an object 1433that supports the full `ResourceManager API`_ documented above. 1434 1435``get_resource_filename(manager, resource_name)`` 1436 Return a true filesystem path for ``resource_name``, coordinating the 1437 extraction with ``manager``, if the resource must be unpacked to the 1438 filesystem. 1439 1440``get_resource_stream(manager, resource_name)`` 1441 Return a readable file-like object for ``resource_name``. 1442 1443``get_resource_string(manager, resource_name)`` 1444 Return a string containing the contents of ``resource_name``. 1445 1446``has_resource(resource_name)`` 1447 Does the package contain the named resource? 1448 1449``resource_isdir(resource_name)`` 1450 Is the named resource a directory? Return a false value if the resource 1451 does not exist or is not a directory. 1452 1453``resource_listdir(resource_name)`` 1454 Return a list of the contents of the resource directory, ala 1455 ``os.listdir()``. Requesting the contents of a non-existent directory may 1456 raise an exception. 1457 1458Note, by the way, that your provider classes need not (and should not) subclass 1459``IResourceProvider`` or ``IMetadataProvider``! These classes exist solely 1460for documentation purposes and do not provide any useful implementation code. 1461You may instead wish to subclass one of the `built-in resource providers`_. 1462 1463 1464Built-in Resource Providers 1465--------------------------- 1466 1467``pkg_resources`` includes several provider classes that are automatically used 1468where appropriate. Their inheritance tree looks like this:: 1469 1470 NullProvider 1471 EggProvider 1472 DefaultProvider 1473 PathMetadata 1474 ZipProvider 1475 EggMetadata 1476 EmptyProvider 1477 FileMetadata 1478 1479 1480``NullProvider`` 1481 This provider class is just an abstract base that provides for common 1482 provider behaviors (such as running scripts), given a definition for just 1483 a few abstract methods. 1484 1485``EggProvider`` 1486 This provider class adds in some egg-specific features that are common 1487 to zipped and unzipped eggs. 1488 1489``DefaultProvider`` 1490 This provider class is used for unpacked eggs and "plain old Python" 1491 filesystem modules. 1492 1493``ZipProvider`` 1494 This provider class is used for all zipped modules, whether they are eggs 1495 or not. 1496 1497``EmptyProvider`` 1498 This provider class always returns answers consistent with a provider that 1499 has no metadata or resources. ``Distribution`` objects created without 1500 a ``metadata`` argument use an instance of this provider class instead. 1501 Since all ``EmptyProvider`` instances are equivalent, there is no need 1502 to have more than one instance. ``pkg_resources`` therefore creates a 1503 global instance of this class under the name ``empty_provider``, and you 1504 may use it if you have need of an ``EmptyProvider`` instance. 1505 1506``PathMetadata(path, egg_info)`` 1507 Create an ``IResourceProvider`` for a filesystem-based distribution, where 1508 ``path`` is the filesystem location of the importable modules, and ``egg_info`` 1509 is the filesystem location of the distribution's metadata directory. 1510 ``egg_info`` should usually be the ``EGG-INFO`` subdirectory of ``path`` for an 1511 "unpacked egg", and a ``ProjectName.egg-info`` subdirectory of ``path`` for 1512 a "development egg". However, other uses are possible for custom purposes. 1513 1514``EggMetadata(zipimporter)`` 1515 Create an ``IResourceProvider`` for a zipfile-based distribution. The 1516 ``zipimporter`` should be a ``zipimport.zipimporter`` instance, and may 1517 represent a "basket" (a zipfile containing multiple ".egg" subdirectories) 1518 a specific egg *within* a basket, or a zipfile egg (where the zipfile 1519 itself is a ".egg"). It can also be a combination, such as a zipfile egg 1520 that also contains other eggs. 1521 1522``FileMetadata(path_to_pkg_info)`` 1523 Create an ``IResourceProvider`` that provides exactly one metadata 1524 resource: ``PKG-INFO``. The supplied path should be a distutils PKG-INFO 1525 file. This is basically the same as an ``EmptyProvider``, except that 1526 requests for ``PKG-INFO`` will be answered using the contents of the 1527 designated file. (This provider is used to wrap ``.egg-info`` files 1528 installed by vendor-supplied system packages.) 1529 1530 1531Utility Functions 1532================= 1533 1534In addition to its high-level APIs, ``pkg_resources`` also includes several 1535generally-useful utility routines. These routines are used to implement the 1536high-level APIs, but can also be quite useful by themselves. 1537 1538 1539Parsing Utilities 1540----------------- 1541 1542``parse_version(version)`` 1543 Parsed a project's version string as defined by PEP 440. The returned 1544 value will be an object that represents the version. These objects may 1545 be compared to each other and sorted. The sorting algorithm is as defined 1546 by PEP 440 with the addition that any version which is not a valid PEP 440 1547 version will be considered less than any valid PEP 440 version and the 1548 invalid versions will continue sorting using the original algorithm. 1549 1550.. _yield_lines(): 1551 1552``yield_lines(strs)`` 1553 Yield non-empty/non-comment lines from a string/unicode or a possibly- 1554 nested sequence thereof. If ``strs`` is an instance of ``basestring``, it 1555 is split into lines, and each non-blank, non-comment line is yielded after 1556 stripping leading and trailing whitespace. (Lines whose first non-blank 1557 character is ``#`` are considered comment lines.) 1558 1559 If ``strs`` is not an instance of ``basestring``, it is iterated over, and 1560 each item is passed recursively to ``yield_lines()``, so that an arbitrarily 1561 nested sequence of strings, or sequences of sequences of strings can be 1562 flattened out to the lines contained therein. So for example, passing 1563 a file object or a list of strings to ``yield_lines`` will both work. 1564 (Note that between each string in a sequence of strings there is assumed to 1565 be an implicit line break, so lines cannot bridge two strings in a 1566 sequence.) 1567 1568 This routine is used extensively by ``pkg_resources`` to parse metadata 1569 and file formats of various kinds, and most other ``pkg_resources`` 1570 parsing functions that yield multiple values will use it to break up their 1571 input. However, this routine is idempotent, so calling ``yield_lines()`` 1572 on the output of another call to ``yield_lines()`` is completely harmless. 1573 1574``split_sections(strs)`` 1575 Split a string (or possibly-nested iterable thereof), yielding ``(section, 1576 content)`` pairs found using an ``.ini``-like syntax. Each ``section`` is 1577 a whitespace-stripped version of the section name ("``[section]``") 1578 and each ``content`` is a list of stripped lines excluding blank lines and 1579 comment-only lines. If there are any non-blank, non-comment lines before 1580 the first section header, they're yielded in a first ``section`` of 1581 ``None``. 1582 1583 This routine uses ``yield_lines()`` as its front end, so you can pass in 1584 anything that ``yield_lines()`` accepts, such as an open text file, string, 1585 or sequence of strings. ``ValueError`` is raised if a malformed section 1586 header is found (i.e. a line starting with ``[`` but not ending with 1587 ``]``). 1588 1589 Note that this simplistic parser assumes that any line whose first nonblank 1590 character is ``[`` is a section heading, so it can't support .ini format 1591 variations that allow ``[`` as the first nonblank character on other lines. 1592 1593``safe_name(name)`` 1594 Return a "safe" form of a project's name, suitable for use in a 1595 ``Requirement`` string, as a distribution name, or a PyPI project name. 1596 All non-alphanumeric runs are condensed to single "-" characters, such that 1597 a name like "The $$$ Tree" becomes "The-Tree". Note that if you are 1598 generating a filename from this value you should combine it with a call to 1599 ``to_filename()`` so all dashes ("-") are replaced by underscores ("_"). 1600 See ``to_filename()``. 1601 1602``safe_version(version)`` 1603 This will return the normalized form of any PEP 440 version. If the version 1604 string is not PEP 440 compatible, this function behaves similar to 1605 ``safe_name()`` except that spaces in the input become dots, and dots are 1606 allowed to exist in the output. As with ``safe_name()``, if you are 1607 generating a filename from this you should replace any "-" characters in 1608 the output with underscores. 1609 1610``safe_extra(extra)`` 1611 Return a "safe" form of an extra's name, suitable for use in a requirement 1612 string or a setup script's ``extras_require`` keyword. This routine is 1613 similar to ``safe_name()`` except that non-alphanumeric runs are replaced 1614 by a single underbar (``_``), and the result is lowercased. 1615 1616``to_filename(name_or_version)`` 1617 Escape a name or version string so it can be used in a dash-separated 1618 filename (or ``#egg=name-version`` tag) without ambiguity. You 1619 should only pass in values that were returned by ``safe_name()`` or 1620 ``safe_version()``. 1621 1622 1623Platform Utilities 1624------------------ 1625 1626``get_build_platform()`` 1627 Return this platform's identifier string. For Windows, the return value 1628 is ``"win32"``, and for macOS it is a string of the form 1629 ``"macosx-10.4-ppc"``. All other platforms return the same uname-based 1630 string that the ``distutils.util.get_platform()`` function returns. 1631 This string is the minimum platform version required by distributions built 1632 on the local machine. (Backward compatibility note: setuptools versions 1633 prior to 0.6b1 called this function ``get_platform()``, and the function is 1634 still available under that name for backward compatibility reasons.) 1635 1636``get_supported_platform()`` (New in 0.6b1) 1637 This is the similar to ``get_build_platform()``, but is the maximum 1638 platform version that the local machine supports. You will usually want 1639 to use this value as the ``provided`` argument to the 1640 ``compatible_platforms()`` function. 1641 1642``compatible_platforms(provided, required)`` 1643 Return true if a distribution built on the ``provided`` platform may be used 1644 on the ``required`` platform. If either platform value is ``None``, it is 1645 considered a wildcard, and the platforms are therefore compatible. 1646 Likewise, if the platform strings are equal, they're also considered 1647 compatible, and ``True`` is returned. Currently, the only non-equal 1648 platform strings that are considered compatible are macOS platform 1649 strings with the same hardware type (e.g. ``ppc``) and major version 1650 (e.g. ``10``) with the ``provided`` platform's minor version being less than 1651 or equal to the ``required`` platform's minor version. 1652 1653``get_default_cache()`` 1654 Determine the default cache location for extracting resources from zipped 1655 eggs. This routine returns the ``PYTHON_EGG_CACHE`` environment variable, 1656 if set. Otherwise, on Windows, it returns a "Python-Eggs" subdirectory of 1657 the user's "Application Data" directory. On all other systems, it returns 1658 ``os.path.expanduser("~/.python-eggs")`` if ``PYTHON_EGG_CACHE`` is not 1659 set. 1660 1661 1662PEP 302 Utilities 1663----------------- 1664 1665``get_importer(path_item)`` 1666 A deprecated alias for ``pkgutil.get_importer()`` 1667 1668 1669File/Path Utilities 1670------------------- 1671 1672``ensure_directory(path)`` 1673 Ensure that the parent directory (``os.path.dirname``) of ``path`` actually 1674 exists, using ``os.makedirs()`` if necessary. 1675 1676``normalize_path(path)`` 1677 Return a "normalized" version of ``path``, such that two paths represent 1678 the same filesystem location if they have equal ``normalized_path()`` 1679 values. Specifically, this is a shortcut for calling ``os.path.realpath`` 1680 and ``os.path.normcase`` on ``path``. Unfortunately, on certain platforms 1681 (notably Cygwin and macOS) the ``normcase`` function does not accurately 1682 reflect the platform's case-sensitivity, so there is always the possibility 1683 of two apparently-different paths being equal on such platforms. 1684 1685History 1686------- 1687 16880.6c9 1689 * Fix ``resource_listdir('')`` always returning an empty list for zipped eggs. 1690 16910.6c7 1692 * Fix package precedence problem where single-version eggs installed in 1693 ``site-packages`` would take precedence over ``.egg`` files (or directories) 1694 installed in ``site-packages``. 1695 16960.6c6 1697 * Fix extracted C extensions not having executable permissions under Cygwin. 1698 1699 * Allow ``.egg-link`` files to contain relative paths. 1700 1701 * Fix cache dir defaults on Windows when multiple environment vars are needed 1702 to construct a path. 1703 17040.6c4 1705 * Fix "dev" versions being considered newer than release candidates. 1706 17070.6c3 1708 * Python 2.5 compatibility fixes. 1709 17100.6c2 1711 * Fix a problem with eggs specified directly on ``PYTHONPATH`` on 1712 case-insensitive filesystems possibly not showing up in the default 1713 working set, due to differing normalizations of ``sys.path`` entries. 1714 17150.6b3 1716 * Fixed a duplicate path insertion problem on case-insensitive filesystems. 1717 17180.6b1 1719 * Split ``get_platform()`` into ``get_supported_platform()`` and 1720 ``get_build_platform()`` to work around a Mac versioning problem that caused 1721 the behavior of ``compatible_platforms()`` to be platform specific. 1722 1723 * Fix entry point parsing when a standalone module name has whitespace 1724 between it and the extras. 1725 17260.6a11 1727 * Added ``ExtractionError`` and ``ResourceManager.extraction_error()`` so that 1728 cache permission problems get a more user-friendly explanation of the 1729 problem, and so that programs can catch and handle extraction errors if they 1730 need to. 1731 17320.6a10 1733 * Added the ``extras`` attribute to ``Distribution``, the ``find_plugins()`` 1734 method to ``WorkingSet``, and the ``__add__()`` and ``__iadd__()`` methods 1735 to ``Environment``. 1736 1737 * ``safe_name()`` now allows dots in project names. 1738 1739 * There is a new ``to_filename()`` function that escapes project names and 1740 versions for safe use in constructing egg filenames from a Distribution 1741 object's metadata. 1742 1743 * Added ``Distribution.clone()`` method, and keyword argument support to other 1744 ``Distribution`` constructors. 1745 1746 * Added the ``DEVELOP_DIST`` precedence, and automatically assign it to 1747 eggs using ``.egg-info`` format. 1748 17490.6a9 1750 * Don't raise an error when an invalid (unfinished) distribution is found 1751 unless absolutely necessary. Warn about skipping invalid/unfinished eggs 1752 when building an Environment. 1753 1754 * Added support for ``.egg-info`` files or directories with version/platform 1755 information embedded in the filename, so that system packagers have the 1756 option of including ``PKG-INFO`` files to indicate the presence of a 1757 system-installed egg, without needing to use ``.egg`` directories, zipfiles, 1758 or ``.pth`` manipulation. 1759 1760 * Changed ``parse_version()`` to remove dashes before pre-release tags, so 1761 that ``0.2-rc1`` is considered an *older* version than ``0.2``, and is equal 1762 to ``0.2rc1``. The idea that a dash *always* meant a post-release version 1763 was highly non-intuitive to setuptools users and Python developers, who 1764 seem to want to use ``-rc`` version numbers a lot. 1765 17660.6a8 1767 * Fixed a problem with ``WorkingSet.resolve()`` that prevented version 1768 conflicts from being detected at runtime. 1769 1770 * Improved runtime conflict warning message to identify a line in the user's 1771 program, rather than flagging the ``warn()`` call in ``pkg_resources``. 1772 1773 * Avoid giving runtime conflict warnings for namespace packages, even if they 1774 were declared by a different package than the one currently being activated. 1775 1776 * Fix path insertion algorithm for case-insensitive filesystems. 1777 1778 * Fixed a problem with nested namespace packages (e.g. ``peak.util``) not 1779 being set as an attribute of their parent package. 1780 17810.6a6 1782 * Activated distributions are now inserted in ``sys.path`` (and the working 1783 set) just before the directory that contains them, instead of at the end. 1784 This allows e.g. eggs in ``site-packages`` to override unmanaged modules in 1785 the same location, and allows eggs found earlier on ``sys.path`` to override 1786 ones found later. 1787 1788 * When a distribution is activated, it now checks whether any contained 1789 non-namespace modules have already been imported and issues a warning if 1790 a conflicting module has already been imported. 1791 1792 * Changed dependency processing so that it's breadth-first, allowing a 1793 depender's preferences to override those of a dependee, to prevent conflicts 1794 when a lower version is acceptable to the dependee, but not the depender. 1795 1796 * Fixed a problem extracting zipped files on Windows, when the egg in question 1797 has had changed contents but still has the same version number. 1798 17990.6a4 1800 * Fix a bug in ``WorkingSet.resolve()`` that was introduced in 0.6a3. 1801 18020.6a3 1803 * Added ``safe_extra()`` parsing utility routine, and use it for Requirement, 1804 EntryPoint, and Distribution objects' extras handling. 1805 18060.6a1 1807 * Enhanced performance of ``require()`` and related operations when all 1808 requirements are already in the working set, and enhanced performance of 1809 directory scanning for distributions. 1810 1811 * Fixed some problems using ``pkg_resources`` w/PEP 302 loaders other than 1812 ``zipimport``, and the previously-broken "eager resource" support. 1813 1814 * Fixed ``pkg_resources.resource_exists()`` not working correctly, along with 1815 some other resource API bugs. 1816 1817 * Many API changes and enhancements: 1818 1819 * Added ``EntryPoint``, ``get_entry_map``, ``load_entry_point``, and 1820 ``get_entry_info`` APIs for dynamic plugin discovery. 1821 1822 * ``list_resources`` is now ``resource_listdir`` (and it actually works) 1823 1824 * Resource API functions like ``resource_string()`` that accepted a package 1825 name and resource name, will now also accept a ``Requirement`` object in 1826 place of the package name (to allow access to non-package data files in 1827 an egg). 1828 1829 * ``get_provider()`` will now accept a ``Requirement`` instance or a module 1830 name. If it is given a ``Requirement``, it will return a corresponding 1831 ``Distribution`` (by calling ``require()`` if a suitable distribution 1832 isn't already in the working set), rather than returning a metadata and 1833 resource provider for a specific module. (The difference is in how 1834 resource paths are interpreted; supplying a module name means resources 1835 path will be module-relative, rather than relative to the distribution's 1836 root.) 1837 1838 * ``Distribution`` objects now implement the ``IResourceProvider`` and 1839 ``IMetadataProvider`` interfaces, so you don't need to reference the (no 1840 longer available) ``metadata`` attribute to get at these interfaces. 1841 1842 * ``Distribution`` and ``Requirement`` both have a ``project_name`` 1843 attribute for the project name they refer to. (Previously these were 1844 ``name`` and ``distname`` attributes.) 1845 1846 * The ``path`` attribute of ``Distribution`` objects is now ``location``, 1847 because it isn't necessarily a filesystem path (and hasn't been for some 1848 time now). The ``location`` of ``Distribution`` objects in the filesystem 1849 should always be normalized using ``pkg_resources.normalize_path()``; all 1850 of the setuptools' code that generates distributions from the filesystem 1851 (including ``Distribution.from_filename()``) ensure this invariant, but if 1852 you use a more generic API like ``Distribution()`` or 1853 ``Distribution.from_location()`` you should take care that you don't 1854 create a distribution with an un-normalized filesystem path. 1855 1856 * ``Distribution`` objects now have an ``as_requirement()`` method that 1857 returns a ``Requirement`` for the distribution's project name and version. 1858 1859 * Distribution objects no longer have an ``installed_on()`` method, and the 1860 ``install_on()`` method is now ``activate()`` (but may go away altogether 1861 soon). The ``depends()`` method has also been renamed to ``requires()``, 1862 and ``InvalidOption`` is now ``UnknownExtra``. 1863 1864 * ``find_distributions()`` now takes an additional argument called ``only``, 1865 that tells it to only yield distributions whose location is the passed-in 1866 path. (It defaults to False, so that the default behavior is unchanged.) 1867 1868 * ``AvailableDistributions`` is now called ``Environment``, and the 1869 ``get()``, ``__len__()``, and ``__contains__()`` methods were removed, 1870 because they weren't particularly useful. ``__getitem__()`` no longer 1871 raises ``KeyError``; it just returns an empty list if there are no 1872 distributions for the named project. 1873 1874 * The ``resolve()`` method of ``Environment`` is now a method of 1875 ``WorkingSet`` instead, and the ``best_match()`` method now uses a working 1876 set instead of a path list as its second argument. 1877 1878 * There is a new ``pkg_resources.add_activation_listener()`` API that lets 1879 you register a callback for notifications about distributions added to 1880 ``sys.path`` (including the distributions already on it). This is 1881 basically a hook for extensible applications and frameworks to be able to 1882 search for plugin metadata in distributions added at runtime. 1883 18840.5a13 1885 * Fixed a bug in resource extraction from nested packages in a zipped egg. 1886 18870.5a12 1888 * Updated extraction/cache mechanism for zipped resources to avoid inter- 1889 process and inter-thread races during extraction. The default cache 1890 location can now be set via the ``PYTHON_EGGS_CACHE`` environment variable, 1891 and the default Windows cache is now a ``Python-Eggs`` subdirectory of the 1892 current user's "Application Data" directory, if the ``PYTHON_EGGS_CACHE`` 1893 variable isn't set. 1894 18950.5a10 1896 * Fix a problem with ``pkg_resources`` being confused by non-existent eggs on 1897 ``sys.path`` (e.g. if a user deletes an egg without removing it from the 1898 ``easy-install.pth`` file). 1899 1900 * Fix a problem with "basket" support in ``pkg_resources``, where egg-finding 1901 never actually went inside ``.egg`` files. 1902 1903 * Made ``pkg_resources`` import the module you request resources from, if it's 1904 not already imported. 1905 19060.5a4 1907 * ``pkg_resources.AvailableDistributions.resolve()`` and related methods now 1908 accept an ``installer`` argument: a callable taking one argument, a 1909 ``Requirement`` instance. The callable must return a ``Distribution`` 1910 object, or ``None`` if no distribution is found. This feature is used by 1911 EasyInstall to resolve dependencies by recursively invoking itself. 1912 19130.4a4 1914 * Fix problems with ``resource_listdir()``, ``resource_isdir()`` and resource 1915 directory extraction for zipped eggs. 1916 19170.4a3 1918 * Fixed scripts not being able to see a ``__file__`` variable in ``__main__`` 1919 1920 * Fixed a problem with ``resource_isdir()`` implementation that was introduced 1921 in 0.4a2. 1922 19230.4a1 1924 * Fixed a bug in requirements processing for exact versions (i.e. ``==`` and 1925 ``!=``) when only one condition was included. 1926 1927 * Added ``safe_name()`` and ``safe_version()`` APIs to clean up handling of 1928 arbitrary distribution names and versions found on PyPI. 1929 19300.3a4 1931 * ``pkg_resources`` now supports resource directories, not just the resources 1932 in them. In particular, there are ``resource_listdir()`` and 1933 ``resource_isdir()`` APIs. 1934 1935 * ``pkg_resources`` now supports "egg baskets" -- .egg zipfiles which contain 1936 multiple distributions in subdirectories whose names end with ``.egg``. 1937 Having such a "basket" in a directory on ``sys.path`` is equivalent to 1938 having the individual eggs in that directory, but the contained eggs can 1939 be individually added (or not) to ``sys.path``. Currently, however, there 1940 is no automated way to create baskets. 1941 1942 * Namespace package manipulation is now protected by the Python import lock. 1943 19440.3a1 1945 * Initial release. 1946