setuptools.txt 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663
  1. ==================================================
  2. Building and Distributing Packages with Setuptools
  3. ==================================================
  4. ``Setuptools`` is a collection of enhancements to the Python ``distutils``
  5. (for Python 2.6 and up) that allow developers to more easily build and
  6. distribute Python packages, especially ones that have dependencies on other
  7. packages.
  8. Packages built and distributed using ``setuptools`` look to the user like
  9. ordinary Python packages based on the ``distutils``. Your users don't need to
  10. install or even know about setuptools in order to use them, and you don't
  11. have to include the entire setuptools package in your distributions. By
  12. including just a single `bootstrap module`_ (a 12K .py file), your package will
  13. automatically download and install ``setuptools`` if the user is building your
  14. package from source and doesn't have a suitable version already installed.
  15. .. _bootstrap module: https://bootstrap.pypa.io/ez_setup.py
  16. Feature Highlights:
  17. * Automatically find/download/install/upgrade dependencies at build time using
  18. the `EasyInstall tool <easy_install.html>`_,
  19. which supports downloading via HTTP, FTP, Subversion, and SourceForge, and
  20. automatically scans web pages linked from PyPI to find download links. (It's
  21. the closest thing to CPAN currently available for Python.)
  22. * Create `Python Eggs <http://peak.telecommunity.com/DevCenter/PythonEggs>`_ -
  23. a single-file importable distribution format
  24. * Enhanced support for accessing data files hosted in zipped packages.
  25. * Automatically include all packages in your source tree, without listing them
  26. individually in setup.py
  27. * Automatically include all relevant files in your source distributions,
  28. without needing to create a ``MANIFEST.in`` file, and without having to force
  29. regeneration of the ``MANIFEST`` file when your source tree changes.
  30. * Automatically generate wrapper scripts or Windows (console and GUI) .exe
  31. files for any number of "main" functions in your project. (Note: this is not
  32. a py2exe replacement; the .exe files rely on the local Python installation.)
  33. * Transparent Pyrex support, so that your setup.py can list ``.pyx`` files and
  34. still work even when the end-user doesn't have Pyrex installed (as long as
  35. you include the Pyrex-generated C in your source distribution)
  36. * Command aliases - create project-specific, per-user, or site-wide shortcut
  37. names for commonly used commands and options
  38. * PyPI upload support - upload your source distributions and eggs to PyPI
  39. * Deploy your project in "development mode", such that it's available on
  40. ``sys.path``, yet can still be edited directly from its source checkout.
  41. * Easily extend the distutils with new commands or ``setup()`` arguments, and
  42. distribute/reuse your extensions for multiple projects, without copying code.
  43. * Create extensible applications and frameworks that automatically discover
  44. extensions, using simple "entry points" declared in a project's setup script.
  45. In addition to the PyPI downloads, the development version of ``setuptools``
  46. is available from the `Python SVN sandbox`_, and in-development versions of the
  47. `0.6 branch`_ are available as well.
  48. .. _0.6 branch: http://svn.python.org/projects/sandbox/branches/setuptools-0.6/#egg=setuptools-dev06
  49. .. _Python SVN sandbox: http://svn.python.org/projects/sandbox/trunk/setuptools/#egg=setuptools-dev
  50. .. contents:: **Table of Contents**
  51. .. _ez_setup.py: `bootstrap module`_
  52. -----------------
  53. Developer's Guide
  54. -----------------
  55. Installing ``setuptools``
  56. =========================
  57. Please follow the `EasyInstall Installation Instructions`_ to install the
  58. current stable version of setuptools. In particular, be sure to read the
  59. section on `Custom Installation Locations`_ if you are installing anywhere
  60. other than Python's ``site-packages`` directory.
  61. .. _EasyInstall Installation Instructions: easy_install.html#installation-instructions
  62. .. _Custom Installation Locations: easy_install.html#custom-installation-locations
  63. If you want the current in-development version of setuptools, you should first
  64. install a stable version, and then run::
  65. ez_setup.py setuptools==dev
  66. This will download and install the latest development (i.e. unstable) version
  67. of setuptools from the Python Subversion sandbox.
  68. Basic Use
  69. =========
  70. For basic use of setuptools, just import things from setuptools instead of
  71. the distutils. Here's a minimal setup script using setuptools::
  72. from setuptools import setup, find_packages
  73. setup(
  74. name = "HelloWorld",
  75. version = "0.1",
  76. packages = find_packages(),
  77. )
  78. As you can see, it doesn't take much to use setuptools in a project.
  79. Run that script in your project folder, alongside the Python packages
  80. you have developed.
  81. Invoke that script to produce eggs, upload to
  82. PyPI, and automatically include all packages in the directory where the
  83. setup.py lives. See the `Command Reference`_ section below to see what
  84. commands you can give to this setup script. For example,
  85. to produce a source distribution, simply invoke::
  86. python setup.py sdist
  87. Of course, before you release your project to PyPI, you'll want to add a bit
  88. more information to your setup script to help people find or learn about your
  89. project. And maybe your project will have grown by then to include a few
  90. dependencies, and perhaps some data files and scripts::
  91. from setuptools import setup, find_packages
  92. setup(
  93. name = "HelloWorld",
  94. version = "0.1",
  95. packages = find_packages(),
  96. scripts = ['say_hello.py'],
  97. # Project uses reStructuredText, so ensure that the docutils get
  98. # installed or upgraded on the target machine
  99. install_requires = ['docutils>=0.3'],
  100. package_data = {
  101. # If any package contains *.txt or *.rst files, include them:
  102. '': ['*.txt', '*.rst'],
  103. # And include any *.msg files found in the 'hello' package, too:
  104. 'hello': ['*.msg'],
  105. },
  106. # metadata for upload to PyPI
  107. author = "Me",
  108. author_email = "me@example.com",
  109. description = "This is an Example Package",
  110. license = "PSF",
  111. keywords = "hello world example examples",
  112. url = "http://example.com/HelloWorld/", # project home page, if any
  113. # could also include long_description, download_url, classifiers, etc.
  114. )
  115. In the sections that follow, we'll explain what most of these ``setup()``
  116. arguments do (except for the metadata ones), and the various ways you might use
  117. them in your own project(s).
  118. Specifying Your Project's Version
  119. ---------------------------------
  120. Setuptools can work well with most versioning schemes; there are, however, a
  121. few special things to watch out for, in order to ensure that setuptools and
  122. EasyInstall can always tell what version of your package is newer than another
  123. version. Knowing these things will also help you correctly specify what
  124. versions of other projects your project depends on.
  125. A version consists of an alternating series of release numbers and pre-release
  126. or post-release tags. A release number is a series of digits punctuated by
  127. dots, such as ``2.4`` or ``0.5``. Each series of digits is treated
  128. numerically, so releases ``2.1`` and ``2.1.0`` are different ways to spell the
  129. same release number, denoting the first subrelease of release 2. But ``2.10``
  130. is the *tenth* subrelease of release 2, and so is a different and newer release
  131. from ``2.1`` or ``2.1.0``. Leading zeros within a series of digits are also
  132. ignored, so ``2.01`` is the same as ``2.1``, and different from ``2.0.1``.
  133. Following a release number, you can have either a pre-release or post-release
  134. tag. Pre-release tags make a version be considered *older* than the version
  135. they are appended to. So, revision ``2.4`` is *newer* than revision ``2.4c1``,
  136. which in turn is newer than ``2.4b1`` or ``2.4a1``. Postrelease tags make
  137. a version be considered *newer* than the version they are appended to. So,
  138. revisions like ``2.4-1`` and ``2.4pl3`` are newer than ``2.4``, but are *older*
  139. than ``2.4.1`` (which has a higher release number).
  140. A pre-release tag is a series of letters that are alphabetically before
  141. "final". Some examples of prerelease tags would include ``alpha``, ``beta``,
  142. ``a``, ``c``, ``dev``, and so on. You do not have to place a dot or dash
  143. before the prerelease tag if it's immediately after a number, but it's okay to
  144. do so if you prefer. Thus, ``2.4c1`` and ``2.4.c1`` and ``2.4-c1`` all
  145. represent release candidate 1 of version ``2.4``, and are treated as identical
  146. by setuptools.
  147. In addition, there are three special prerelease tags that are treated as if
  148. they were the letter ``c``: ``pre``, ``preview``, and ``rc``. So, version
  149. ``2.4rc1``, ``2.4pre1`` and ``2.4preview1`` are all the exact same version as
  150. ``2.4c1``, and are treated as identical by setuptools.
  151. A post-release tag is either a series of letters that are alphabetically
  152. greater than or equal to "final", or a dash (``-``). Post-release tags are
  153. generally used to separate patch numbers, port numbers, build numbers, revision
  154. numbers, or date stamps from the release number. For example, the version
  155. ``2.4-r1263`` might denote Subversion revision 1263 of a post-release patch of
  156. version ``2.4``. Or you might use ``2.4-20051127`` to denote a date-stamped
  157. post-release.
  158. Notice that after each pre or post-release tag, you are free to place another
  159. release number, followed again by more pre- or post-release tags. For example,
  160. ``0.6a9.dev-r41475`` could denote Subversion revision 41475 of the in-
  161. development version of the ninth alpha of release 0.6. Notice that ``dev`` is
  162. a pre-release tag, so this version is a *lower* version number than ``0.6a9``,
  163. which would be the actual ninth alpha of release 0.6. But the ``-r41475`` is
  164. a post-release tag, so this version is *newer* than ``0.6a9.dev``.
  165. For the most part, setuptools' interpretation of version numbers is intuitive,
  166. but here are a few tips that will keep you out of trouble in the corner cases:
  167. * Don't stick adjoining pre-release tags together without a dot or number
  168. between them. Version ``1.9adev`` is the ``adev`` prerelease of ``1.9``,
  169. *not* a development pre-release of ``1.9a``. Use ``.dev`` instead, as in
  170. ``1.9a.dev``, or separate the prerelease tags with a number, as in
  171. ``1.9a0dev``. ``1.9a.dev``, ``1.9a0dev``, and even ``1.9.a.dev`` are
  172. identical versions from setuptools' point of view, so you can use whatever
  173. scheme you prefer.
  174. * If you want to be certain that your chosen numbering scheme works the way
  175. you think it will, you can use the ``pkg_resources.parse_version()`` function
  176. to compare different version numbers::
  177. >>> from pkg_resources import parse_version
  178. >>> parse_version('1.9.a.dev') == parse_version('1.9a0dev')
  179. True
  180. >>> parse_version('2.1-rc2') < parse_version('2.1')
  181. True
  182. >>> parse_version('0.6a9dev-r41475') < parse_version('0.6a9')
  183. True
  184. Once you've decided on a version numbering scheme for your project, you can
  185. have setuptools automatically tag your in-development releases with various
  186. pre- or post-release tags. See the following sections for more details:
  187. * `Tagging and "Daily Build" or "Snapshot" Releases`_
  188. * `Managing "Continuous Releases" Using Subversion`_
  189. * The `egg_info`_ command
  190. New and Changed ``setup()`` Keywords
  191. ====================================
  192. The following keyword arguments to ``setup()`` are added or changed by
  193. ``setuptools``. All of them are optional; you do not have to supply them
  194. unless you need the associated ``setuptools`` feature.
  195. ``include_package_data``
  196. If set to ``True``, this tells ``setuptools`` to automatically include any
  197. data files it finds inside your package directories that are specified by
  198. your ``MANIFEST.in`` file. For more information, see the section below on
  199. `Including Data Files`_.
  200. ``exclude_package_data``
  201. A dictionary mapping package names to lists of glob patterns that should
  202. be *excluded* from your package directories. You can use this to trim back
  203. any excess files included by ``include_package_data``. For a complete
  204. description and examples, see the section below on `Including Data Files`_.
  205. ``package_data``
  206. A dictionary mapping package names to lists of glob patterns. For a
  207. complete description and examples, see the section below on `Including
  208. Data Files`_. You do not need to use this option if you are using
  209. ``include_package_data``, unless you need to add e.g. files that are
  210. generated by your setup script and build process. (And are therefore not
  211. in source control or are files that you don't want to include in your
  212. source distribution.)
  213. ``zip_safe``
  214. A boolean (True or False) flag specifying whether the project can be
  215. safely installed and run from a zip file. If this argument is not
  216. supplied, the ``bdist_egg`` command will have to analyze all of your
  217. project's contents for possible problems each time it builds an egg.
  218. ``install_requires``
  219. A string or list of strings specifying what other distributions need to
  220. be installed when this one is. See the section below on `Declaring
  221. Dependencies`_ for details and examples of the format of this argument.
  222. ``entry_points``
  223. A dictionary mapping entry point group names to strings or lists of strings
  224. defining the entry points. Entry points are used to support dynamic
  225. discovery of services or plugins provided by a project. See `Dynamic
  226. Discovery of Services and Plugins`_ for details and examples of the format
  227. of this argument. In addition, this keyword is used to support `Automatic
  228. Script Creation`_.
  229. ``extras_require``
  230. A dictionary mapping names of "extras" (optional features of your project)
  231. to strings or lists of strings specifying what other distributions must be
  232. installed to support those features. See the section below on `Declaring
  233. Dependencies`_ for details and examples of the format of this argument.
  234. ``setup_requires``
  235. A string or list of strings specifying what other distributions need to
  236. be present in order for the *setup script* to run. ``setuptools`` will
  237. attempt to obtain these (even going so far as to download them using
  238. ``EasyInstall``) before processing the rest of the setup script or commands.
  239. This argument is needed if you are using distutils extensions as part of
  240. your build process; for example, extensions that process setup() arguments
  241. and turn them into EGG-INFO metadata files.
  242. (Note: projects listed in ``setup_requires`` will NOT be automatically
  243. installed on the system where the setup script is being run. They are
  244. simply downloaded to the ./.eggs directory if they're not locally available
  245. already. If you want them to be installed, as well as being available
  246. when the setup script is run, you should add them to ``install_requires``
  247. **and** ``setup_requires``.)
  248. ``dependency_links``
  249. A list of strings naming URLs to be searched when satisfying dependencies.
  250. These links will be used if needed to install packages specified by
  251. ``setup_requires`` or ``tests_require``. They will also be written into
  252. the egg's metadata for use by tools like EasyInstall to use when installing
  253. an ``.egg`` file.
  254. ``namespace_packages``
  255. A list of strings naming the project's "namespace packages". A namespace
  256. package is a package that may be split across multiple project
  257. distributions. For example, Zope 3's ``zope`` package is a namespace
  258. package, because subpackages like ``zope.interface`` and ``zope.publisher``
  259. may be distributed separately. The egg runtime system can automatically
  260. merge such subpackages into a single parent package at runtime, as long
  261. as you declare them in each project that contains any subpackages of the
  262. namespace package, and as long as the namespace package's ``__init__.py``
  263. does not contain any code other than a namespace declaration. See the
  264. section below on `Namespace Packages`_ for more information.
  265. ``test_suite``
  266. A string naming a ``unittest.TestCase`` subclass (or a package or module
  267. containing one or more of them, or a method of such a subclass), or naming
  268. a function that can be called with no arguments and returns a
  269. ``unittest.TestSuite``. If the named suite is a module, and the module
  270. has an ``additional_tests()`` function, it is called and the results are
  271. added to the tests to be run. If the named suite is a package, any
  272. submodules and subpackages are recursively added to the overall test suite.
  273. Specifying this argument enables use of the `test`_ command to run the
  274. specified test suite, e.g. via ``setup.py test``. See the section on the
  275. `test`_ command below for more details.
  276. ``tests_require``
  277. If your project's tests need one or more additional packages besides those
  278. needed to install it, you can use this option to specify them. It should
  279. be a string or list of strings specifying what other distributions need to
  280. be present for the package's tests to run. When you run the ``test``
  281. command, ``setuptools`` will attempt to obtain these (even going
  282. so far as to download them using ``EasyInstall``). Note that these
  283. required projects will *not* be installed on the system where the tests
  284. are run, but only downloaded to the project's setup directory if they're
  285. not already installed locally.
  286. .. _test_loader:
  287. ``test_loader``
  288. If you would like to use a different way of finding tests to run than what
  289. setuptools normally uses, you can specify a module name and class name in
  290. this argument. The named class must be instantiable with no arguments, and
  291. its instances must support the ``loadTestsFromNames()`` method as defined
  292. in the Python ``unittest`` module's ``TestLoader`` class. Setuptools will
  293. pass only one test "name" in the `names` argument: the value supplied for
  294. the ``test_suite`` argument. The loader you specify may interpret this
  295. string in any way it likes, as there are no restrictions on what may be
  296. contained in a ``test_suite`` string.
  297. The module name and class name must be separated by a ``:``. The default
  298. value of this argument is ``"setuptools.command.test:ScanningLoader"``. If
  299. you want to use the default ``unittest`` behavior, you can specify
  300. ``"unittest:TestLoader"`` as your ``test_loader`` argument instead. This
  301. will prevent automatic scanning of submodules and subpackages.
  302. The module and class you specify here may be contained in another package,
  303. as long as you use the ``tests_require`` option to ensure that the package
  304. containing the loader class is available when the ``test`` command is run.
  305. ``eager_resources``
  306. A list of strings naming resources that should be extracted together, if
  307. any of them is needed, or if any C extensions included in the project are
  308. imported. This argument is only useful if the project will be installed as
  309. a zipfile, and there is a need to have all of the listed resources be
  310. extracted to the filesystem *as a unit*. Resources listed here
  311. should be '/'-separated paths, relative to the source root, so to list a
  312. resource ``foo.png`` in package ``bar.baz``, you would include the string
  313. ``bar/baz/foo.png`` in this argument.
  314. If you only need to obtain resources one at a time, or you don't have any C
  315. extensions that access other files in the project (such as data files or
  316. shared libraries), you probably do NOT need this argument and shouldn't
  317. mess with it. For more details on how this argument works, see the section
  318. below on `Automatic Resource Extraction`_.
  319. ``use_2to3``
  320. Convert the source code from Python 2 to Python 3 with 2to3 during the
  321. build process. See :doc:`python3` for more details.
  322. ``convert_2to3_doctests``
  323. List of doctest source files that need to be converted with 2to3.
  324. See :doc:`python3` for more details.
  325. ``use_2to3_fixers``
  326. A list of modules to search for additional fixers to be used during
  327. the 2to3 conversion. See :doc:`python3` for more details.
  328. Using ``find_packages()``
  329. -------------------------
  330. For simple projects, it's usually easy enough to manually add packages to
  331. the ``packages`` argument of ``setup()``. However, for very large projects
  332. (Twisted, PEAK, Zope, Chandler, etc.), it can be a big burden to keep the
  333. package list updated. That's what ``setuptools.find_packages()`` is for.
  334. ``find_packages()`` takes a source directory and two lists of package name
  335. patterns to exclude and include. If omitted, the source directory defaults to
  336. the same
  337. directory as the setup script. Some projects use a ``src`` or ``lib``
  338. directory as the root of their source tree, and those projects would of course
  339. use ``"src"`` or ``"lib"`` as the first argument to ``find_packages()``. (And
  340. such projects also need something like ``package_dir = {'':'src'}`` in their
  341. ``setup()`` arguments, but that's just a normal distutils thing.)
  342. Anyway, ``find_packages()`` walks the target directory, filtering by inclusion
  343. patterns, and finds Python packages (any directory). On Python 3.2 and
  344. earlier, packages are only recognized if they include an ``__init__.py`` file.
  345. Finally, exclusion patterns are applied to remove matching packages.
  346. Inclusion and exclusion patterns are package names, optionally including
  347. wildcards. For
  348. example, ``find_packages(exclude=["*.tests"])`` will exclude all packages whose
  349. last name part is ``tests``. Or, ``find_packages(exclude=["*.tests",
  350. "*.tests.*"])`` will also exclude any subpackages of packages named ``tests``,
  351. but it still won't exclude a top-level ``tests`` package or the children
  352. thereof. In fact, if you really want no ``tests`` packages at all, you'll need
  353. something like this::
  354. find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"])
  355. in order to cover all the bases. Really, the exclusion patterns are intended
  356. to cover simpler use cases than this, like excluding a single, specified
  357. package and its subpackages.
  358. Regardless of the parameters, the ``find_packages()``
  359. function returns a list of package names suitable for use as the ``packages``
  360. argument to ``setup()``, and so is usually the easiest way to set that
  361. argument in your setup script. Especially since it frees you from having to
  362. remember to modify your setup script whenever your project grows additional
  363. top-level packages or subpackages.
  364. Automatic Script Creation
  365. =========================
  366. Packaging and installing scripts can be a bit awkward with the distutils. For
  367. one thing, there's no easy way to have a script's filename match local
  368. conventions on both Windows and POSIX platforms. For another, you often have
  369. to create a separate file just for the "main" script, when your actual "main"
  370. is a function in a module somewhere. And even in Python 2.4, using the ``-m``
  371. option only works for actual ``.py`` files that aren't installed in a package.
  372. ``setuptools`` fixes all of these problems by automatically generating scripts
  373. for you with the correct extension, and on Windows it will even create an
  374. ``.exe`` file so that users don't have to change their ``PATHEXT`` settings.
  375. The way to use this feature is to define "entry points" in your setup script
  376. that indicate what function the generated script should import and run. For
  377. example, to create two console scripts called ``foo`` and ``bar``, and a GUI
  378. script called ``baz``, you might do something like this::
  379. setup(
  380. # other arguments here...
  381. entry_points={
  382. 'console_scripts': [
  383. 'foo = my_package.some_module:main_func',
  384. 'bar = other_module:some_func',
  385. ],
  386. 'gui_scripts': [
  387. 'baz = my_package_gui:start_func',
  388. ]
  389. }
  390. )
  391. When this project is installed on non-Windows platforms (using "setup.py
  392. install", "setup.py develop", or by using EasyInstall), a set of ``foo``,
  393. ``bar``, and ``baz`` scripts will be installed that import ``main_func`` and
  394. ``some_func`` from the specified modules. The functions you specify are called
  395. with no arguments, and their return value is passed to ``sys.exit()``, so you
  396. can return an errorlevel or message to print to stderr.
  397. On Windows, a set of ``foo.exe``, ``bar.exe``, and ``baz.exe`` launchers are
  398. created, alongside a set of ``foo.py``, ``bar.py``, and ``baz.pyw`` files. The
  399. ``.exe`` wrappers find and execute the right version of Python to run the
  400. ``.py`` or ``.pyw`` file.
  401. You may define as many "console script" and "gui script" entry points as you
  402. like, and each one can optionally specify "extras" that it depends on, that
  403. will be added to ``sys.path`` when the script is run. For more information on
  404. "extras", see the section below on `Declaring Extras`_. For more information
  405. on "entry points" in general, see the section below on `Dynamic Discovery of
  406. Services and Plugins`_.
  407. "Eggsecutable" Scripts
  408. ----------------------
  409. Occasionally, there are situations where it's desirable to make an ``.egg``
  410. file directly executable. You can do this by including an entry point such
  411. as the following::
  412. setup(
  413. # other arguments here...
  414. entry_points = {
  415. 'setuptools.installation': [
  416. 'eggsecutable = my_package.some_module:main_func',
  417. ]
  418. }
  419. )
  420. Any eggs built from the above setup script will include a short executable
  421. prelude that imports and calls ``main_func()`` from ``my_package.some_module``.
  422. The prelude can be run on Unix-like platforms (including Mac and Linux) by
  423. invoking the egg with ``/bin/sh``, or by enabling execute permissions on the
  424. ``.egg`` file. For the executable prelude to run, the appropriate version of
  425. Python must be available via the ``PATH`` environment variable, under its
  426. "long" name. That is, if the egg is built for Python 2.3, there must be a
  427. ``python2.3`` executable present in a directory on ``PATH``.
  428. This feature is primarily intended to support ez_setup the installation of
  429. setuptools itself on non-Windows platforms, but may also be useful for other
  430. projects as well.
  431. IMPORTANT NOTE: Eggs with an "eggsecutable" header cannot be renamed, or
  432. invoked via symlinks. They *must* be invoked using their original filename, in
  433. order to ensure that, once running, ``pkg_resources`` will know what project
  434. and version is in use. The header script will check this and exit with an
  435. error if the ``.egg`` file has been renamed or is invoked via a symlink that
  436. changes its base name.
  437. Declaring Dependencies
  438. ======================
  439. ``setuptools`` supports automatically installing dependencies when a package is
  440. installed, and including information about dependencies in Python Eggs (so that
  441. package management tools like EasyInstall can use the information).
  442. ``setuptools`` and ``pkg_resources`` use a common syntax for specifying a
  443. project's required dependencies. This syntax consists of a project's PyPI
  444. name, optionally followed by a comma-separated list of "extras" in square
  445. brackets, optionally followed by a comma-separated list of version
  446. specifiers. A version specifier is one of the operators ``<``, ``>``, ``<=``,
  447. ``>=``, ``==`` or ``!=``, followed by a version identifier. Tokens may be
  448. separated by whitespace, but any whitespace or nonstandard characters within a
  449. project name or version identifier must be replaced with ``-``.
  450. Version specifiers for a given project are internally sorted into ascending
  451. version order, and used to establish what ranges of versions are acceptable.
  452. Adjacent redundant conditions are also consolidated (e.g. ``">1, >2"`` becomes
  453. ``">1"``, and ``"<2,<3"`` becomes ``"<3"``). ``"!="`` versions are excised from
  454. the ranges they fall within. A project's version is then checked for
  455. membership in the resulting ranges. (Note that providing conflicting conditions
  456. for the same version (e.g. "<2,>=2" or "==2,!=2") is meaningless and may
  457. therefore produce bizarre results.)
  458. Here are some example requirement specifiers::
  459. docutils >= 0.3
  460. # comment lines and \ continuations are allowed in requirement strings
  461. BazSpam ==1.1, ==1.2, ==1.3, ==1.4, ==1.5, \
  462. ==1.6, ==1.7 # and so are line-end comments
  463. PEAK[FastCGI, reST]>=0.5a4
  464. setuptools==0.5a7
  465. The simplest way to include requirement specifiers is to use the
  466. ``install_requires`` argument to ``setup()``. It takes a string or list of
  467. strings containing requirement specifiers. If you include more than one
  468. requirement in a string, each requirement must begin on a new line.
  469. This has three effects:
  470. 1. When your project is installed, either by using EasyInstall, ``setup.py
  471. install``, or ``setup.py develop``, all of the dependencies not already
  472. installed will be located (via PyPI), downloaded, built (if necessary),
  473. and installed.
  474. 2. Any scripts in your project will be installed with wrappers that verify
  475. the availability of the specified dependencies at runtime, and ensure that
  476. the correct versions are added to ``sys.path`` (e.g. if multiple versions
  477. have been installed).
  478. 3. Python Egg distributions will include a metadata file listing the
  479. dependencies.
  480. Note, by the way, that if you declare your dependencies in ``setup.py``, you do
  481. *not* need to use the ``require()`` function in your scripts or modules, as
  482. long as you either install the project or use ``setup.py develop`` to do
  483. development work on it. (See `"Development Mode"`_ below for more details on
  484. using ``setup.py develop``.)
  485. Dependencies that aren't in PyPI
  486. --------------------------------
  487. If your project depends on packages that aren't registered in PyPI, you may
  488. still be able to depend on them, as long as they are available for download
  489. as:
  490. - an egg, in the standard distutils ``sdist`` format,
  491. - a single ``.py`` file, or
  492. - a VCS repository (Subversion, Mercurial, or Git).
  493. You just need to add some URLs to the ``dependency_links`` argument to
  494. ``setup()``.
  495. The URLs must be either:
  496. 1. direct download URLs,
  497. 2. the URLs of web pages that contain direct download links, or
  498. 3. the repository's URL
  499. In general, it's better to link to web pages, because it is usually less
  500. complex to update a web page than to release a new version of your project.
  501. You can also use a SourceForge ``showfiles.php`` link in the case where a
  502. package you depend on is distributed via SourceForge.
  503. If you depend on a package that's distributed as a single ``.py`` file, you
  504. must include an ``"#egg=project-version"`` suffix to the URL, to give a project
  505. name and version number. (Be sure to escape any dashes in the name or version
  506. by replacing them with underscores.) EasyInstall will recognize this suffix
  507. and automatically create a trivial ``setup.py`` to wrap the single ``.py`` file
  508. as an egg.
  509. In the case of a VCS checkout, you should also append ``#egg=project-version``
  510. in order to identify for what package that checkout should be used. You can
  511. append ``@REV`` to the URL's path (before the fragment) to specify a revision.
  512. Additionally, you can also force the VCS being used by prepending the URL with
  513. a certain prefix. Currently available are:
  514. - ``svn+URL`` for Subversion,
  515. - ``git+URL`` for Git, and
  516. - ``hg+URL`` for Mercurial
  517. A more complete example would be:
  518. ``vcs+proto://host/path@revision#egg=project-version``
  519. Be careful with the version. It should match the one inside the project files.
  520. If you want to disregard the version, you have to omit it both in the
  521. ``requires`` and in the URL's fragment.
  522. This will do a checkout (or a clone, in Git and Mercurial parlance) to a
  523. temporary folder and run ``setup.py bdist_egg``.
  524. The ``dependency_links`` option takes the form of a list of URL strings. For
  525. example, the below will cause EasyInstall to search the specified page for
  526. eggs or source distributions, if the package's dependencies aren't already
  527. installed::
  528. setup(
  529. ...
  530. dependency_links = [
  531. "http://peak.telecommunity.com/snapshots/"
  532. ],
  533. )
  534. .. _Declaring Extras:
  535. Declaring "Extras" (optional features with their own dependencies)
  536. ------------------------------------------------------------------
  537. Sometimes a project has "recommended" dependencies, that are not required for
  538. all uses of the project. For example, a project might offer optional PDF
  539. output if ReportLab is installed, and reStructuredText support if docutils is
  540. installed. These optional features are called "extras", and setuptools allows
  541. you to define their requirements as well. In this way, other projects that
  542. require these optional features can force the additional requirements to be
  543. installed, by naming the desired extras in their ``install_requires``.
  544. For example, let's say that Project A offers optional PDF and reST support::
  545. setup(
  546. name="Project-A",
  547. ...
  548. extras_require = {
  549. 'PDF': ["ReportLab>=1.2", "RXP"],
  550. 'reST': ["docutils>=0.3"],
  551. }
  552. )
  553. As you can see, the ``extras_require`` argument takes a dictionary mapping
  554. names of "extra" features, to strings or lists of strings describing those
  555. features' requirements. These requirements will *not* be automatically
  556. installed unless another package depends on them (directly or indirectly) by
  557. including the desired "extras" in square brackets after the associated project
  558. name. (Or if the extras were listed in a requirement spec on the EasyInstall
  559. command line.)
  560. Extras can be used by a project's `entry points`_ to specify dynamic
  561. dependencies. For example, if Project A includes a "rst2pdf" script, it might
  562. declare it like this, so that the "PDF" requirements are only resolved if the
  563. "rst2pdf" script is run::
  564. setup(
  565. name="Project-A",
  566. ...
  567. entry_points = {
  568. 'console_scripts': [
  569. 'rst2pdf = project_a.tools.pdfgen [PDF]',
  570. 'rst2html = project_a.tools.htmlgen',
  571. # more script entry points ...
  572. ],
  573. }
  574. )
  575. Projects can also use another project's extras when specifying dependencies.
  576. For example, if project B needs "project A" with PDF support installed, it
  577. might declare the dependency like this::
  578. setup(
  579. name="Project-B",
  580. install_requires = ["Project-A[PDF]"],
  581. ...
  582. )
  583. This will cause ReportLab to be installed along with project A, if project B is
  584. installed -- even if project A was already installed. In this way, a project
  585. can encapsulate groups of optional "downstream dependencies" under a feature
  586. name, so that packages that depend on it don't have to know what the downstream
  587. dependencies are. If a later version of Project A builds in PDF support and
  588. no longer needs ReportLab, or if it ends up needing other dependencies besides
  589. ReportLab in order to provide PDF support, Project B's setup information does
  590. not need to change, but the right packages will still be installed if needed.
  591. Note, by the way, that if a project ends up not needing any other packages to
  592. support a feature, it should keep an empty requirements list for that feature
  593. in its ``extras_require`` argument, so that packages depending on that feature
  594. don't break (due to an invalid feature name). For example, if Project A above
  595. builds in PDF support and no longer needs ReportLab, it could change its
  596. setup to this::
  597. setup(
  598. name="Project-A",
  599. ...
  600. extras_require = {
  601. 'PDF': [],
  602. 'reST': ["docutils>=0.3"],
  603. }
  604. )
  605. so that Package B doesn't have to remove the ``[PDF]`` from its requirement
  606. specifier.
  607. Including Data Files
  608. ====================
  609. The distutils have traditionally allowed installation of "data files", which
  610. are placed in a platform-specific location. However, the most common use case
  611. for data files distributed with a package is for use *by* the package, usually
  612. by including the data files in the package directory.
  613. Setuptools offers three ways to specify data files to be included in your
  614. packages. First, you can simply use the ``include_package_data`` keyword,
  615. e.g.::
  616. from setuptools import setup, find_packages
  617. setup(
  618. ...
  619. include_package_data = True
  620. )
  621. This tells setuptools to install any data files it finds in your packages.
  622. The data files must be specified via the distutils' ``MANIFEST.in`` file.
  623. (They can also be tracked by a revision control system, using an appropriate
  624. plugin. See the section below on `Adding Support for Revision Control
  625. Systems`_ for information on how to write such plugins.)
  626. If you want finer-grained control over what files are included (for example,
  627. if you have documentation files in your package directories and want to exclude
  628. them from installation), then you can also use the ``package_data`` keyword,
  629. e.g.::
  630. from setuptools import setup, find_packages
  631. setup(
  632. ...
  633. package_data = {
  634. # If any package contains *.txt or *.rst files, include them:
  635. '': ['*.txt', '*.rst'],
  636. # And include any *.msg files found in the 'hello' package, too:
  637. 'hello': ['*.msg'],
  638. }
  639. )
  640. The ``package_data`` argument is a dictionary that maps from package names to
  641. lists of glob patterns. The globs may include subdirectory names, if the data
  642. files are contained in a subdirectory of the package. For example, if the
  643. package tree looks like this::
  644. setup.py
  645. src/
  646. mypkg/
  647. __init__.py
  648. mypkg.txt
  649. data/
  650. somefile.dat
  651. otherdata.dat
  652. The setuptools setup file might look like this::
  653. from setuptools import setup, find_packages
  654. setup(
  655. ...
  656. packages = find_packages('src'), # include all packages under src
  657. package_dir = {'':'src'}, # tell distutils packages are under src
  658. package_data = {
  659. # If any package contains *.txt files, include them:
  660. '': ['*.txt'],
  661. # And include any *.dat files found in the 'data' subdirectory
  662. # of the 'mypkg' package, also:
  663. 'mypkg': ['data/*.dat'],
  664. }
  665. )
  666. Notice that if you list patterns in ``package_data`` under the empty string,
  667. these patterns are used to find files in every package, even ones that also
  668. have their own patterns listed. Thus, in the above example, the ``mypkg.txt``
  669. file gets included even though it's not listed in the patterns for ``mypkg``.
  670. Also notice that if you use paths, you *must* use a forward slash (``/``) as
  671. the path separator, even if you are on Windows. Setuptools automatically
  672. converts slashes to appropriate platform-specific separators at build time.
  673. (Note: although the ``package_data`` argument was previously only available in
  674. ``setuptools``, it was also added to the Python ``distutils`` package as of
  675. Python 2.4; there is `some documentation for the feature`__ available on the
  676. python.org website. If using the setuptools-specific ``include_package_data``
  677. argument, files specified by ``package_data`` will *not* be automatically
  678. added to the manifest unless they are listed in the MANIFEST.in file.)
  679. __ http://docs.python.org/dist/node11.html
  680. Sometimes, the ``include_package_data`` or ``package_data`` options alone
  681. aren't sufficient to precisely define what files you want included. For
  682. example, you may want to include package README files in your revision control
  683. system and source distributions, but exclude them from being installed. So,
  684. setuptools offers an ``exclude_package_data`` option as well, that allows you
  685. to do things like this::
  686. from setuptools import setup, find_packages
  687. setup(
  688. ...
  689. packages = find_packages('src'), # include all packages under src
  690. package_dir = {'':'src'}, # tell distutils packages are under src
  691. include_package_data = True, # include everything in source control
  692. # ...but exclude README.txt from all packages
  693. exclude_package_data = { '': ['README.txt'] },
  694. )
  695. The ``exclude_package_data`` option is a dictionary mapping package names to
  696. lists of wildcard patterns, just like the ``package_data`` option. And, just
  697. as with that option, a key of ``''`` will apply the given pattern(s) to all
  698. packages. However, any files that match these patterns will be *excluded*
  699. from installation, even if they were listed in ``package_data`` or were
  700. included as a result of using ``include_package_data``.
  701. In summary, the three options allow you to:
  702. ``include_package_data``
  703. Accept all data files and directories matched by ``MANIFEST.in``.
  704. ``package_data``
  705. Specify additional patterns to match files and directories that may or may
  706. not be matched by ``MANIFEST.in`` or found in source control.
  707. ``exclude_package_data``
  708. Specify patterns for data files and directories that should *not* be
  709. included when a package is installed, even if they would otherwise have
  710. been included due to the use of the preceding options.
  711. NOTE: Due to the way the distutils build process works, a data file that you
  712. include in your project and then stop including may be "orphaned" in your
  713. project's build directories, requiring you to run ``setup.py clean --all`` to
  714. fully remove them. This may also be important for your users and contributors
  715. if they track intermediate revisions of your project using Subversion; be sure
  716. to let them know when you make changes that remove files from inclusion so they
  717. can run ``setup.py clean --all``.
  718. Accessing Data Files at Runtime
  719. -------------------------------
  720. Typically, existing programs manipulate a package's ``__file__`` attribute in
  721. order to find the location of data files. However, this manipulation isn't
  722. compatible with PEP 302-based import hooks, including importing from zip files
  723. and Python Eggs. It is strongly recommended that, if you are using data files,
  724. you should use the `Resource Management API`_ of ``pkg_resources`` to access
  725. them. The ``pkg_resources`` module is distributed as part of setuptools, so if
  726. you're using setuptools to distribute your package, there is no reason not to
  727. use its resource management API. See also `Accessing Package Resources`_ for
  728. a quick example of converting code that uses ``__file__`` to use
  729. ``pkg_resources`` instead.
  730. .. _Resource Management API: http://peak.telecommunity.com/DevCenter/PkgResources#resourcemanager-api
  731. .. _Accessing Package Resources: http://peak.telecommunity.com/DevCenter/PythonEggs#accessing-package-resources
  732. Non-Package Data Files
  733. ----------------------
  734. The ``distutils`` normally install general "data files" to a platform-specific
  735. location (e.g. ``/usr/share``). This feature intended to be used for things
  736. like documentation, example configuration files, and the like. ``setuptools``
  737. does not install these data files in a separate location, however. They are
  738. bundled inside the egg file or directory, alongside the Python modules and
  739. packages. The data files can also be accessed using the `Resource Management
  740. API`_, by specifying a ``Requirement`` instead of a package name::
  741. from pkg_resources import Requirement, resource_filename
  742. filename = resource_filename(Requirement.parse("MyProject"),"sample.conf")
  743. The above code will obtain the filename of the "sample.conf" file in the data
  744. root of the "MyProject" distribution.
  745. Note, by the way, that this encapsulation of data files means that you can't
  746. actually install data files to some arbitrary location on a user's machine;
  747. this is a feature, not a bug. You can always include a script in your
  748. distribution that extracts and copies your the documentation or data files to
  749. a user-specified location, at their discretion. If you put related data files
  750. in a single directory, you can use ``resource_filename()`` with the directory
  751. name to get a filesystem directory that then can be copied with the ``shutil``
  752. module. (Even if your package is installed as a zipfile, calling
  753. ``resource_filename()`` on a directory will return an actual filesystem
  754. directory, whose contents will be that entire subtree of your distribution.)
  755. (Of course, if you're writing a new package, you can just as easily place your
  756. data files or directories inside one of your packages, rather than using the
  757. distutils' approach. However, if you're updating an existing application, it
  758. may be simpler not to change the way it currently specifies these data files.)
  759. Automatic Resource Extraction
  760. -----------------------------
  761. If you are using tools that expect your resources to be "real" files, or your
  762. project includes non-extension native libraries or other files that your C
  763. extensions expect to be able to access, you may need to list those files in
  764. the ``eager_resources`` argument to ``setup()``, so that the files will be
  765. extracted together, whenever a C extension in the project is imported.
  766. This is especially important if your project includes shared libraries *other*
  767. than distutils-built C extensions, and those shared libraries use file
  768. extensions other than ``.dll``, ``.so``, or ``.dylib``, which are the
  769. extensions that setuptools 0.6a8 and higher automatically detects as shared
  770. libraries and adds to the ``native_libs.txt`` file for you. Any shared
  771. libraries whose names do not end with one of those extensions should be listed
  772. as ``eager_resources``, because they need to be present in the filesystem when
  773. he C extensions that link to them are used.
  774. The ``pkg_resources`` runtime for compressed packages will automatically
  775. extract *all* C extensions and ``eager_resources`` at the same time, whenever
  776. *any* C extension or eager resource is requested via the ``resource_filename()``
  777. API. (C extensions are imported using ``resource_filename()`` internally.)
  778. This ensures that C extensions will see all of the "real" files that they
  779. expect to see.
  780. Note also that you can list directory resource names in ``eager_resources`` as
  781. well, in which case the directory's contents (including subdirectories) will be
  782. extracted whenever any C extension or eager resource is requested.
  783. Please note that if you're not sure whether you need to use this argument, you
  784. don't! It's really intended to support projects with lots of non-Python
  785. dependencies and as a last resort for crufty projects that can't otherwise
  786. handle being compressed. If your package is pure Python, Python plus data
  787. files, or Python plus C, you really don't need this. You've got to be using
  788. either C or an external program that needs "real" files in your project before
  789. there's any possibility of ``eager_resources`` being relevant to your project.
  790. Extensible Applications and Frameworks
  791. ======================================
  792. .. _Entry Points:
  793. Dynamic Discovery of Services and Plugins
  794. -----------------------------------------
  795. ``setuptools`` supports creating libraries that "plug in" to extensible
  796. applications and frameworks, by letting you register "entry points" in your
  797. project that can be imported by the application or framework.
  798. For example, suppose that a blogging tool wants to support plugins
  799. that provide translation for various file types to the blog's output format.
  800. The framework might define an "entry point group" called ``blogtool.parsers``,
  801. and then allow plugins to register entry points for the file extensions they
  802. support.
  803. This would allow people to create distributions that contain one or more
  804. parsers for different file types, and then the blogging tool would be able to
  805. find the parsers at runtime by looking up an entry point for the file
  806. extension (or mime type, or however it wants to).
  807. Note that if the blogging tool includes parsers for certain file formats, it
  808. can register these as entry points in its own setup script, which means it
  809. doesn't have to special-case its built-in formats. They can just be treated
  810. the same as any other plugin's entry points would be.
  811. If you're creating a project that plugs in to an existing application or
  812. framework, you'll need to know what entry points or entry point groups are
  813. defined by that application or framework. Then, you can register entry points
  814. in your setup script. Here are a few examples of ways you might register an
  815. ``.rst`` file parser entry point in the ``blogtool.parsers`` entry point group,
  816. for our hypothetical blogging tool::
  817. setup(
  818. # ...
  819. entry_points = {'blogtool.parsers': '.rst = some_module:SomeClass'}
  820. )
  821. setup(
  822. # ...
  823. entry_points = {'blogtool.parsers': ['.rst = some_module:a_func']}
  824. )
  825. setup(
  826. # ...
  827. entry_points = """
  828. [blogtool.parsers]
  829. .rst = some.nested.module:SomeClass.some_classmethod [reST]
  830. """,
  831. extras_require = dict(reST = "Docutils>=0.3.5")
  832. )
  833. The ``entry_points`` argument to ``setup()`` accepts either a string with
  834. ``.ini``-style sections, or a dictionary mapping entry point group names to
  835. either strings or lists of strings containing entry point specifiers. An
  836. entry point specifier consists of a name and value, separated by an ``=``
  837. sign. The value consists of a dotted module name, optionally followed by a
  838. ``:`` and a dotted identifier naming an object within the module. It can
  839. also include a bracketed list of "extras" that are required for the entry
  840. point to be used. When the invoking application or framework requests loading
  841. of an entry point, any requirements implied by the associated extras will be
  842. passed to ``pkg_resources.require()``, so that an appropriate error message
  843. can be displayed if the needed package(s) are missing. (Of course, the
  844. invoking app or framework can ignore such errors if it wants to make an entry
  845. point optional if a requirement isn't installed.)
  846. Defining Additional Metadata
  847. ----------------------------
  848. Some extensible applications and frameworks may need to define their own kinds
  849. of metadata to include in eggs, which they can then access using the
  850. ``pkg_resources`` metadata APIs. Ordinarily, this is done by having plugin
  851. developers include additional files in their ``ProjectName.egg-info``
  852. directory. However, since it can be tedious to create such files by hand, you
  853. may want to create a distutils extension that will create the necessary files
  854. from arguments to ``setup()``, in much the same way that ``setuptools`` does
  855. for many of the ``setup()`` arguments it adds. See the section below on
  856. `Creating distutils Extensions`_ for more details, especially the subsection on
  857. `Adding new EGG-INFO Files`_.
  858. "Development Mode"
  859. ==================
  860. Under normal circumstances, the ``distutils`` assume that you are going to
  861. build a distribution of your project, not use it in its "raw" or "unbuilt"
  862. form. If you were to use the ``distutils`` that way, you would have to rebuild
  863. and reinstall your project every time you made a change to it during
  864. development.
  865. Another problem that sometimes comes up with the ``distutils`` is that you may
  866. need to do development on two related projects at the same time. You may need
  867. to put both projects' packages in the same directory to run them, but need to
  868. keep them separate for revision control purposes. How can you do this?
  869. Setuptools allows you to deploy your projects for use in a common directory or
  870. staging area, but without copying any files. Thus, you can edit each project's
  871. code in its checkout directory, and only need to run build commands when you
  872. change a project's C extensions or similarly compiled files. You can even
  873. deploy a project into another project's checkout directory, if that's your
  874. preferred way of working (as opposed to using a common independent staging area
  875. or the site-packages directory).
  876. To do this, use the ``setup.py develop`` command. It works very similarly to
  877. ``setup.py install`` or the EasyInstall tool, except that it doesn't actually
  878. install anything. Instead, it creates a special ``.egg-link`` file in the
  879. deployment directory, that links to your project's source code. And, if your
  880. deployment directory is Python's ``site-packages`` directory, it will also
  881. update the ``easy-install.pth`` file to include your project's source code,
  882. thereby making it available on ``sys.path`` for all programs using that Python
  883. installation.
  884. If you have enabled the ``use_2to3`` flag, then of course the ``.egg-link``
  885. will not link directly to your source code when run under Python 3, since
  886. that source code would be made for Python 2 and not work under Python 3.
  887. Instead the ``setup.py develop`` will build Python 3 code under the ``build``
  888. directory, and link there. This means that after doing code changes you will
  889. have to run ``setup.py build`` before these changes are picked up by your
  890. Python 3 installation.
  891. In addition, the ``develop`` command creates wrapper scripts in the target
  892. script directory that will run your in-development scripts after ensuring that
  893. all your ``install_requires`` packages are available on ``sys.path``.
  894. You can deploy the same project to multiple staging areas, e.g. if you have
  895. multiple projects on the same machine that are sharing the same project you're
  896. doing development work.
  897. When you're done with a given development task, you can remove the project
  898. source from a staging area using ``setup.py develop --uninstall``, specifying
  899. the desired staging area if it's not the default.
  900. There are several options to control the precise behavior of the ``develop``
  901. command; see the section on the `develop`_ command below for more details.
  902. Note that you can also apply setuptools commands to non-setuptools projects,
  903. using commands like this::
  904. python -c "import setuptools; execfile('setup.py')" develop
  905. That is, you can simply list the normal setup commands and options following
  906. the quoted part.
  907. Distributing a ``setuptools``-based project
  908. ===========================================
  909. Using ``setuptools``... Without bundling it!
  910. ---------------------------------------------
  911. Your users might not have ``setuptools`` installed on their machines, or even
  912. if they do, it might not be the right version. Fixing this is easy; just
  913. download `ez_setup.py`_, and put it in the same directory as your ``setup.py``
  914. script. (Be sure to add it to your revision control system, too.) Then add
  915. these two lines to the very top of your setup script, before the script imports
  916. anything from setuptools:
  917. .. code-block:: python
  918. import ez_setup
  919. ez_setup.use_setuptools()
  920. That's it. The ``ez_setup`` module will automatically download a matching
  921. version of ``setuptools`` from PyPI, if it isn't present on the target system.
  922. Whenever you install an updated version of setuptools, you should also update
  923. your projects' ``ez_setup.py`` files, so that a matching version gets installed
  924. on the target machine(s).
  925. By the way, setuptools supports the new PyPI "upload" command, so you can use
  926. ``setup.py sdist upload`` or ``setup.py bdist_egg upload`` to upload your
  927. source or egg distributions respectively. Your project's current version must
  928. be registered with PyPI first, of course; you can use ``setup.py register`` to
  929. do that. Or you can do it all in one step, e.g. ``setup.py register sdist
  930. bdist_egg upload`` will register the package, build source and egg
  931. distributions, and then upload them both to PyPI, where they'll be easily
  932. found by other projects that depend on them.
  933. (By the way, if you need to distribute a specific version of ``setuptools``,
  934. you can specify the exact version and base download URL as parameters to the
  935. ``use_setuptools()`` function. See the function's docstring for details.)
  936. What Your Users Should Know
  937. ---------------------------
  938. In general, a setuptools-based project looks just like any distutils-based
  939. project -- as long as your users have an internet connection and are installing
  940. to ``site-packages``, that is. But for some users, these conditions don't
  941. apply, and they may become frustrated if this is their first encounter with
  942. a setuptools-based project. To keep these users happy, you should review the
  943. following topics in your project's installation instructions, if they are
  944. relevant to your project and your target audience isn't already familiar with
  945. setuptools and ``easy_install``.
  946. Network Access
  947. If your project is using ``ez_setup``, you should inform users of the
  948. need to either have network access, or to preinstall the correct version of
  949. setuptools using the `EasyInstall installation instructions`_. Those
  950. instructions also have tips for dealing with firewalls as well as how to
  951. manually download and install setuptools.
  952. Custom Installation Locations
  953. You should inform your users that if they are installing your project to
  954. somewhere other than the main ``site-packages`` directory, they should
  955. first install setuptools using the instructions for `Custom Installation
  956. Locations`_, before installing your project.
  957. Your Project's Dependencies
  958. If your project depends on other projects that may need to be downloaded
  959. from PyPI or elsewhere, you should list them in your installation
  960. instructions, or tell users how to find out what they are. While most
  961. users will not need this information, any users who don't have unrestricted
  962. internet access may have to find, download, and install the other projects
  963. manually. (Note, however, that they must still install those projects
  964. using ``easy_install``, or your project will not know they are installed,
  965. and your setup script will try to download them again.)
  966. If you want to be especially friendly to users with limited network access,
  967. you may wish to build eggs for your project and its dependencies, making
  968. them all available for download from your site, or at least create a page
  969. with links to all of the needed eggs. In this way, users with limited
  970. network access can manually download all the eggs to a single directory,
  971. then use the ``-f`` option of ``easy_install`` to specify the directory
  972. to find eggs in. Users who have full network access can just use ``-f``
  973. with the URL of your download page, and ``easy_install`` will find all the
  974. needed eggs using your links directly. This is also useful when your
  975. target audience isn't able to compile packages (e.g. most Windows users)
  976. and your package or some of its dependencies include C code.
  977. Revision Control System Users and Co-Developers
  978. Users and co-developers who are tracking your in-development code using
  979. a revision control system should probably read this manual's sections
  980. regarding such development. Alternately, you may wish to create a
  981. quick-reference guide containing the tips from this manual that apply to
  982. your particular situation. For example, if you recommend that people use
  983. ``setup.py develop`` when tracking your in-development code, you should let
  984. them know that this needs to be run after every update or commit.
  985. Similarly, if you remove modules or data files from your project, you
  986. should remind them to run ``setup.py clean --all`` and delete any obsolete
  987. ``.pyc`` or ``.pyo``. (This tip applies to the distutils in general, not
  988. just setuptools, but not everybody knows about them; be kind to your users
  989. by spelling out your project's best practices rather than leaving them
  990. guessing.)
  991. Creating System Packages
  992. Some users want to manage all Python packages using a single package
  993. manager, and sometimes that package manager isn't ``easy_install``!
  994. Setuptools currently supports ``bdist_rpm``, ``bdist_wininst``, and
  995. ``bdist_dumb`` formats for system packaging. If a user has a locally-
  996. installed "bdist" packaging tool that internally uses the distutils
  997. ``install`` command, it should be able to work with ``setuptools``. Some
  998. examples of "bdist" formats that this should work with include the
  999. ``bdist_nsi`` and ``bdist_msi`` formats for Windows.
  1000. However, packaging tools that build binary distributions by running
  1001. ``setup.py install`` on the command line or as a subprocess will require
  1002. modification to work with setuptools. They should use the
  1003. ``--single-version-externally-managed`` option to the ``install`` command,
  1004. combined with the standard ``--root`` or ``--record`` options.
  1005. See the `install command`_ documentation below for more details. The
  1006. ``bdist_deb`` command is an example of a command that currently requires
  1007. this kind of patching to work with setuptools.
  1008. If you or your users have a problem building a usable system package for
  1009. your project, please report the problem via the mailing list so that
  1010. either the "bdist" tool in question or setuptools can be modified to
  1011. resolve the issue.
  1012. Setting the ``zip_safe`` flag
  1013. -----------------------------
  1014. For some use cases (such as bundling as part of a larger application), Python
  1015. packages may be run directly from a zip file.
  1016. Not all packages, however, are capable of running in compressed form, because
  1017. they may expect to be able to access either source code or data files as
  1018. normal operating system files. So, ``setuptools`` can install your project
  1019. as a zipfile or a directory, and its default choice is determined by the
  1020. project's ``zip_safe`` flag.
  1021. You can pass a True or False value for the ``zip_safe`` argument to the
  1022. ``setup()`` function, or you can omit it. If you omit it, the ``bdist_egg``
  1023. command will analyze your project's contents to see if it can detect any
  1024. conditions that would prevent it from working in a zipfile. It will output
  1025. notices to the console about any such conditions that it finds.
  1026. Currently, this analysis is extremely conservative: it will consider the
  1027. project unsafe if it contains any C extensions or datafiles whatsoever. This
  1028. does *not* mean that the project can't or won't work as a zipfile! It just
  1029. means that the ``bdist_egg`` authors aren't yet comfortable asserting that
  1030. the project *will* work. If the project contains no C or data files, and does
  1031. no ``__file__`` or ``__path__`` introspection or source code manipulation, then
  1032. there is an extremely solid chance the project will work when installed as a
  1033. zipfile. (And if the project uses ``pkg_resources`` for all its data file
  1034. access, then C extensions and other data files shouldn't be a problem at all.
  1035. See the `Accessing Data Files at Runtime`_ section above for more information.)
  1036. However, if ``bdist_egg`` can't be *sure* that your package will work, but
  1037. you've checked over all the warnings it issued, and you are either satisfied it
  1038. *will* work (or if you want to try it for yourself), then you should set
  1039. ``zip_safe`` to ``True`` in your ``setup()`` call. If it turns out that it
  1040. doesn't work, you can always change it to ``False``, which will force
  1041. ``setuptools`` to install your project as a directory rather than as a zipfile.
  1042. Of course, the end-user can still override either decision, if they are using
  1043. EasyInstall to install your package. And, if you want to override for testing
  1044. purposes, you can just run ``setup.py easy_install --zip-ok .`` or ``setup.py
  1045. easy_install --always-unzip .`` in your project directory. to install the
  1046. package as a zipfile or directory, respectively.
  1047. In the future, as we gain more experience with different packages and become
  1048. more satisfied with the robustness of the ``pkg_resources`` runtime, the
  1049. "zip safety" analysis may become less conservative. However, we strongly
  1050. recommend that you determine for yourself whether your project functions
  1051. correctly when installed as a zipfile, correct any problems if you can, and
  1052. then make an explicit declaration of ``True`` or ``False`` for the ``zip_safe``
  1053. flag, so that it will not be necessary for ``bdist_egg`` or ``EasyInstall`` to
  1054. try to guess whether your project can work as a zipfile.
  1055. Namespace Packages
  1056. ------------------
  1057. Sometimes, a large package is more useful if distributed as a collection of
  1058. smaller eggs. However, Python does not normally allow the contents of a
  1059. package to be retrieved from more than one location. "Namespace packages"
  1060. are a solution for this problem. When you declare a package to be a namespace
  1061. package, it means that the package has no meaningful contents in its
  1062. ``__init__.py``, and that it is merely a container for modules and subpackages.
  1063. The ``pkg_resources`` runtime will then automatically ensure that the contents
  1064. of namespace packages that are spread over multiple eggs or directories are
  1065. combined into a single "virtual" package.
  1066. The ``namespace_packages`` argument to ``setup()`` lets you declare your
  1067. project's namespace packages, so that they will be included in your project's
  1068. metadata. The argument should list the namespace packages that the egg
  1069. participates in. For example, the ZopeInterface project might do this::
  1070. setup(
  1071. # ...
  1072. namespace_packages = ['zope']
  1073. )
  1074. because it contains a ``zope.interface`` package that lives in the ``zope``
  1075. namespace package. Similarly, a project for a standalone ``zope.publisher``
  1076. would also declare the ``zope`` namespace package. When these projects are
  1077. installed and used, Python will see them both as part of a "virtual" ``zope``
  1078. package, even though they will be installed in different locations.
  1079. Namespace packages don't have to be top-level packages. For example, Zope 3's
  1080. ``zope.app`` package is a namespace package, and in the future PEAK's
  1081. ``peak.util`` package will be too.
  1082. Note, by the way, that your project's source tree must include the namespace
  1083. packages' ``__init__.py`` files (and the ``__init__.py`` of any parent
  1084. packages), in a normal Python package layout. These ``__init__.py`` files
  1085. *must* contain the line::
  1086. __import__('pkg_resources').declare_namespace(__name__)
  1087. This code ensures that the namespace package machinery is operating and that
  1088. the current package is registered as a namespace package.
  1089. You must NOT include any other code and data in a namespace package's
  1090. ``__init__.py``. Even though it may appear to work during development, or when
  1091. projects are installed as ``.egg`` files, it will not work when the projects
  1092. are installed using "system" packaging tools -- in such cases the
  1093. ``__init__.py`` files will not be installed, let alone executed.
  1094. You must include the ``declare_namespace()`` line in the ``__init__.py`` of
  1095. *every* project that has contents for the namespace package in question, in
  1096. order to ensure that the namespace will be declared regardless of which
  1097. project's copy of ``__init__.py`` is loaded first. If the first loaded
  1098. ``__init__.py`` doesn't declare it, it will never *be* declared, because no
  1099. other copies will ever be loaded!
  1100. TRANSITIONAL NOTE
  1101. ~~~~~~~~~~~~~~~~~
  1102. Setuptools automatically calls ``declare_namespace()`` for you at runtime,
  1103. but future versions may *not*. This is because the automatic declaration
  1104. feature has some negative side effects, such as needing to import all namespace
  1105. packages during the initialization of the ``pkg_resources`` runtime, and also
  1106. the need for ``pkg_resources`` to be explicitly imported before any namespace
  1107. packages work at all. In some future releases, you'll be responsible
  1108. for including your own declaration lines, and the automatic declaration feature
  1109. will be dropped to get rid of the negative side effects.
  1110. During the remainder of the current development cycle, therefore, setuptools
  1111. will warn you about missing ``declare_namespace()`` calls in your
  1112. ``__init__.py`` files, and you should correct these as soon as possible
  1113. before the compatibility support is removed.
  1114. Namespace packages without declaration lines will not work
  1115. correctly once a user has upgraded to a later version, so it's important that
  1116. you make this change now in order to avoid having your code break in the field.
  1117. Our apologies for the inconvenience, and thank you for your patience.
  1118. Tagging and "Daily Build" or "Snapshot" Releases
  1119. ------------------------------------------------
  1120. When a set of related projects are under development, it may be important to
  1121. track finer-grained version increments than you would normally use for e.g.
  1122. "stable" releases. While stable releases might be measured in dotted numbers
  1123. with alpha/beta/etc. status codes, development versions of a project often
  1124. need to be tracked by revision or build number or even build date. This is
  1125. especially true when projects in development need to refer to one another, and
  1126. therefore may literally need an up-to-the-minute version of something!
  1127. To support these scenarios, ``setuptools`` allows you to "tag" your source and
  1128. egg distributions by adding one or more of the following to the project's
  1129. "official" version identifier:
  1130. * A manually-specified pre-release tag, such as "build" or "dev", or a
  1131. manually-specified post-release tag, such as a build or revision number
  1132. (``--tag-build=STRING, -bSTRING``)
  1133. * A "last-modified revision number" string generated automatically from
  1134. Subversion's metadata (assuming your project is being built from a Subversion
  1135. "working copy") (``--tag-svn-revision, -r``)
  1136. * An 8-character representation of the build date (``--tag-date, -d``), as
  1137. a postrelease tag
  1138. You can add these tags by adding ``egg_info`` and the desired options to
  1139. the command line ahead of the ``sdist`` or ``bdist`` commands that you want
  1140. to generate a daily build or snapshot for. See the section below on the
  1141. `egg_info`_ command for more details.
  1142. (Also, before you release your project, be sure to see the section above on
  1143. `Specifying Your Project's Version`_ for more information about how pre- and
  1144. post-release tags affect how setuptools and EasyInstall interpret version
  1145. numbers. This is important in order to make sure that dependency processing
  1146. tools will know which versions of your project are newer than others.)
  1147. Finally, if you are creating builds frequently, and either building them in a
  1148. downloadable location or are copying them to a distribution server, you should
  1149. probably also check out the `rotate`_ command, which lets you automatically
  1150. delete all but the N most-recently-modified distributions matching a glob
  1151. pattern. So, you can use a command line like::
  1152. setup.py egg_info -rbDEV bdist_egg rotate -m.egg -k3
  1153. to build an egg whose version info includes 'DEV-rNNNN' (where NNNN is the
  1154. most recent Subversion revision that affected the source tree), and then
  1155. delete any egg files from the distribution directory except for the three
  1156. that were built most recently.
  1157. If you have to manage automated builds for multiple packages, each with
  1158. different tagging and rotation policies, you may also want to check out the
  1159. `alias`_ command, which would let each package define an alias like ``daily``
  1160. that would perform the necessary tag, build, and rotate commands. Then, a
  1161. simpler script or cron job could just run ``setup.py daily`` in each project
  1162. directory. (And, you could also define sitewide or per-user default versions
  1163. of the ``daily`` alias, so that projects that didn't define their own would
  1164. use the appropriate defaults.)
  1165. Generating Source Distributions
  1166. -------------------------------
  1167. ``setuptools`` enhances the distutils' default algorithm for source file
  1168. selection with pluggable endpoints for looking up files to include. If you are
  1169. using a revision control system, and your source distributions only need to
  1170. include files that you're tracking in revision control, use a corresponding
  1171. plugin instead of writing a ``MANIFEST.in`` file. See the section below on
  1172. `Adding Support for Revision Control Systems`_ for information on plugins.
  1173. If you need to include automatically generated files, or files that are kept in
  1174. an unsupported revision control system, you'll need to create a ``MANIFEST.in``
  1175. file to specify any files that the default file location algorithm doesn't
  1176. catch. See the distutils documentation for more information on the format of
  1177. the ``MANIFEST.in`` file.
  1178. But, be sure to ignore any part of the distutils documentation that deals with
  1179. ``MANIFEST`` or how it's generated from ``MANIFEST.in``; setuptools shields you
  1180. from these issues and doesn't work the same way in any case. Unlike the
  1181. distutils, setuptools regenerates the source distribution manifest file
  1182. every time you build a source distribution, and it builds it inside the
  1183. project's ``.egg-info`` directory, out of the way of your main project
  1184. directory. You therefore need not worry about whether it is up-to-date or not.
  1185. Indeed, because setuptools' approach to determining the contents of a source
  1186. distribution is so much simpler, its ``sdist`` command omits nearly all of
  1187. the options that the distutils' more complex ``sdist`` process requires. For
  1188. all practical purposes, you'll probably use only the ``--formats`` option, if
  1189. you use any option at all.
  1190. Making your package available for EasyInstall
  1191. ---------------------------------------------
  1192. If you use the ``register`` command (``setup.py register``) to register your
  1193. package with PyPI, that's most of the battle right there. (See the
  1194. `docs for the register command`_ for more details.)
  1195. .. _docs for the register command: http://docs.python.org/dist/package-index.html
  1196. If you also use the `upload`_ command to upload actual distributions of your
  1197. package, that's even better, because EasyInstall will be able to find and
  1198. download them directly from your project's PyPI page.
  1199. However, there may be reasons why you don't want to upload distributions to
  1200. PyPI, and just want your existing distributions (or perhaps a Subversion
  1201. checkout) to be used instead.
  1202. So here's what you need to do before running the ``register`` command. There
  1203. are three ``setup()`` arguments that affect EasyInstall:
  1204. ``url`` and ``download_url``
  1205. These become links on your project's PyPI page. EasyInstall will examine
  1206. them to see if they link to a package ("primary links"), or whether they are
  1207. HTML pages. If they're HTML pages, EasyInstall scans all HREF's on the
  1208. page for primary links
  1209. ``long_description``
  1210. EasyInstall will check any URLs contained in this argument to see if they
  1211. are primary links.
  1212. A URL is considered a "primary link" if it is a link to a .tar.gz, .tgz, .zip,
  1213. .egg, .egg.zip, .tar.bz2, or .exe file, or if it has an ``#egg=project`` or
  1214. ``#egg=project-version`` fragment identifier attached to it. EasyInstall
  1215. attempts to determine a project name and optional version number from the text
  1216. of a primary link *without* downloading it. When it has found all the primary
  1217. links, EasyInstall will select the best match based on requested version,
  1218. platform compatibility, and other criteria.
  1219. So, if your ``url`` or ``download_url`` point either directly to a downloadable
  1220. source distribution, or to HTML page(s) that have direct links to such, then
  1221. EasyInstall will be able to locate downloads automatically. If you want to
  1222. make Subversion checkouts available, then you should create links with either
  1223. ``#egg=project`` or ``#egg=project-version`` added to the URL. You should
  1224. replace ``project`` and ``version`` with the values they would have in an egg
  1225. filename. (Be sure to actually generate an egg and then use the initial part
  1226. of the filename, rather than trying to guess what the escaped form of the
  1227. project name and version number will be.)
  1228. Note that Subversion checkout links are of lower precedence than other kinds
  1229. of distributions, so EasyInstall will not select a Subversion checkout for
  1230. downloading unless it has a version included in the ``#egg=`` suffix, and
  1231. it's a higher version than EasyInstall has seen in any other links for your
  1232. project.
  1233. As a result, it's a common practice to use mark checkout URLs with a version of
  1234. "dev" (i.e., ``#egg=projectname-dev``), so that users can do something like
  1235. this::
  1236. easy_install --editable projectname==dev
  1237. in order to check out the in-development version of ``projectname``.
  1238. Managing "Continuous Releases" Using Subversion
  1239. -----------------------------------------------
  1240. If you expect your users to track in-development versions of your project via
  1241. Subversion, there are a few additional steps you should take to ensure that
  1242. things work smoothly with EasyInstall. First, you should add the following
  1243. to your project's ``setup.cfg`` file:
  1244. .. code-block:: ini
  1245. [egg_info]
  1246. tag_build = .dev
  1247. tag_svn_revision = 1
  1248. This will tell ``setuptools`` to generate package version numbers like
  1249. ``1.0a1.dev-r1263``, which will be considered to be an *older* release than
  1250. ``1.0a1``. Thus, when you actually release ``1.0a1``, the entire egg
  1251. infrastructure (including ``setuptools``, ``pkg_resources`` and EasyInstall)
  1252. will know that ``1.0a1`` supersedes any interim snapshots from Subversion, and
  1253. handle upgrades accordingly.
  1254. (Note: the project version number you specify in ``setup.py`` should always be
  1255. the *next* version of your software, not the last released version.
  1256. Alternately, you can leave out the ``tag_build=.dev``, and always use the
  1257. *last* release as a version number, so that your post-1.0 builds are labelled
  1258. ``1.0-r1263``, indicating a post-1.0 patchlevel. Most projects so far,
  1259. however, seem to prefer to think of their project as being a future version
  1260. still under development, rather than a past version being patched. It is of
  1261. course possible for a single project to have both situations, using
  1262. post-release numbering on release branches, and pre-release numbering on the
  1263. trunk. But you don't have to make things this complex if you don't want to.)
  1264. Commonly, projects releasing code from Subversion will include a PyPI link to
  1265. their checkout URL (as described in the previous section) with an
  1266. ``#egg=projectname-dev`` suffix. This allows users to request EasyInstall
  1267. to download ``projectname==dev`` in order to get the latest in-development
  1268. code. Note that if your project depends on such in-progress code, you may wish
  1269. to specify your ``install_requires`` (or other requirements) to include
  1270. ``==dev``, e.g.:
  1271. .. code-block:: python
  1272. install_requires = ["OtherProject>=0.2a1.dev-r143,==dev"]
  1273. The above example says, "I really want at least this particular development
  1274. revision number, but feel free to follow and use an ``#egg=OtherProject-dev``
  1275. link if you find one". This avoids the need to have actual source or binary
  1276. distribution snapshots of in-development code available, just to be able to
  1277. depend on the latest and greatest a project has to offer.
  1278. A final note for Subversion development: if you are using SVN revision tags
  1279. as described in this section, it's a good idea to run ``setup.py develop``
  1280. after each Subversion checkin or update, because your project's version number
  1281. will be changing, and your script wrappers need to be updated accordingly.
  1282. Also, if the project's requirements have changed, the ``develop`` command will
  1283. take care of fetching the updated dependencies, building changed extensions,
  1284. etc. Be sure to also remind any of your users who check out your project
  1285. from Subversion that they need to run ``setup.py develop`` after every update
  1286. in order to keep their checkout completely in sync.
  1287. Making "Official" (Non-Snapshot) Releases
  1288. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1289. When you make an official release, creating source or binary distributions,
  1290. you will need to override the tag settings from ``setup.cfg``, so that you
  1291. don't end up registering versions like ``foobar-0.7a1.dev-r34832``. This is
  1292. easy to do if you are developing on the trunk and using tags or branches for
  1293. your releases - just make the change to ``setup.cfg`` after branching or
  1294. tagging the release, so the trunk will still produce development snapshots.
  1295. Alternately, if you are not branching for releases, you can override the
  1296. default version options on the command line, using something like::
  1297. python setup.py egg_info -RDb "" sdist bdist_egg register upload
  1298. The first part of this command (``egg_info -RDb ""``) will override the
  1299. configured tag information, before creating source and binary eggs, registering
  1300. the project with PyPI, and uploading the files. Thus, these commands will use
  1301. the plain version from your ``setup.py``, without adding the Subversion
  1302. revision number or build designation string.
  1303. Of course, if you will be doing this a lot, you may wish to create a personal
  1304. alias for this operation, e.g.::
  1305. python setup.py alias -u release egg_info -RDb ""
  1306. You can then use it like this::
  1307. python setup.py release sdist bdist_egg register upload
  1308. Or of course you can create more elaborate aliases that do all of the above.
  1309. See the sections below on the `egg_info`_ and `alias`_ commands for more ideas.
  1310. Distributing Extensions compiled with Pyrex
  1311. -------------------------------------------
  1312. ``setuptools`` includes transparent support for building Pyrex extensions, as
  1313. long as you define your extensions using ``setuptools.Extension``, *not*
  1314. ``distutils.Extension``. You must also not import anything from Pyrex in
  1315. your setup script.
  1316. If you follow these rules, you can safely list ``.pyx`` files as the source
  1317. of your ``Extension`` objects in the setup script. ``setuptools`` will detect
  1318. at build time whether Pyrex is installed or not. If it is, then ``setuptools``
  1319. will use it. If not, then ``setuptools`` will silently change the
  1320. ``Extension`` objects to refer to the ``.c`` counterparts of the ``.pyx``
  1321. files, so that the normal distutils C compilation process will occur.
  1322. Of course, for this to work, your source distributions must include the C
  1323. code generated by Pyrex, as well as your original ``.pyx`` files. This means
  1324. that you will probably want to include current ``.c`` files in your revision
  1325. control system, rebuilding them whenever you check changes in for the ``.pyx``
  1326. source files. This will ensure that people tracking your project in a revision
  1327. control system will be able to build it even if they don't have Pyrex
  1328. installed, and that your source releases will be similarly usable with or
  1329. without Pyrex.
  1330. -----------------
  1331. Command Reference
  1332. -----------------
  1333. .. _alias:
  1334. ``alias`` - Define shortcuts for commonly used commands
  1335. =======================================================
  1336. Sometimes, you need to use the same commands over and over, but you can't
  1337. necessarily set them as defaults. For example, if you produce both development
  1338. snapshot releases and "stable" releases of a project, you may want to put
  1339. the distributions in different places, or use different ``egg_info`` tagging
  1340. options, etc. In these cases, it doesn't make sense to set the options in
  1341. a distutils configuration file, because the values of the options changed based
  1342. on what you're trying to do.
  1343. Setuptools therefore allows you to define "aliases" - shortcut names for
  1344. an arbitrary string of commands and options, using ``setup.py alias aliasname
  1345. expansion``, where aliasname is the name of the new alias, and the remainder of
  1346. the command line supplies its expansion. For example, this command defines
  1347. a sitewide alias called "daily", that sets various ``egg_info`` tagging
  1348. options::
  1349. setup.py alias --global-config daily egg_info --tag-svn-revision \
  1350. --tag-build=development
  1351. Once the alias is defined, it can then be used with other setup commands,
  1352. e.g.::
  1353. setup.py daily bdist_egg # generate a daily-build .egg file
  1354. setup.py daily sdist # generate a daily-build source distro
  1355. setup.py daily sdist bdist_egg # generate both
  1356. The above commands are interpreted as if the word ``daily`` were replaced with
  1357. ``egg_info --tag-svn-revision --tag-build=development``.
  1358. Note that setuptools will expand each alias *at most once* in a given command
  1359. line. This serves two purposes. First, if you accidentally create an alias
  1360. loop, it will have no effect; you'll instead get an error message about an
  1361. unknown command. Second, it allows you to define an alias for a command, that
  1362. uses that command. For example, this (project-local) alias::
  1363. setup.py alias bdist_egg bdist_egg rotate -k1 -m.egg
  1364. redefines the ``bdist_egg`` command so that it always runs the ``rotate``
  1365. command afterwards to delete all but the newest egg file. It doesn't loop
  1366. indefinitely on ``bdist_egg`` because the alias is only expanded once when
  1367. used.
  1368. You can remove a defined alias with the ``--remove`` (or ``-r``) option, e.g.::
  1369. setup.py alias --global-config --remove daily
  1370. would delete the "daily" alias we defined above.
  1371. Aliases can be defined on a project-specific, per-user, or sitewide basis. The
  1372. default is to define or remove a project-specific alias, but you can use any of
  1373. the `configuration file options`_ (listed under the `saveopts`_ command, below)
  1374. to determine which distutils configuration file an aliases will be added to
  1375. (or removed from).
  1376. Note that if you omit the "expansion" argument to the ``alias`` command,
  1377. you'll get output showing that alias' current definition (and what
  1378. configuration file it's defined in). If you omit the alias name as well,
  1379. you'll get a listing of all current aliases along with their configuration
  1380. file locations.
  1381. ``bdist_egg`` - Create a Python Egg for the project
  1382. ===================================================
  1383. This command generates a Python Egg (``.egg`` file) for the project. Python
  1384. Eggs are the preferred binary distribution format for EasyInstall, because they
  1385. are cross-platform (for "pure" packages), directly importable, and contain
  1386. project metadata including scripts and information about the project's
  1387. dependencies. They can be simply downloaded and added to ``sys.path``
  1388. directly, or they can be placed in a directory on ``sys.path`` and then
  1389. automatically discovered by the egg runtime system.
  1390. This command runs the `egg_info`_ command (if it hasn't already run) to update
  1391. the project's metadata (``.egg-info``) directory. If you have added any extra
  1392. metadata files to the ``.egg-info`` directory, those files will be included in
  1393. the new egg file's metadata directory, for use by the egg runtime system or by
  1394. any applications or frameworks that use that metadata.
  1395. You won't usually need to specify any special options for this command; just
  1396. use ``bdist_egg`` and you're done. But there are a few options that may
  1397. be occasionally useful:
  1398. ``--dist-dir=DIR, -d DIR``
  1399. Set the directory where the ``.egg`` file will be placed. If you don't
  1400. supply this, then the ``--dist-dir`` setting of the ``bdist`` command
  1401. will be used, which is usually a directory named ``dist`` in the project
  1402. directory.
  1403. ``--plat-name=PLATFORM, -p PLATFORM``
  1404. Set the platform name string that will be embedded in the egg's filename
  1405. (assuming the egg contains C extensions). This can be used to override
  1406. the distutils default platform name with something more meaningful. Keep
  1407. in mind, however, that the egg runtime system expects to see eggs with
  1408. distutils platform names, so it may ignore or reject eggs with non-standard
  1409. platform names. Similarly, the EasyInstall program may ignore them when
  1410. searching web pages for download links. However, if you are
  1411. cross-compiling or doing some other unusual things, you might find a use
  1412. for this option.
  1413. ``--exclude-source-files``
  1414. Don't include any modules' ``.py`` files in the egg, just compiled Python,
  1415. C, and data files. (Note that this doesn't affect any ``.py`` files in the
  1416. EGG-INFO directory or its subdirectories, since for example there may be
  1417. scripts with a ``.py`` extension which must still be retained.) We don't
  1418. recommend that you use this option except for packages that are being
  1419. bundled for proprietary end-user applications, or for "embedded" scenarios
  1420. where space is at an absolute premium. On the other hand, if your package
  1421. is going to be installed and used in compressed form, you might as well
  1422. exclude the source because Python's ``traceback`` module doesn't currently
  1423. understand how to display zipped source code anyway, or how to deal with
  1424. files that are in a different place from where their code was compiled.
  1425. There are also some options you will probably never need, but which are there
  1426. because they were copied from similar ``bdist`` commands used as an example for
  1427. creating this one. They may be useful for testing and debugging, however,
  1428. which is why we kept them:
  1429. ``--keep-temp, -k``
  1430. Keep the contents of the ``--bdist-dir`` tree around after creating the
  1431. ``.egg`` file.
  1432. ``--bdist-dir=DIR, -b DIR``
  1433. Set the temporary directory for creating the distribution. The entire
  1434. contents of this directory are zipped to create the ``.egg`` file, after
  1435. running various installation commands to copy the package's modules, data,
  1436. and extensions here.
  1437. ``--skip-build``
  1438. Skip doing any "build" commands; just go straight to the
  1439. install-and-compress phases.
  1440. .. _develop:
  1441. ``develop`` - Deploy the project source in "Development Mode"
  1442. =============================================================
  1443. This command allows you to deploy your project's source for use in one or more
  1444. "staging areas" where it will be available for importing. This deployment is
  1445. done in such a way that changes to the project source are immediately available
  1446. in the staging area(s), without needing to run a build or install step after
  1447. each change.
  1448. The ``develop`` command works by creating an ``.egg-link`` file (named for the
  1449. project) in the given staging area. If the staging area is Python's
  1450. ``site-packages`` directory, it also updates an ``easy-install.pth`` file so
  1451. that the project is on ``sys.path`` by default for all programs run using that
  1452. Python installation.
  1453. The ``develop`` command also installs wrapper scripts in the staging area (or
  1454. a separate directory, as specified) that will ensure the project's dependencies
  1455. are available on ``sys.path`` before running the project's source scripts.
  1456. And, it ensures that any missing project dependencies are available in the
  1457. staging area, by downloading and installing them if necessary.
  1458. Last, but not least, the ``develop`` command invokes the ``build_ext -i``
  1459. command to ensure any C extensions in the project have been built and are
  1460. up-to-date, and the ``egg_info`` command to ensure the project's metadata is
  1461. updated (so that the runtime and wrappers know what the project's dependencies
  1462. are). If you make any changes to the project's setup script or C extensions,
  1463. you should rerun the ``develop`` command against all relevant staging areas to
  1464. keep the project's scripts, metadata and extensions up-to-date. Most other
  1465. kinds of changes to your project should not require any build operations or
  1466. rerunning ``develop``, but keep in mind that even minor changes to the setup
  1467. script (e.g. changing an entry point definition) require you to re-run the
  1468. ``develop`` or ``test`` commands to keep the distribution updated.
  1469. Here are some of the options that the ``develop`` command accepts. Note that
  1470. they affect the project's dependencies as well as the project itself, so if you
  1471. have dependencies that need to be installed and you use ``--exclude-scripts``
  1472. (for example), the dependencies' scripts will not be installed either! For
  1473. this reason, you may want to use EasyInstall to install the project's
  1474. dependencies before using the ``develop`` command, if you need finer control
  1475. over the installation options for dependencies.
  1476. ``--uninstall, -u``
  1477. Un-deploy the current project. You may use the ``--install-dir`` or ``-d``
  1478. option to designate the staging area. The created ``.egg-link`` file will
  1479. be removed, if present and it is still pointing to the project directory.
  1480. The project directory will be removed from ``easy-install.pth`` if the
  1481. staging area is Python's ``site-packages`` directory.
  1482. Note that this option currently does *not* uninstall script wrappers! You
  1483. must uninstall them yourself, or overwrite them by using EasyInstall to
  1484. activate a different version of the package. You can also avoid installing
  1485. script wrappers in the first place, if you use the ``--exclude-scripts``
  1486. (aka ``-x``) option when you run ``develop`` to deploy the project.
  1487. ``--multi-version, -m``
  1488. "Multi-version" mode. Specifying this option prevents ``develop`` from
  1489. adding an ``easy-install.pth`` entry for the project(s) being deployed, and
  1490. if an entry for any version of a project already exists, the entry will be
  1491. removed upon successful deployment. In multi-version mode, no specific
  1492. version of the package is available for importing, unless you use
  1493. ``pkg_resources.require()`` to put it on ``sys.path``, or you are running
  1494. a wrapper script generated by ``setuptools`` or EasyInstall. (In which
  1495. case the wrapper script calls ``require()`` for you.)
  1496. Note that if you install to a directory other than ``site-packages``,
  1497. this option is automatically in effect, because ``.pth`` files can only be
  1498. used in ``site-packages`` (at least in Python 2.3 and 2.4). So, if you use
  1499. the ``--install-dir`` or ``-d`` option (or they are set via configuration
  1500. file(s)) your project and its dependencies will be deployed in multi-
  1501. version mode.
  1502. ``--install-dir=DIR, -d DIR``
  1503. Set the installation directory (staging area). If this option is not
  1504. directly specified on the command line or in a distutils configuration
  1505. file, the distutils default installation location is used. Normally, this
  1506. will be the ``site-packages`` directory, but if you are using distutils
  1507. configuration files, setting things like ``prefix`` or ``install_lib``,
  1508. then those settings are taken into account when computing the default
  1509. staging area.
  1510. ``--script-dir=DIR, -s DIR``
  1511. Set the script installation directory. If you don't supply this option
  1512. (via the command line or a configuration file), but you *have* supplied
  1513. an ``--install-dir`` (via command line or config file), then this option
  1514. defaults to the same directory, so that the scripts will be able to find
  1515. their associated package installation. Otherwise, this setting defaults
  1516. to the location where the distutils would normally install scripts, taking
  1517. any distutils configuration file settings into account.
  1518. ``--exclude-scripts, -x``
  1519. Don't deploy script wrappers. This is useful if you don't want to disturb
  1520. existing versions of the scripts in the staging area.
  1521. ``--always-copy, -a``
  1522. Copy all needed distributions to the staging area, even if they
  1523. are already present in another directory on ``sys.path``. By default, if
  1524. a requirement can be met using a distribution that is already available in
  1525. a directory on ``sys.path``, it will not be copied to the staging area.
  1526. ``--egg-path=DIR``
  1527. Force the generated ``.egg-link`` file to use a specified relative path
  1528. to the source directory. This can be useful in circumstances where your
  1529. installation directory is being shared by code running under multiple
  1530. platforms (e.g. Mac and Windows) which have different absolute locations
  1531. for the code under development, but the same *relative* locations with
  1532. respect to the installation directory. If you use this option when
  1533. installing, you must supply the same relative path when uninstalling.
  1534. In addition to the above options, the ``develop`` command also accepts all of
  1535. the same options accepted by ``easy_install``. If you've configured any
  1536. ``easy_install`` settings in your ``setup.cfg`` (or other distutils config
  1537. files), the ``develop`` command will use them as defaults, unless you override
  1538. them in a ``[develop]`` section or on the command line.
  1539. ``easy_install`` - Find and install packages
  1540. ============================================
  1541. This command runs the `EasyInstall tool
  1542. <easy_install.html>`_ for you. It is exactly
  1543. equivalent to running the ``easy_install`` command. All command line arguments
  1544. following this command are consumed and not processed further by the distutils,
  1545. so this must be the last command listed on the command line. Please see
  1546. the EasyInstall documentation for the options reference and usage examples.
  1547. Normally, there is no reason to use this command via the command line, as you
  1548. can just use ``easy_install`` directly. It's only listed here so that you know
  1549. it's a distutils command, which means that you can:
  1550. * create command aliases that use it,
  1551. * create distutils extensions that invoke it as a subcommand, and
  1552. * configure options for it in your ``setup.cfg`` or other distutils config
  1553. files.
  1554. .. _egg_info:
  1555. ``egg_info`` - Create egg metadata and set build tags
  1556. =====================================================
  1557. This command performs two operations: it updates a project's ``.egg-info``
  1558. metadata directory (used by the ``bdist_egg``, ``develop``, and ``test``
  1559. commands), and it allows you to temporarily change a project's version string,
  1560. to support "daily builds" or "snapshot" releases. It is run automatically by
  1561. the ``sdist``, ``bdist_egg``, ``develop``, ``register``, and ``test`` commands
  1562. in order to update the project's metadata, but you can also specify it
  1563. explicitly in order to temporarily change the project's version string while
  1564. executing other commands. (It also generates the``.egg-info/SOURCES.txt``
  1565. manifest file, which is used when you are building source distributions.)
  1566. In addition to writing the core egg metadata defined by ``setuptools`` and
  1567. required by ``pkg_resources``, this command can be extended to write other
  1568. metadata files as well, by defining entry points in the ``egg_info.writers``
  1569. group. See the section on `Adding new EGG-INFO Files`_ below for more details.
  1570. Note that using additional metadata writers may require you to include a
  1571. ``setup_requires`` argument to ``setup()`` in order to ensure that the desired
  1572. writers are available on ``sys.path``.
  1573. Release Tagging Options
  1574. -----------------------
  1575. The following options can be used to modify the project's version string for
  1576. all remaining commands on the setup command line. The options are processed
  1577. in the order shown, so if you use more than one, the requested tags will be
  1578. added in the following order:
  1579. ``--tag-build=NAME, -b NAME``
  1580. Append NAME to the project's version string. Due to the way setuptools
  1581. processes "pre-release" version suffixes beginning with the letters "a"
  1582. through "e" (like "alpha", "beta", and "candidate"), you will usually want
  1583. to use a tag like ".build" or ".dev", as this will cause the version number
  1584. to be considered *lower* than the project's default version. (If you
  1585. want to make the version number *higher* than the default version, you can
  1586. always leave off --tag-build and then use one or both of the following
  1587. options.)
  1588. If you have a default build tag set in your ``setup.cfg``, you can suppress
  1589. it on the command line using ``-b ""`` or ``--tag-build=""`` as an argument
  1590. to the ``egg_info`` command.
  1591. ``--tag-svn-revision, -r``
  1592. If the current directory is a Subversion checkout (i.e. has a ``.svn``
  1593. subdirectory, this appends a string of the form "-rNNNN" to the project's
  1594. version string, where NNNN is the revision number of the most recent
  1595. modification to the current directory, as obtained from the ``svn info``
  1596. command.
  1597. If the current directory is not a Subversion checkout, the command will
  1598. look for a ``PKG-INFO`` file instead, and try to find the revision number
  1599. from that, by looking for a "-rNNNN" string at the end of the version
  1600. number. (This is so that building a package from a source distribution of
  1601. a Subversion snapshot will produce a binary with the correct version
  1602. number.)
  1603. If there is no ``PKG-INFO`` file, or the version number contained therein
  1604. does not end with ``-r`` and a number, then ``-r0`` is used.
  1605. ``--no-svn-revision, -R``
  1606. Don't include the Subversion revision in the version number. This option
  1607. is included so you can override a default setting put in ``setup.cfg``.
  1608. ``--tag-date, -d``
  1609. Add a date stamp of the form "-YYYYMMDD" (e.g. "-20050528") to the
  1610. project's version number.
  1611. ``--no-date, -D``
  1612. Don't include a date stamp in the version number. This option is included
  1613. so you can override a default setting in ``setup.cfg``.
  1614. (Note: Because these options modify the version number used for source and
  1615. binary distributions of your project, you should first make sure that you know
  1616. how the resulting version numbers will be interpreted by automated tools
  1617. like EasyInstall. See the section above on `Specifying Your Project's
  1618. Version`_ for an explanation of pre- and post-release tags, as well as tips on
  1619. how to choose and verify a versioning scheme for your your project.)
  1620. For advanced uses, there is one other option that can be set, to change the
  1621. location of the project's ``.egg-info`` directory. Commands that need to find
  1622. the project's source directory or metadata should get it from this setting:
  1623. Other ``egg_info`` Options
  1624. --------------------------
  1625. ``--egg-base=SOURCEDIR, -e SOURCEDIR``
  1626. Specify the directory that should contain the .egg-info directory. This
  1627. should normally be the root of your project's source tree (which is not
  1628. necessarily the same as your project directory; some projects use a ``src``
  1629. or ``lib`` subdirectory as the source root). You should not normally need
  1630. to specify this directory, as it is normally determined from the
  1631. ``package_dir`` argument to the ``setup()`` function, if any. If there is
  1632. no ``package_dir`` set, this option defaults to the current directory.
  1633. ``egg_info`` Examples
  1634. ---------------------
  1635. Creating a dated "nightly build" snapshot egg::
  1636. python setup.py egg_info --tag-date --tag-build=DEV bdist_egg
  1637. Creating and uploading a release with no version tags, even if some default
  1638. tags are specified in ``setup.cfg``::
  1639. python setup.py egg_info -RDb "" sdist bdist_egg register upload
  1640. (Notice that ``egg_info`` must always appear on the command line *before* any
  1641. commands that you want the version changes to apply to.)
  1642. .. _install command:
  1643. ``install`` - Run ``easy_install`` or old-style installation
  1644. ============================================================
  1645. The setuptools ``install`` command is basically a shortcut to run the
  1646. ``easy_install`` command on the current project. However, for convenience
  1647. in creating "system packages" of setuptools-based projects, you can also
  1648. use this option:
  1649. ``--single-version-externally-managed``
  1650. This boolean option tells the ``install`` command to perform an "old style"
  1651. installation, with the addition of an ``.egg-info`` directory so that the
  1652. installed project will still have its metadata available and operate
  1653. normally. If you use this option, you *must* also specify the ``--root``
  1654. or ``--record`` options (or both), because otherwise you will have no way
  1655. to identify and remove the installed files.
  1656. This option is automatically in effect when ``install`` is invoked by another
  1657. distutils command, so that commands like ``bdist_wininst`` and ``bdist_rpm``
  1658. will create system packages of eggs. It is also automatically in effect if
  1659. you specify the ``--root`` option.
  1660. ``install_egg_info`` - Install an ``.egg-info`` directory in ``site-packages``
  1661. ==============================================================================
  1662. Setuptools runs this command as part of ``install`` operations that use the
  1663. ``--single-version-externally-managed`` options. You should not invoke it
  1664. directly; it is documented here for completeness and so that distutils
  1665. extensions such as system package builders can make use of it. This command
  1666. has only one option:
  1667. ``--install-dir=DIR, -d DIR``
  1668. The parent directory where the ``.egg-info`` directory will be placed.
  1669. Defaults to the same as the ``--install-dir`` option specified for the
  1670. ``install_lib`` command, which is usually the system ``site-packages``
  1671. directory.
  1672. This command assumes that the ``egg_info`` command has been given valid options
  1673. via the command line or ``setup.cfg``, as it will invoke the ``egg_info``
  1674. command and use its options to locate the project's source ``.egg-info``
  1675. directory.
  1676. .. _rotate:
  1677. ``rotate`` - Delete outdated distribution files
  1678. ===============================================
  1679. As you develop new versions of your project, your distribution (``dist``)
  1680. directory will gradually fill up with older source and/or binary distribution
  1681. files. The ``rotate`` command lets you automatically clean these up, keeping
  1682. only the N most-recently modified files matching a given pattern.
  1683. ``--match=PATTERNLIST, -m PATTERNLIST``
  1684. Comma-separated list of glob patterns to match. This option is *required*.
  1685. The project name and ``-*`` is prepended to the supplied patterns, in order
  1686. to match only distributions belonging to the current project (in case you
  1687. have a shared distribution directory for multiple projects). Typically,
  1688. you will use a glob pattern like ``.zip`` or ``.egg`` to match files of
  1689. the specified type. Note that each supplied pattern is treated as a
  1690. distinct group of files for purposes of selecting files to delete.
  1691. ``--keep=COUNT, -k COUNT``
  1692. Number of matching distributions to keep. For each group of files
  1693. identified by a pattern specified with the ``--match`` option, delete all
  1694. but the COUNT most-recently-modified files in that group. This option is
  1695. *required*.
  1696. ``--dist-dir=DIR, -d DIR``
  1697. Directory where the distributions are. This defaults to the value of the
  1698. ``bdist`` command's ``--dist-dir`` option, which will usually be the
  1699. project's ``dist`` subdirectory.
  1700. **Example 1**: Delete all .tar.gz files from the distribution directory, except
  1701. for the 3 most recently modified ones::
  1702. setup.py rotate --match=.tar.gz --keep=3
  1703. **Example 2**: Delete all Python 2.3 or Python 2.4 eggs from the distribution
  1704. directory, except the most recently modified one for each Python version::
  1705. setup.py rotate --match=-py2.3*.egg,-py2.4*.egg --keep=1
  1706. .. _saveopts:
  1707. ``saveopts`` - Save used options to a configuration file
  1708. ========================================================
  1709. Finding and editing ``distutils`` configuration files can be a pain, especially
  1710. since you also have to translate the configuration options from command-line
  1711. form to the proper configuration file format. You can avoid these hassles by
  1712. using the ``saveopts`` command. Just add it to the command line to save the
  1713. options you used. For example, this command builds the project using
  1714. the ``mingw32`` C compiler, then saves the --compiler setting as the default
  1715. for future builds (even those run implicitly by the ``install`` command)::
  1716. setup.py build --compiler=mingw32 saveopts
  1717. The ``saveopts`` command saves all options for every command specified on the
  1718. command line to the project's local ``setup.cfg`` file, unless you use one of
  1719. the `configuration file options`_ to change where the options are saved. For
  1720. example, this command does the same as above, but saves the compiler setting
  1721. to the site-wide (global) distutils configuration::
  1722. setup.py build --compiler=mingw32 saveopts -g
  1723. Note that it doesn't matter where you place the ``saveopts`` command on the
  1724. command line; it will still save all the options specified for all commands.
  1725. For example, this is another valid way to spell the last example::
  1726. setup.py saveopts -g build --compiler=mingw32
  1727. Note, however, that all of the commands specified are always run, regardless of
  1728. where ``saveopts`` is placed on the command line.
  1729. Configuration File Options
  1730. --------------------------
  1731. Normally, settings such as options and aliases are saved to the project's
  1732. local ``setup.cfg`` file. But you can override this and save them to the
  1733. global or per-user configuration files, or to a manually-specified filename.
  1734. ``--global-config, -g``
  1735. Save settings to the global ``distutils.cfg`` file inside the ``distutils``
  1736. package directory. You must have write access to that directory to use
  1737. this option. You also can't combine this option with ``-u`` or ``-f``.
  1738. ``--user-config, -u``
  1739. Save settings to the current user's ``~/.pydistutils.cfg`` (POSIX) or
  1740. ``$HOME/pydistutils.cfg`` (Windows) file. You can't combine this option
  1741. with ``-g`` or ``-f``.
  1742. ``--filename=FILENAME, -f FILENAME``
  1743. Save settings to the specified configuration file to use. You can't
  1744. combine this option with ``-g`` or ``-u``. Note that if you specify a
  1745. non-standard filename, the ``distutils`` and ``setuptools`` will not
  1746. use the file's contents. This option is mainly included for use in
  1747. testing.
  1748. These options are used by other ``setuptools`` commands that modify
  1749. configuration files, such as the `alias`_ and `setopt`_ commands.
  1750. .. _setopt:
  1751. ``setopt`` - Set a distutils or setuptools option in a config file
  1752. ==================================================================
  1753. This command is mainly for use by scripts, but it can also be used as a quick
  1754. and dirty way to change a distutils configuration option without having to
  1755. remember what file the options are in and then open an editor.
  1756. **Example 1**. Set the default C compiler to ``mingw32`` (using long option
  1757. names)::
  1758. setup.py setopt --command=build --option=compiler --set-value=mingw32
  1759. **Example 2**. Remove any setting for the distutils default package
  1760. installation directory (short option names)::
  1761. setup.py setopt -c install -o install_lib -r
  1762. Options for the ``setopt`` command:
  1763. ``--command=COMMAND, -c COMMAND``
  1764. Command to set the option for. This option is required.
  1765. ``--option=OPTION, -o OPTION``
  1766. The name of the option to set. This option is required.
  1767. ``--set-value=VALUE, -s VALUE``
  1768. The value to set the option to. Not needed if ``-r`` or ``--remove`` is
  1769. set.
  1770. ``--remove, -r``
  1771. Remove (unset) the option, instead of setting it.
  1772. In addition to the above options, you may use any of the `configuration file
  1773. options`_ (listed under the `saveopts`_ command, above) to determine which
  1774. distutils configuration file the option will be added to (or removed from).
  1775. .. _test:
  1776. ``test`` - Build package and run a unittest suite
  1777. =================================================
  1778. When doing test-driven development, or running automated builds that need
  1779. testing before they are deployed for downloading or use, it's often useful
  1780. to be able to run a project's unit tests without actually deploying the project
  1781. anywhere, even using the ``develop`` command. The ``test`` command runs a
  1782. project's unit tests without actually deploying it, by temporarily putting the
  1783. project's source on ``sys.path``, after first running ``build_ext -i`` and
  1784. ``egg_info`` to ensure that any C extensions and project metadata are
  1785. up-to-date.
  1786. To use this command, your project's tests must be wrapped in a ``unittest``
  1787. test suite by either a function, a ``TestCase`` class or method, or a module
  1788. or package containing ``TestCase`` classes. If the named suite is a module,
  1789. and the module has an ``additional_tests()`` function, it is called and the
  1790. result (which must be a ``unittest.TestSuite``) is added to the tests to be
  1791. run. If the named suite is a package, any submodules and subpackages are
  1792. recursively added to the overall test suite. (Note: if your project specifies
  1793. a ``test_loader``, the rules for processing the chosen ``test_suite`` may
  1794. differ; see the `test_loader`_ documentation for more details.)
  1795. Note that many test systems including ``doctest`` support wrapping their
  1796. non-``unittest`` tests in ``TestSuite`` objects. So, if you are using a test
  1797. package that does not support this, we suggest you encourage its developers to
  1798. implement test suite support, as this is a convenient and standard way to
  1799. aggregate a collection of tests to be run under a common test harness.
  1800. By default, tests will be run in the "verbose" mode of the ``unittest``
  1801. package's text test runner, but you can get the "quiet" mode (just dots) if
  1802. you supply the ``-q`` or ``--quiet`` option, either as a global option to
  1803. the setup script (e.g. ``setup.py -q test``) or as an option for the ``test``
  1804. command itself (e.g. ``setup.py test -q``). There is one other option
  1805. available:
  1806. ``--test-suite=NAME, -s NAME``
  1807. Specify the test suite (or module, class, or method) to be run
  1808. (e.g. ``some_module.test_suite``). The default for this option can be
  1809. set by giving a ``test_suite`` argument to the ``setup()`` function, e.g.::
  1810. setup(
  1811. # ...
  1812. test_suite = "my_package.tests.test_all"
  1813. )
  1814. If you did not set a ``test_suite`` in your ``setup()`` call, and do not
  1815. provide a ``--test-suite`` option, an error will occur.
  1816. .. _upload:
  1817. ``upload`` - Upload source and/or egg distributions to PyPI
  1818. ===========================================================
  1819. The ``upload`` command is implemented and `documented
  1820. <https://docs.python.org/3.1/distutils/uploading.html>`_
  1821. in distutils.
  1822. Setuptools augments the ``upload`` command with support
  1823. for `keyring <https://pypi.python.org/pypi/keyring>`_,
  1824. allowing the password to be stored in a secure
  1825. location and not in plaintext in the .pypirc file. To use
  1826. keyring, first install keyring and set the password for
  1827. the relevant repository, e.g.::
  1828. python -m keyring set <repository> <username>
  1829. Password for '<username>' in '<repository>': ********
  1830. Then, in .pypirc, set the repository configuration as normal,
  1831. but omit the password. Thereafter, uploads will use the
  1832. password from the keyring.
  1833. New in 20.1: Added keyring support.
  1834. .. _upload_docs:
  1835. ``upload_docs`` - Upload package documentation to PyPI
  1836. ======================================================
  1837. PyPI now supports uploading project documentation to the dedicated URL
  1838. https://pythonhosted.org/<project>/.
  1839. The ``upload_docs`` command will create the necessary zip file out of a
  1840. documentation directory and will post to the repository.
  1841. Note that to upload the documentation of a project, the corresponding version
  1842. must already be registered with PyPI, using the distutils ``register``
  1843. command -- just like the ``upload`` command.
  1844. Assuming there is an ``Example`` project with documentation in the
  1845. subdirectory ``docs``, e.g.::
  1846. Example/
  1847. |-- example.py
  1848. |-- setup.cfg
  1849. |-- setup.py
  1850. |-- docs
  1851. | |-- build
  1852. | | `-- html
  1853. | | | |-- index.html
  1854. | | | `-- tips_tricks.html
  1855. | |-- conf.py
  1856. | |-- index.txt
  1857. | `-- tips_tricks.txt
  1858. You can simply pass the documentation directory path to the ``upload_docs``
  1859. command::
  1860. python setup.py upload_docs --upload-dir=docs/build/html
  1861. If no ``--upload-dir`` is given, ``upload_docs`` will attempt to run the
  1862. ``build_sphinx`` command to generate uploadable documentation.
  1863. For the command to become available, `Sphinx <http://sphinx.pocoo.org/>`_
  1864. must be installed in the same environment as distribute.
  1865. As with other ``setuptools``-based commands, you can define useful
  1866. defaults in the ``setup.cfg`` of your Python project, e.g.:
  1867. .. code-block:: ini
  1868. [upload_docs]
  1869. upload-dir = docs/build/html
  1870. The ``upload_docs`` command has the following options:
  1871. ``--upload-dir``
  1872. The directory to be uploaded to the repository.
  1873. ``--show-response``
  1874. Display the full response text from server; this is useful for debugging
  1875. PyPI problems.
  1876. ``--repository=URL, -r URL``
  1877. The URL of the repository to upload to. Defaults to
  1878. https://pypi.python.org/pypi (i.e., the main PyPI installation).
  1879. --------------------------------
  1880. Extending and Reusing Setuptools
  1881. --------------------------------
  1882. Creating ``distutils`` Extensions
  1883. =================================
  1884. It can be hard to add new commands or setup arguments to the distutils. But
  1885. the ``setuptools`` package makes it a bit easier, by allowing you to distribute
  1886. a distutils extension as a separate project, and then have projects that need
  1887. the extension just refer to it in their ``setup_requires`` argument.
  1888. With ``setuptools``, your distutils extension projects can hook in new
  1889. commands and ``setup()`` arguments just by defining "entry points". These
  1890. are mappings from command or argument names to a specification of where to
  1891. import a handler from. (See the section on `Dynamic Discovery of Services and
  1892. Plugins`_ above for some more background on entry points.)
  1893. Adding Commands
  1894. ---------------
  1895. You can add new ``setup`` commands by defining entry points in the
  1896. ``distutils.commands`` group. For example, if you wanted to add a ``foo``
  1897. command, you might add something like this to your distutils extension
  1898. project's setup script::
  1899. setup(
  1900. # ...
  1901. entry_points = {
  1902. "distutils.commands": [
  1903. "foo = mypackage.some_module:foo",
  1904. ],
  1905. },
  1906. )
  1907. (Assuming, of course, that the ``foo`` class in ``mypackage.some_module`` is
  1908. a ``setuptools.Command`` subclass.)
  1909. Once a project containing such entry points has been activated on ``sys.path``,
  1910. (e.g. by running "install" or "develop" with a site-packages installation
  1911. directory) the command(s) will be available to any ``setuptools``-based setup
  1912. scripts. It is not necessary to use the ``--command-packages`` option or
  1913. to monkeypatch the ``distutils.command`` package to install your commands;
  1914. ``setuptools`` automatically adds a wrapper to the distutils to search for
  1915. entry points in the active distributions on ``sys.path``. In fact, this is
  1916. how setuptools' own commands are installed: the setuptools project's setup
  1917. script defines entry points for them!
  1918. Adding ``setup()`` Arguments
  1919. ----------------------------
  1920. Sometimes, your commands may need additional arguments to the ``setup()``
  1921. call. You can enable this by defining entry points in the
  1922. ``distutils.setup_keywords`` group. For example, if you wanted a ``setup()``
  1923. argument called ``bar_baz``, you might add something like this to your
  1924. distutils extension project's setup script::
  1925. setup(
  1926. # ...
  1927. entry_points = {
  1928. "distutils.commands": [
  1929. "foo = mypackage.some_module:foo",
  1930. ],
  1931. "distutils.setup_keywords": [
  1932. "bar_baz = mypackage.some_module:validate_bar_baz",
  1933. ],
  1934. },
  1935. )
  1936. The idea here is that the entry point defines a function that will be called
  1937. to validate the ``setup()`` argument, if it's supplied. The ``Distribution``
  1938. object will have the initial value of the attribute set to ``None``, and the
  1939. validation function will only be called if the ``setup()`` call sets it to
  1940. a non-None value. Here's an example validation function::
  1941. def assert_bool(dist, attr, value):
  1942. """Verify that value is True, False, 0, or 1"""
  1943. if bool(value) != value:
  1944. raise DistutilsSetupError(
  1945. "%r must be a boolean value (got %r)" % (attr,value)
  1946. )
  1947. Your function should accept three arguments: the ``Distribution`` object,
  1948. the attribute name, and the attribute value. It should raise a
  1949. ``DistutilsSetupError`` (from the ``distutils.errors`` module) if the argument
  1950. is invalid. Remember, your function will only be called with non-None values,
  1951. and the default value of arguments defined this way is always None. So, your
  1952. commands should always be prepared for the possibility that the attribute will
  1953. be ``None`` when they access it later.
  1954. If more than one active distribution defines an entry point for the same
  1955. ``setup()`` argument, *all* of them will be called. This allows multiple
  1956. distutils extensions to define a common argument, as long as they agree on
  1957. what values of that argument are valid.
  1958. Also note that as with commands, it is not necessary to subclass or monkeypatch
  1959. the distutils ``Distribution`` class in order to add your arguments; it is
  1960. sufficient to define the entry points in your extension, as long as any setup
  1961. script using your extension lists your project in its ``setup_requires``
  1962. argument.
  1963. Adding new EGG-INFO Files
  1964. -------------------------
  1965. Some extensible applications or frameworks may want to allow third parties to
  1966. develop plugins with application or framework-specific metadata included in
  1967. the plugins' EGG-INFO directory, for easy access via the ``pkg_resources``
  1968. metadata API. The easiest way to allow this is to create a distutils extension
  1969. to be used from the plugin projects' setup scripts (via ``setup_requires``)
  1970. that defines a new setup keyword, and then uses that data to write an EGG-INFO
  1971. file when the ``egg_info`` command is run.
  1972. The ``egg_info`` command looks for extension points in an ``egg_info.writers``
  1973. group, and calls them to write the files. Here's a simple example of a
  1974. distutils extension defining a setup argument ``foo_bar``, which is a list of
  1975. lines that will be written to ``foo_bar.txt`` in the EGG-INFO directory of any
  1976. project that uses the argument::
  1977. setup(
  1978. # ...
  1979. entry_points = {
  1980. "distutils.setup_keywords": [
  1981. "foo_bar = setuptools.dist:assert_string_list",
  1982. ],
  1983. "egg_info.writers": [
  1984. "foo_bar.txt = setuptools.command.egg_info:write_arg",
  1985. ],
  1986. },
  1987. )
  1988. This simple example makes use of two utility functions defined by setuptools
  1989. for its own use: a routine to validate that a setup keyword is a sequence of
  1990. strings, and another one that looks up a setup argument and writes it to
  1991. a file. Here's what the writer utility looks like::
  1992. def write_arg(cmd, basename, filename):
  1993. argname = os.path.splitext(basename)[0]
  1994. value = getattr(cmd.distribution, argname, None)
  1995. if value is not None:
  1996. value = '\n'.join(value)+'\n'
  1997. cmd.write_or_delete_file(argname, filename, value)
  1998. As you can see, ``egg_info.writers`` entry points must be a function taking
  1999. three arguments: a ``egg_info`` command instance, the basename of the file to
  2000. write (e.g. ``foo_bar.txt``), and the actual full filename that should be
  2001. written to.
  2002. In general, writer functions should honor the command object's ``dry_run``
  2003. setting when writing files, and use the ``distutils.log`` object to do any
  2004. console output. The easiest way to conform to this requirement is to use
  2005. the ``cmd`` object's ``write_file()``, ``delete_file()``, and
  2006. ``write_or_delete_file()`` methods exclusively for your file operations. See
  2007. those methods' docstrings for more details.
  2008. Adding Support for Revision Control Systems
  2009. -------------------------------------------------
  2010. If the files you want to include in the source distribution are tracked using
  2011. Git, Mercurial or SVN, you can use the following packages to achieve that:
  2012. - Git and Mercurial: `setuptools_scm <https://pypi.python.org/pypi/setuptools_scm>`_
  2013. - SVN: `setuptools_svn <https://pypi.python.org/pypi/setuptools_svn>`_
  2014. If you would like to create a plugin for ``setuptools`` to find files tracked
  2015. by another revision control system, you can do so by adding an entry point to
  2016. the ``setuptools.file_finders`` group. The entry point should be a function
  2017. accepting a single directory name, and should yield all the filenames within
  2018. that directory (and any subdirectories thereof) that are under revision
  2019. control.
  2020. For example, if you were going to create a plugin for a revision control system
  2021. called "foobar", you would write a function something like this:
  2022. .. code-block:: python
  2023. def find_files_for_foobar(dirname):
  2024. # loop to yield paths that start with `dirname`
  2025. And you would register it in a setup script using something like this::
  2026. entry_points = {
  2027. "setuptools.file_finders": [
  2028. "foobar = my_foobar_module:find_files_for_foobar"
  2029. ]
  2030. }
  2031. Then, anyone who wants to use your plugin can simply install it, and their
  2032. local setuptools installation will be able to find the necessary files.
  2033. It is not necessary to distribute source control plugins with projects that
  2034. simply use the other source control system, or to specify the plugins in
  2035. ``setup_requires``. When you create a source distribution with the ``sdist``
  2036. command, setuptools automatically records what files were found in the
  2037. ``SOURCES.txt`` file. That way, recipients of source distributions don't need
  2038. to have revision control at all. However, if someone is working on a package
  2039. by checking out with that system, they will need the same plugin(s) that the
  2040. original author is using.
  2041. A few important points for writing revision control file finders:
  2042. * Your finder function MUST return relative paths, created by appending to the
  2043. passed-in directory name. Absolute paths are NOT allowed, nor are relative
  2044. paths that reference a parent directory of the passed-in directory.
  2045. * Your finder function MUST accept an empty string as the directory name,
  2046. meaning the current directory. You MUST NOT convert this to a dot; just
  2047. yield relative paths. So, yielding a subdirectory named ``some/dir`` under
  2048. the current directory should NOT be rendered as ``./some/dir`` or
  2049. ``/somewhere/some/dir``, but *always* as simply ``some/dir``
  2050. * Your finder function SHOULD NOT raise any errors, and SHOULD deal gracefully
  2051. with the absence of needed programs (i.e., ones belonging to the revision
  2052. control system itself. It *may*, however, use ``distutils.log.warn()`` to
  2053. inform the user of the missing program(s).
  2054. Subclassing ``Command``
  2055. -----------------------
  2056. Sorry, this section isn't written yet, and neither is a lot of what's below
  2057. this point.
  2058. XXX
  2059. Reusing ``setuptools`` Code
  2060. ===========================
  2061. ``ez_setup``
  2062. ------------
  2063. XXX
  2064. ``setuptools.archive_util``
  2065. ---------------------------
  2066. XXX
  2067. ``setuptools.sandbox``
  2068. ----------------------
  2069. XXX
  2070. ``setuptools.package_index``
  2071. ----------------------------
  2072. XXX
  2073. Mailing List and Bug Tracker
  2074. ============================
  2075. Please use the `distutils-sig mailing list`_ for questions and discussion about
  2076. setuptools, and the `setuptools bug tracker`_ ONLY for issues you have
  2077. confirmed via the list are actual bugs, and which you have reduced to a minimal
  2078. set of steps to reproduce.
  2079. .. _distutils-sig mailing list: http://mail.python.org/pipermail/distutils-sig/
  2080. .. _setuptools bug tracker: https://github.com/pypa/setuptools/