1.. _built-dist:
2
3****************************
4Creating Built Distributions
5****************************
6
7.. include:: ./_setuptools_disclaimer.rst
8
9A "built distribution" is what you're probably used to thinking of either as a
10"binary package" or an "installer" (depending on your background).  It's not
11necessarily binary, though, because it might contain only Python source code
12and/or byte-code; and we don't call it a package, because that word is already
13spoken for in Python.  (And "installer" is a term specific to the world of
14mainstream desktop systems.)
15
16A built distribution is how you make life as easy as possible for installers of
17your module distribution: for users of RPM-based Linux systems, it's a binary
18RPM; for Windows users, it's an executable installer; for Debian-based Linux
19users, it's a Debian package; and so forth.  Obviously, no one person will be
20able to create built distributions for every platform under the sun, so the
21Distutils are designed to enable module developers to concentrate on their
22specialty---writing code and creating source distributions---while an
23intermediary species called *packagers* springs up to turn source distributions
24into built distributions for as many platforms as there are packagers.
25
26Of course, the module developer could be their own packager; or the packager could
27be a volunteer "out there" somewhere who has access to a platform which the
28original developer does not; or it could be software periodically grabbing new
29source distributions and turning them into built distributions for as many
30platforms as the software has access to.  Regardless of who they are, a packager
31uses the setup script and the :command:`bdist` command family to generate built
32distributions.
33
34As a simple example, if I run the following command in the Distutils source
35tree::
36
37   python setup.py bdist
38
39then the Distutils builds my module distribution (the Distutils itself in this
40case), does a "fake" installation (also in the :file:`build` directory), and
41creates the default type of built distribution for my platform.  The default
42format for built distributions is a "dumb" tar file on Unix, and a simple
43executable installer on Windows.  (That tar file is considered "dumb" because it
44has to be unpacked in a specific location to work.)
45
46Thus, the above command on a Unix system creates
47:file:`Distutils-1.0.{plat}.tar.gz`; unpacking this tarball from the right place
48installs the Distutils just as though you had downloaded the source distribution
49and run ``python setup.py install``.  (The "right place" is either the root of
50the filesystem or  Python's :file:`{prefix}` directory, depending on the options
51given to the :command:`bdist_dumb` command; the default is to make dumb
52distributions relative to :file:`{prefix}`.)
53
54Obviously, for pure Python distributions, this isn't any simpler than just
55running ``python setup.py install``\ ---but for non-pure distributions, which
56include extensions that would need to be compiled, it can mean the difference
57between someone being able to use your extensions or not.  And creating "smart"
58built distributions, such as an RPM package or an executable installer for
59Windows, is far more convenient for users even if your distribution doesn't
60include any extensions.
61
62The :command:`bdist` command has a :option:`!--formats` option, similar to the
63:command:`sdist` command, which you can use to select the types of built
64distribution to generate: for example, ::
65
66   python setup.py bdist --format=zip
67
68would, when run on a Unix system, create
69:file:`Distutils-1.0.{plat}.zip`\ ---again, this archive would be unpacked
70from the root directory to install the Distutils.
71
72The available formats for built distributions are:
73
74+-------------+------------------------------+---------+
75| Format      | Description                  | Notes   |
76+=============+==============================+=========+
77| ``gztar``   | gzipped tar file             | \(1)    |
78|             | (:file:`.tar.gz`)            |         |
79+-------------+------------------------------+---------+
80| ``bztar``   | bzipped tar file             |         |
81|             | (:file:`.tar.bz2`)           |         |
82+-------------+------------------------------+---------+
83| ``xztar``   | xzipped tar file             |         |
84|             | (:file:`.tar.xz`)            |         |
85+-------------+------------------------------+---------+
86| ``ztar``    | compressed tar file          | \(3)    |
87|             | (:file:`.tar.Z`)             |         |
88+-------------+------------------------------+---------+
89| ``tar``     | tar file (:file:`.tar`)      |         |
90+-------------+------------------------------+---------+
91| ``zip``     | zip file (:file:`.zip`)      | (2),(4) |
92+-------------+------------------------------+---------+
93| ``rpm``     | RPM                          | \(5)    |
94+-------------+------------------------------+---------+
95| ``pkgtool`` | Solaris :program:`pkgtool`   |         |
96+-------------+------------------------------+---------+
97| ``sdux``    | HP-UX :program:`swinstall`   |         |
98+-------------+------------------------------+---------+
99| ``wininst`` | self-extracting ZIP file for | \(4)    |
100|             | Windows                      |         |
101+-------------+------------------------------+---------+
102| ``msi``     | Microsoft Installer.         |         |
103+-------------+------------------------------+---------+
104
105.. versionchanged:: 3.5
106   Added support for the ``xztar`` format.
107
108
109Notes:
110
111(1)
112   default on Unix
113
114(2)
115   default on Windows
116
117(3)
118   requires external :program:`compress` utility.
119
120(4)
121   requires either external :program:`zip` utility or :mod:`zipfile` module (part
122   of the standard Python library since Python 1.6)
123
124(5)
125   requires external :program:`rpm` utility, version 3.0.4 or better (use ``rpm
126   --version`` to find out which version you have)
127
128You don't have to use the :command:`bdist` command with the :option:`!--formats`
129option; you can also use the command that directly implements the format you're
130interested in.  Some of these :command:`bdist` "sub-commands" actually generate
131several similar formats; for instance, the :command:`bdist_dumb` command
132generates all the "dumb" archive formats (``tar``, ``gztar``, ``bztar``,
133``xztar``, ``ztar``, and ``zip``), and :command:`bdist_rpm` generates both
134binary and source RPMs.  The :command:`bdist` sub-commands, and the formats
135generated by each, are:
136
137+--------------------------+-------------------------------------+
138| Command                  | Formats                             |
139+==========================+=====================================+
140| :command:`bdist_dumb`    | tar, gztar, bztar, xztar, ztar, zip |
141+--------------------------+-------------------------------------+
142| :command:`bdist_rpm`     | rpm, srpm                           |
143+--------------------------+-------------------------------------+
144| :command:`bdist_wininst` | wininst                             |
145+--------------------------+-------------------------------------+
146| :command:`bdist_msi`     | msi                                 |
147+--------------------------+-------------------------------------+
148
149.. note::
150   bdist_wininst is deprecated since Python 3.8.
151
152.. note::
153   bdist_msi is deprecated since Python 3.9.
154
155The following sections give details on the individual :command:`bdist_\*`
156commands.
157
158
159.. .. _creating-dumb:
160
161.. Creating dumb built distributions
162.. =================================
163
164.. XXX Need to document absolute vs. prefix-relative packages here, but first
165   I have to implement it!
166
167
168.. _creating-rpms:
169
170Creating RPM packages
171=====================
172
173The RPM format is used by many popular Linux distributions, including Red Hat,
174SuSE, and Mandrake.  If one of these (or any of the other RPM-based Linux
175distributions) is your usual environment, creating RPM packages for other users
176of that same distribution is trivial. Depending on the complexity of your module
177distribution and differences between Linux distributions, you may also be able
178to create RPMs that work on different RPM-based distributions.
179
180The usual way to create an RPM of your module distribution is to run the
181:command:`bdist_rpm` command::
182
183   python setup.py bdist_rpm
184
185or the :command:`bdist` command with the :option:`!--format` option::
186
187   python setup.py bdist --formats=rpm
188
189The former allows you to specify RPM-specific options; the latter allows  you to
190easily specify multiple formats in one run.  If you need to do both, you can
191explicitly specify multiple :command:`bdist_\*` commands and their options::
192
193   python setup.py bdist_rpm --packager="John Doe <jdoe@example.org>" \
194                   bdist_wininst --target-version="2.0"
195
196Creating RPM packages is driven by a :file:`.spec` file, much as using the
197Distutils is driven by the setup script.  To make your life easier, the
198:command:`bdist_rpm` command normally creates a :file:`.spec` file based on the
199information you supply in the setup script, on the command line, and in any
200Distutils configuration files.  Various options and sections in the
201:file:`.spec` file are derived from options in the setup script as follows:
202
203+------------------------------------------+----------------------------------------------+
204| RPM :file:`.spec` file option or section | Distutils setup script option                |
205+==========================================+==============================================+
206| Name                                     | ``name``                                     |
207+------------------------------------------+----------------------------------------------+
208| Summary (in preamble)                    | ``description``                              |
209+------------------------------------------+----------------------------------------------+
210| Version                                  | ``version``                                  |
211+------------------------------------------+----------------------------------------------+
212| Vendor                                   | ``author`` and ``author_email``,             |
213|                                          | or  --- & ``maintainer`` and                 |
214|                                          | ``maintainer_email``                         |
215+------------------------------------------+----------------------------------------------+
216| Copyright                                | ``license``                                  |
217+------------------------------------------+----------------------------------------------+
218| Url                                      | ``url``                                      |
219+------------------------------------------+----------------------------------------------+
220| %description (section)                   | ``long_description``                         |
221+------------------------------------------+----------------------------------------------+
222
223Additionally, there are many options in :file:`.spec` files that don't have
224corresponding options in the setup script.  Most of these are handled through
225options to the :command:`bdist_rpm` command as follows:
226
227+-------------------------------+-----------------------------+-------------------------+
228| RPM :file:`.spec` file option | :command:`bdist_rpm` option | default value           |
229| or section                    |                             |                         |
230+===============================+=============================+=========================+
231| Release                       | ``release``                 | "1"                     |
232+-------------------------------+-----------------------------+-------------------------+
233| Group                         | ``group``                   | "Development/Libraries" |
234+-------------------------------+-----------------------------+-------------------------+
235| Vendor                        | ``vendor``                  | (see above)             |
236+-------------------------------+-----------------------------+-------------------------+
237| Packager                      | ``packager``                | (none)                  |
238+-------------------------------+-----------------------------+-------------------------+
239| Provides                      | ``provides``                | (none)                  |
240+-------------------------------+-----------------------------+-------------------------+
241| Requires                      | ``requires``                | (none)                  |
242+-------------------------------+-----------------------------+-------------------------+
243| Conflicts                     | ``conflicts``               | (none)                  |
244+-------------------------------+-----------------------------+-------------------------+
245| Obsoletes                     | ``obsoletes``               | (none)                  |
246+-------------------------------+-----------------------------+-------------------------+
247| Distribution                  | ``distribution_name``       | (none)                  |
248+-------------------------------+-----------------------------+-------------------------+
249| BuildRequires                 | ``build_requires``          | (none)                  |
250+-------------------------------+-----------------------------+-------------------------+
251| Icon                          | ``icon``                    | (none)                  |
252+-------------------------------+-----------------------------+-------------------------+
253
254Obviously, supplying even a few of these options on the command-line would be
255tedious and error-prone, so it's usually best to put them in the setup
256configuration file, :file:`setup.cfg`\ ---see section :ref:`setup-config`.  If
257you distribute or package many Python module distributions, you might want to
258put options that apply to all of them in your personal Distutils configuration
259file (:file:`~/.pydistutils.cfg`).  If you want to temporarily disable
260this file, you can pass the :option:`!--no-user-cfg` option to :file:`setup.py`.
261
262There are three steps to building a binary RPM package, all of which are
263handled automatically by the Distutils:
264
265#. create a :file:`.spec` file, which describes the package (analogous  to the
266   Distutils setup script; in fact, much of the information in the  setup script
267   winds up in the :file:`.spec` file)
268
269#. create the source RPM
270
271#. create the "binary" RPM (which may or may not contain binary code, depending
272   on whether your module distribution contains Python extensions)
273
274Normally, RPM bundles the last two steps together; when you use the Distutils,
275all three steps are typically bundled together.
276
277If you wish, you can separate these three steps.  You can use the
278:option:`!--spec-only` option to make :command:`bdist_rpm` just create the
279:file:`.spec` file and exit; in this case, the :file:`.spec` file will be
280written to the "distribution directory"---normally :file:`dist/`, but
281customizable with the :option:`!--dist-dir` option.  (Normally, the :file:`.spec`
282file winds up deep in the "build tree," in a temporary directory created by
283:command:`bdist_rpm`.)
284
285.. % \XXX{this isn't implemented yet---is it needed?!}
286.. % You can also specify a custom \file{.spec} file with the
287.. % \longprogramopt{spec-file} option; used in conjunction with
288.. % \longprogramopt{spec-only}, this gives you an opportunity to customize
289.. % the \file{.spec} file manually:
290.. %
291.. % \ begin{verbatim}
292.. % > python setup.py bdist_rpm --spec-only
293.. % # ...edit dist/FooBar-1.0.spec
294.. % > python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec
295.. % \ end{verbatim}
296.. %
297.. % (Although a better way to do this is probably to override the standard
298.. % \command{bdist\_rpm} command with one that writes whatever else you want
299.. % to the \file{.spec} file.)
300
301
302.. _creating-wininst:
303
304Creating Windows Installers
305===========================
306
307.. warning::
308   bdist_wininst is deprecated since Python 3.8.
309
310.. warning::
311   bdist_msi is deprecated since Python 3.9.
312
313Executable installers are the natural format for binary distributions on
314Windows.  They display a nice graphical user interface, display some information
315about the module distribution to be installed taken from the metadata in the
316setup script, let the user select a few options, and start or cancel the
317installation.
318
319Since the metadata is taken from the setup script, creating Windows installers
320is usually as easy as running::
321
322   python setup.py bdist_wininst
323
324or the :command:`bdist` command with the :option:`!--formats` option::
325
326   python setup.py bdist --formats=wininst
327
328If you have a pure module distribution (only containing pure Python modules and
329packages), the resulting installer will be version independent and have a name
330like :file:`foo-1.0.win32.exe`. Note that creating ``wininst`` binary
331distributions in only supported on Windows systems.
332
333If you have a non-pure distribution, the extensions can only be created on a
334Windows platform, and will be Python version dependent. The installer filename
335will reflect this and now has the form :file:`foo-1.0.win32-py2.0.exe`.  You
336have to create a separate installer for every Python version you want to
337support.
338
339The installer will try to compile pure modules into :term:`bytecode` after installation
340on the target system in normal and optimizing mode.  If you don't want this to
341happen for some reason, you can run the :command:`bdist_wininst` command with
342the :option:`!--no-target-compile` and/or the :option:`!--no-target-optimize`
343option.
344
345By default the installer will display the cool "Python Powered" logo when it is
346run, but you can also supply your own 152x261 bitmap which must be a Windows
347:file:`.bmp` file with the :option:`!--bitmap` option.
348
349The installer will also display a large title on the desktop background window
350when it is run, which is constructed from the name of your distribution and the
351version number.  This can be changed to another text by using the
352:option:`!--title` option.
353
354The installer file will be written to the "distribution directory" --- normally
355:file:`dist/`, but customizable with the :option:`!--dist-dir` option.
356
357.. _cross-compile-windows:
358
359Cross-compiling on Windows
360==========================
361
362Starting with Python 2.6, distutils is capable of cross-compiling between
363Windows platforms.  In practice, this means that with the correct tools
364installed, you can use a 32bit version of Windows to create 64bit extensions
365and vice-versa.
366
367To build for an alternate platform, specify the :option:`!--plat-name` option
368to the build command.  Valid values are currently 'win32', and  'win-amd64'.
369For example, on a 32bit version of Windows, you could execute::
370
371   python setup.py build --plat-name=win-amd64
372
373to build a 64bit version of your extension.  The Windows Installers also
374support this option, so the command::
375
376   python setup.py build --plat-name=win-amd64 bdist_wininst
377
378would create a 64bit installation executable on your 32bit version of Windows.
379
380To cross-compile, you must download the Python source code and cross-compile
381Python itself for the platform you are targeting - it is not possible from a
382binary installation of Python (as the .lib etc file for other platforms are
383not included.)  In practice, this means the user of a 32 bit operating
384system will need to use Visual Studio 2008 to open the
385:file:`PCbuild/PCbuild.sln` solution in the Python source tree and build the
386"x64" configuration of the 'pythoncore' project before cross-compiling
387extensions is possible.
388
389Note that by default, Visual Studio 2008 does not install 64bit compilers or
390tools.  You may need to reexecute the Visual Studio setup process and select
391these tools (using Control Panel->[Add/Remove] Programs is a convenient way to
392check or modify your existing install.)
393
394.. _postinstallation-script:
395
396The Postinstallation script
397---------------------------
398
399Starting with Python 2.3, a postinstallation script can be specified with the
400:option:`!--install-script` option.  The basename of the script must be
401specified, and the script filename must also be listed in the scripts argument
402to the setup function.
403
404This script will be run at installation time on the target system after all the
405files have been copied, with ``argv[1]`` set to :option:`!-install`, and again at
406uninstallation time before the files are removed with ``argv[1]`` set to
407:option:`!-remove`.
408
409The installation script runs embedded in the windows installer, every output
410(``sys.stdout``, ``sys.stderr``) is redirected into a buffer and will be
411displayed in the GUI after the script has finished.
412
413Some functions especially useful in this context are available as additional
414built-in functions in the installation script.
415
416
417.. function:: directory_created(path)
418              file_created(path)
419
420   These functions should be called when a directory or file is created by the
421   postinstall script at installation time.  It will register *path* with the
422   uninstaller, so that it will be removed when the distribution is uninstalled.
423   To be safe, directories are only removed if they are empty.
424
425
426.. function:: get_special_folder_path(csidl_string)
427
428   This function can be used to retrieve special folder locations on Windows like
429   the Start Menu or the Desktop.  It returns the full path to the folder.
430   *csidl_string* must be one of the following strings::
431
432      "CSIDL_APPDATA"
433
434      "CSIDL_COMMON_STARTMENU"
435      "CSIDL_STARTMENU"
436
437      "CSIDL_COMMON_DESKTOPDIRECTORY"
438      "CSIDL_DESKTOPDIRECTORY"
439
440      "CSIDL_COMMON_STARTUP"
441      "CSIDL_STARTUP"
442
443      "CSIDL_COMMON_PROGRAMS"
444      "CSIDL_PROGRAMS"
445
446      "CSIDL_FONTS"
447
448   If the folder cannot be retrieved, :exc:`OSError` is raised.
449
450   Which folders are available depends on the exact Windows version, and probably
451   also the configuration.  For details refer to Microsoft's documentation of the
452   :c:func:`SHGetSpecialFolderPath` function.
453
454
455.. function:: create_shortcut(target, description, filename[, arguments[, workdir[, iconpath[, iconindex]]]])
456
457   This function creates a shortcut. *target* is the path to the program to be
458   started by the shortcut. *description* is the description of the shortcut.
459   *filename* is the title of the shortcut that the user will see. *arguments*
460   specifies the command line arguments, if any. *workdir* is the working directory
461   for the program. *iconpath* is the file containing the icon for the shortcut,
462   and *iconindex* is the index of the icon in the file *iconpath*.  Again, for
463   details consult the Microsoft documentation for the :class:`IShellLink`
464   interface.
465
466
467Vista User Access Control (UAC)
468===============================
469
470Starting with Python 2.6, bdist_wininst supports a :option:`!--user-access-control`
471option.  The default is 'none' (meaning no UAC handling is done), and other
472valid values are 'auto' (meaning prompt for UAC elevation if Python was
473installed for all users) and 'force' (meaning always prompt for elevation).
474
475.. note::
476   bdist_wininst is deprecated since Python 3.8.
477
478.. note::
479   bdist_msi is deprecated since Python 3.9.
480