blob: 2ca76a096e8bde92fae526de74831c8965bfe17a [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _examples:
2
3********
4Examples
5********
6
7This chapter provides a number of basic examples to help get started with
8distutils. Additional information about using distutils can be found in the
9Distutils Cookbook.
10
11
12.. seealso::
13
Georg Brandle73778c2014-10-29 08:36:35 +010014 `Distutils Cookbook <https://wiki.python.org/moin/Distutils/Cookbook>`_
Georg Brandl116aa622007-08-15 14:28:22 +000015 Collection of recipes showing how to achieve more control over distutils.
16
17
18.. _pure-mod:
19
20Pure Python distribution (by module)
21====================================
22
23If you're just distributing a couple of modules, especially if they don't live
24in a particular package, you can specify them individually using the
Georg Brandl3f40c402014-09-21 00:35:08 +020025``py_modules`` option in the setup script.
Georg Brandl116aa622007-08-15 14:28:22 +000026
27In the simplest case, you'll have two files to worry about: a setup script and
28the single module you're distributing, :file:`foo.py` in this example::
29
30 <root>/
31 setup.py
32 foo.py
33
34(In all diagrams in this section, *<root>* will refer to the distribution root
35directory.) A minimal setup script to describe this situation would be::
36
37 from distutils.core import setup
38 setup(name='foo',
39 version='1.0',
40 py_modules=['foo'],
41 )
42
43Note that the name of the distribution is specified independently with the
Georg Brandl3f40c402014-09-21 00:35:08 +020044``name`` option, and there's no rule that says it has to be the same as
Georg Brandl116aa622007-08-15 14:28:22 +000045the name of the sole module in the distribution (although that's probably a good
46convention to follow). However, the distribution name is used to generate
47filenames, so you should stick to letters, digits, underscores, and hyphens.
48
Georg Brandl3f40c402014-09-21 00:35:08 +020049Since ``py_modules`` is a list, you can of course specify multiple
Georg Brandl116aa622007-08-15 14:28:22 +000050modules, eg. if you're distributing modules :mod:`foo` and :mod:`bar`, your
51setup might look like this::
52
53 <root>/
54 setup.py
55 foo.py
56 bar.py
57
58and the setup script might be ::
59
60 from distutils.core import setup
61 setup(name='foobar',
62 version='1.0',
63 py_modules=['foo', 'bar'],
64 )
65
66You can put module source files into another directory, but if you have enough
67modules to do that, it's probably easier to specify modules by package rather
68than listing them individually.
69
70
71.. _pure-pkg:
72
73Pure Python distribution (by package)
74=====================================
75
76If you have more than a couple of modules to distribute, especially if they are
77in multiple packages, it's probably easier to specify whole packages rather than
78individual modules. This works even if your modules are not in a package; you
79can just tell the Distutils to process modules from the root package, and that
80works the same as any other package (except that you don't have to have an
81:file:`__init__.py` file).
82
83The setup script from the last example could also be written as ::
84
85 from distutils.core import setup
86 setup(name='foobar',
87 version='1.0',
88 packages=[''],
89 )
90
91(The empty string stands for the root package.)
92
93If those two files are moved into a subdirectory, but remain in the root
94package, e.g.::
95
96 <root>/
97 setup.py
98 src/ foo.py
99 bar.py
100
101then you would still specify the root package, but you have to tell the
102Distutils where source files in the root package live::
103
104 from distutils.core import setup
105 setup(name='foobar',
106 version='1.0',
107 package_dir={'': 'src'},
108 packages=[''],
109 )
110
111More typically, though, you will want to distribute multiple modules in the same
112package (or in sub-packages). For example, if the :mod:`foo` and :mod:`bar`
113modules belong in package :mod:`foobar`, one way to layout your source tree is
114::
115
116 <root>/
117 setup.py
118 foobar/
119 __init__.py
120 foo.py
121 bar.py
122
123This is in fact the default layout expected by the Distutils, and the one that
124requires the least work to describe in your setup script::
125
126 from distutils.core import setup
127 setup(name='foobar',
128 version='1.0',
129 packages=['foobar'],
130 )
131
132If you want to put modules in directories not named for their package, then you
Georg Brandl3f40c402014-09-21 00:35:08 +0200133need to use the ``package_dir`` option again. For example, if the
Georg Brandl116aa622007-08-15 14:28:22 +0000134:file:`src` directory holds modules in the :mod:`foobar` package::
135
136 <root>/
137 setup.py
138 src/
139 __init__.py
140 foo.py
141 bar.py
142
143an appropriate setup script would be ::
144
145 from distutils.core import setup
146 setup(name='foobar',
147 version='1.0',
148 package_dir={'foobar': 'src'},
149 packages=['foobar'],
150 )
151
152Or, you might put modules from your main package right in the distribution
153root::
154
155 <root>/
156 setup.py
157 __init__.py
158 foo.py
159 bar.py
160
161in which case your setup script would be ::
162
163 from distutils.core import setup
164 setup(name='foobar',
165 version='1.0',
166 package_dir={'foobar': ''},
167 packages=['foobar'],
168 )
169
170(The empty string also stands for the current directory.)
171
Georg Brandl3f40c402014-09-21 00:35:08 +0200172If you have sub-packages, they must be explicitly listed in ``packages``,
173but any entries in ``package_dir`` automatically extend to sub-packages.
Georg Brandl116aa622007-08-15 14:28:22 +0000174(In other words, the Distutils does *not* scan your source tree, trying to
175figure out which directories correspond to Python packages by looking for
176:file:`__init__.py` files.) Thus, if the default layout grows a sub-package::
177
178 <root>/
179 setup.py
180 foobar/
181 __init__.py
182 foo.py
183 bar.py
184 subfoo/
185 __init__.py
186 blah.py
187
188then the corresponding setup script would be ::
189
190 from distutils.core import setup
191 setup(name='foobar',
192 version='1.0',
193 packages=['foobar', 'foobar.subfoo'],
194 )
195
Georg Brandl116aa622007-08-15 14:28:22 +0000196
197.. _single-ext:
198
199Single extension module
200=======================
201
Georg Brandl3f40c402014-09-21 00:35:08 +0200202Extension modules are specified using the ``ext_modules`` option.
203``package_dir`` has no effect on where extension source files are found;
Georg Brandl116aa622007-08-15 14:28:22 +0000204it only affects the source for pure Python modules. The simplest case, a
205single extension module in a single C source file, is::
206
207 <root>/
208 setup.py
209 foo.c
210
211If the :mod:`foo` extension belongs in the root package, the setup script for
212this could be ::
213
214 from distutils.core import setup
215 from distutils.extension import Extension
216 setup(name='foobar',
217 version='1.0',
218 ext_modules=[Extension('foo', ['foo.c'])],
219 )
220
221If the extension actually belongs in a package, say :mod:`foopkg`, then
222
223With exactly the same source tree layout, this extension can be put in the
224:mod:`foopkg` package simply by changing the name of the extension::
225
226 from distutils.core import setup
227 from distutils.extension import Extension
228 setup(name='foobar',
229 version='1.0',
230 ext_modules=[Extension('foopkg.foo', ['foo.c'])],
231 )
232
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000233Checking a package
234==================
235
236The ``check`` command allows you to verify if your package meta-data
237meet the minimum requirements to build a distribution.
238
239To run it, just call it using your :file:`setup.py` script. If something is
240missing, ``check`` will display a warning.
241
242Let's take an example with a simple script::
243
244 from distutils.core import setup
245
246 setup(name='foobar')
247
248Running the ``check`` command will display some warnings::
249
250 $ python setup.py check
251 running check
252 warning: check: missing required meta-data: version, url
253 warning: check: missing meta-data: either (author and author_email) or
254 (maintainer and maintainer_email) must be supplied
255
256
Senthil Kumaran82270452010-10-15 13:29:33 +0000257If you use the reStructuredText syntax in the ``long_description`` field and
258`docutils`_ is installed you can check if the syntax is fine with the
259``check`` command, using the ``restructuredtext`` option.
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000260
261For example, if the :file:`setup.py` script is changed like this::
262
263 from distutils.core import setup
264
265 desc = """\
266 My description
267 =============
268
269 This is the description of the ``foobar`` package.
270 """
271
272 setup(name='foobar', version='1', author='tarek',
273 author_email='tarek@ziade.org',
274 url='http://example.com', long_description=desc)
275
276Where the long description is broken, ``check`` will be able to detect it
Senthil Kumaran82270452010-10-15 13:29:33 +0000277by using the :mod:`docutils` parser::
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000278
Éric Araujo2b9388a2011-04-22 21:27:10 +0200279 $ python setup.py check --restructuredtext
Tarek Ziadé96c45a92010-07-31 09:10:51 +0000280 running check
281 warning: check: Title underline too short. (line 2)
282 warning: check: Could not finish the parsing.
283
Jason R. Coombs3492e392013-11-10 18:15:03 -0500284Reading the metadata
285=====================
286
287The :func:`distutils.core.setup` function provides a command-line interface
288that allows you to query the metadata fields of a project through the
Georg Brandl6b4c8472014-10-30 22:26:26 +0100289``setup.py`` script of a given project::
Jason R. Coombs3492e392013-11-10 18:15:03 -0500290
291 $ python setup.py --name
292 distribute
293
Georg Brandl6b4c8472014-10-30 22:26:26 +0100294This call reads the ``name`` metadata by running the
Jason R. Coombs3492e392013-11-10 18:15:03 -0500295:func:`distutils.core.setup` function. Although, when a source or binary
296distribution is created with Distutils, the metadata fields are written
297in a static file called :file:`PKG-INFO`. When a Distutils-based project is
298installed in Python, the :file:`PKG-INFO` file is copied alongside the modules
299and packages of the distribution under :file:`NAME-VERSION-pyX.X.egg-info`,
Georg Brandl6b4c8472014-10-30 22:26:26 +0100300where ``NAME`` is the name of the project, ``VERSION`` its version as defined
301in the Metadata, and ``pyX.X`` the major and minor version of Python like
302``2.7`` or ``3.2``.
Jason R. Coombs3492e392013-11-10 18:15:03 -0500303
304You can read back this static file, by using the
305:class:`distutils.dist.DistributionMetadata` class and its
306:func:`read_pkg_file` method::
307
308 >>> from distutils.dist import DistributionMetadata
309 >>> metadata = DistributionMetadata()
310 >>> metadata.read_pkg_file(open('distribute-0.6.8-py2.7.egg-info'))
311 >>> metadata.name
312 'distribute'
313 >>> metadata.version
314 '0.6.8'
315 >>> metadata.description
316 'Easily download, build, install, upgrade, and uninstall Python packages'
317
318Notice that the class can also be instanciated with a metadata file path to
319loads its values::
320
321 >>> pkg_info_path = 'distribute-0.6.8-py2.7.egg-info'
322 >>> DistributionMetadata(pkg_info_path).name
323 'distribute'
324
325
Georg Brandl116aa622007-08-15 14:28:22 +0000326.. % \section{Multiple extension modules}
327.. % \label{multiple-ext}
328
329.. % \section{Putting it all together}
330
331
Senthil Kumaran82270452010-10-15 13:29:33 +0000332.. _docutils: http://docutils.sourceforge.net