pkg_resources.txt 92 KB

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