/Doc/distutils/examples.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 241 lines · 179 code · 62 blank · 0 comment · 0 complexity · 8ae5d6df9f32fc265c3ba2279d7f1b46 MD5 · raw file

  1. .. _examples:
  2. ********
  3. Examples
  4. ********
  5. This chapter provides a number of basic examples to help get started with
  6. distutils. Additional information about using distutils can be found in the
  7. Distutils Cookbook.
  8. .. seealso::
  9. `Distutils Cookbook <http://wiki.python.org/moin/Distutils/Cookbook>`_
  10. Collection of recipes showing how to achieve more control over distutils.
  11. .. _pure-mod:
  12. Pure Python distribution (by module)
  13. ====================================
  14. If you're just distributing a couple of modules, especially if they don't live
  15. in a particular package, you can specify them individually using the
  16. :option:`py_modules` option in the setup script.
  17. In the simplest case, you'll have two files to worry about: a setup script and
  18. the single module you're distributing, :file:`foo.py` in this example::
  19. <root>/
  20. setup.py
  21. foo.py
  22. (In all diagrams in this section, *<root>* will refer to the distribution root
  23. directory.) A minimal setup script to describe this situation would be::
  24. from distutils.core import setup
  25. setup(name='foo',
  26. version='1.0',
  27. py_modules=['foo'],
  28. )
  29. Note that the name of the distribution is specified independently with the
  30. :option:`name` option, and there's no rule that says it has to be the same as
  31. the name of the sole module in the distribution (although that's probably a good
  32. convention to follow). However, the distribution name is used to generate
  33. filenames, so you should stick to letters, digits, underscores, and hyphens.
  34. Since :option:`py_modules` is a list, you can of course specify multiple
  35. modules, eg. if you're distributing modules :mod:`foo` and :mod:`bar`, your
  36. setup might look like this::
  37. <root>/
  38. setup.py
  39. foo.py
  40. bar.py
  41. and the setup script might be ::
  42. from distutils.core import setup
  43. setup(name='foobar',
  44. version='1.0',
  45. py_modules=['foo', 'bar'],
  46. )
  47. You can put module source files into another directory, but if you have enough
  48. modules to do that, it's probably easier to specify modules by package rather
  49. than listing them individually.
  50. .. _pure-pkg:
  51. Pure Python distribution (by package)
  52. =====================================
  53. If you have more than a couple of modules to distribute, especially if they are
  54. in multiple packages, it's probably easier to specify whole packages rather than
  55. individual modules. This works even if your modules are not in a package; you
  56. can just tell the Distutils to process modules from the root package, and that
  57. works the same as any other package (except that you don't have to have an
  58. :file:`__init__.py` file).
  59. The setup script from the last example could also be written as ::
  60. from distutils.core import setup
  61. setup(name='foobar',
  62. version='1.0',
  63. packages=[''],
  64. )
  65. (The empty string stands for the root package.)
  66. If those two files are moved into a subdirectory, but remain in the root
  67. package, e.g.::
  68. <root>/
  69. setup.py
  70. src/ foo.py
  71. bar.py
  72. then you would still specify the root package, but you have to tell the
  73. Distutils where source files in the root package live::
  74. from distutils.core import setup
  75. setup(name='foobar',
  76. version='1.0',
  77. package_dir={'': 'src'},
  78. packages=[''],
  79. )
  80. More typically, though, you will want to distribute multiple modules in the same
  81. package (or in sub-packages). For example, if the :mod:`foo` and :mod:`bar`
  82. modules belong in package :mod:`foobar`, one way to layout your source tree is
  83. ::
  84. <root>/
  85. setup.py
  86. foobar/
  87. __init__.py
  88. foo.py
  89. bar.py
  90. This is in fact the default layout expected by the Distutils, and the one that
  91. requires the least work to describe in your setup script::
  92. from distutils.core import setup
  93. setup(name='foobar',
  94. version='1.0',
  95. packages=['foobar'],
  96. )
  97. If you want to put modules in directories not named for their package, then you
  98. need to use the :option:`package_dir` option again. For example, if the
  99. :file:`src` directory holds modules in the :mod:`foobar` package::
  100. <root>/
  101. setup.py
  102. src/
  103. __init__.py
  104. foo.py
  105. bar.py
  106. an appropriate setup script would be ::
  107. from distutils.core import setup
  108. setup(name='foobar',
  109. version='1.0',
  110. package_dir={'foobar': 'src'},
  111. packages=['foobar'],
  112. )
  113. Or, you might put modules from your main package right in the distribution
  114. root::
  115. <root>/
  116. setup.py
  117. __init__.py
  118. foo.py
  119. bar.py
  120. in which case your setup script would be ::
  121. from distutils.core import setup
  122. setup(name='foobar',
  123. version='1.0',
  124. package_dir={'foobar': ''},
  125. packages=['foobar'],
  126. )
  127. (The empty string also stands for the current directory.)
  128. If you have sub-packages, they must be explicitly listed in :option:`packages`,
  129. but any entries in :option:`package_dir` automatically extend to sub-packages.
  130. (In other words, the Distutils does *not* scan your source tree, trying to
  131. figure out which directories correspond to Python packages by looking for
  132. :file:`__init__.py` files.) Thus, if the default layout grows a sub-package::
  133. <root>/
  134. setup.py
  135. foobar/
  136. __init__.py
  137. foo.py
  138. bar.py
  139. subfoo/
  140. __init__.py
  141. blah.py
  142. then the corresponding setup script would be ::
  143. from distutils.core import setup
  144. setup(name='foobar',
  145. version='1.0',
  146. packages=['foobar', 'foobar.subfoo'],
  147. )
  148. (Again, the empty string in :option:`package_dir` stands for the current
  149. directory.)
  150. .. _single-ext:
  151. Single extension module
  152. =======================
  153. Extension modules are specified using the :option:`ext_modules` option.
  154. :option:`package_dir` has no effect on where extension source files are found;
  155. it only affects the source for pure Python modules. The simplest case, a
  156. single extension module in a single C source file, is::
  157. <root>/
  158. setup.py
  159. foo.c
  160. If the :mod:`foo` extension belongs in the root package, the setup script for
  161. this could be ::
  162. from distutils.core import setup
  163. from distutils.extension import Extension
  164. setup(name='foobar',
  165. version='1.0',
  166. ext_modules=[Extension('foo', ['foo.c'])],
  167. )
  168. If the extension actually belongs in a package, say :mod:`foopkg`, then
  169. With exactly the same source tree layout, this extension can be put in the
  170. :mod:`foopkg` package simply by changing the name of the extension::
  171. from distutils.core import setup
  172. from distutils.extension import Extension
  173. setup(name='foobar',
  174. version='1.0',
  175. ext_modules=[Extension('foopkg.foo', ['foo.c'])],
  176. )
  177. .. % \section{Multiple extension modules}
  178. .. % \label{multiple-ext}
  179. .. % \section{Putting it all together}