blob: 3f3e7908445a466f374b08b32400aaa9d6898fe7 [file] [log] [blame]
Éric Araujo3a9f58f2011-06-01 20:42:49 +02001.. _packaging-built-dist:
2
3****************************
4Creating Built Distributions
5****************************
6
7A "built distribution" is what you're probably used to thinking of either as a
8"binary package" or an "installer" (depending on your background). It's not
9necessarily binary, though, because it might contain only Python source code
10and/or byte-code; and we don't call it a package, because that word is already
11spoken for in Python. (And "installer" is a term specific to the world of
12mainstream desktop systems.)
13
14A built distribution is how you make life as easy as possible for installers of
15your module distribution: for users of RPM-based Linux systems, it's a binary
16RPM; for Windows users, it's an executable installer; for Debian-based Linux
17users, it's a Debian package; and so forth. Obviously, no one person will be
18able to create built distributions for every platform under the sun, so the
19Distutils are designed to enable module developers to concentrate on their
20specialty---writing code and creating source distributions---while an
21intermediary species called *packagers* springs up to turn source distributions
22into built distributions for as many platforms as there are packagers.
23
24Of course, the module developer could be his own packager; or the packager could
25be a volunteer "out there" somewhere who has access to a platform which the
26original developer does not; or it could be software periodically grabbing new
27source distributions and turning them into built distributions for as many
28platforms as the software has access to. Regardless of who they are, a packager
29uses the setup script and the :command:`bdist` command family to generate built
30distributions.
31
32As a simple example, if I run the following command in the Distutils source
33tree::
34
35 python setup.py bdist
36
37then the Distutils builds my module distribution (the Distutils itself in this
38case), does a "fake" installation (also in the :file:`build` directory), and
39creates the default type of built distribution for my platform. The default
40format for built distributions is a "dumb" tar file on Unix, and a simple
41executable installer on Windows. (That tar file is considered "dumb" because it
42has to be unpacked in a specific location to work.)
43
44Thus, the above command on a Unix system creates
45:file:`Distutils-1.0.{plat}.tar.gz`; unpacking this tarball from the right place
46installs the Distutils just as though you had downloaded the source distribution
47and run ``python setup.py install``. (The "right place" is either the root of
48the filesystem or Python's :file:`{prefix}` directory, depending on the options
49given to the :command:`bdist_dumb` command; the default is to make dumb
50distributions relative to :file:`{prefix}`.)
51
52Obviously, for pure Python distributions, this isn't any simpler than just
53running ``python setup.py install``\ ---but for non-pure distributions, which
54include extensions that would need to be compiled, it can mean the difference
55between someone being able to use your extensions or not. And creating "smart"
56built distributions, such as an executable installer for
57Windows, is far more convenient for users even if your distribution doesn't
58include any extensions.
59
60The :command:`bdist` command has a :option:`--formats` option, similar to the
61:command:`sdist` command, which you can use to select the types of built
62distribution to generate: for example, ::
63
64 python setup.py bdist --format=zip
65
66would, when run on a Unix system, create :file:`Distutils-1.0.{plat}.zip`\
67---again, this archive would be unpacked from the root directory to install the
68Distutils.
69
70The available formats for built distributions are:
71
72+-------------+------------------------------+---------+
73| Format | Description | Notes |
74+=============+==============================+=========+
75| ``gztar`` | gzipped tar file | (1),(3) |
76| | (:file:`.tar.gz`) | |
77+-------------+------------------------------+---------+
78| ``ztar`` | compressed tar file | \(3) |
79| | (:file:`.tar.Z`) | |
80+-------------+------------------------------+---------+
81| ``tar`` | tar file (:file:`.tar`) | \(3) |
82+-------------+------------------------------+---------+
83| ``zip`` | zip file (:file:`.zip`) | (2),(4) |
84+-------------+------------------------------+---------+
85| ``wininst`` | self-extracting ZIP file for | \(4) |
86| | Windows | |
87+-------------+------------------------------+---------+
88| ``msi`` | Microsoft Installer. | |
89+-------------+------------------------------+---------+
90
91
92Notes:
93
94(1)
95 default on Unix
96
97(2)
98 default on Windows
99
100(3)
101 requires external utilities: :program:`tar` and possibly one of :program:`gzip`,
102 :program:`bzip2`, or :program:`compress`
103
104(4)
105 requires either external :program:`zip` utility or :mod:`zipfile` module (part
106 of the standard Python library since Python 1.6)
107
108You don't have to use the :command:`bdist` command with the :option:`--formats`
109option; you can also use the command that directly implements the format you're
110interested in. Some of these :command:`bdist` "sub-commands" actually generate
111several similar formats; for instance, the :command:`bdist_dumb` command
112generates all the "dumb" archive formats (``tar``, ``ztar``, ``gztar``, and
113``zip``). The :command:`bdist` sub-commands, and the formats generated by
114each, are:
115
116+--------------------------+-----------------------+
117| Command | Formats |
118+==========================+=======================+
119| :command:`bdist_dumb` | tar, ztar, gztar, zip |
120+--------------------------+-----------------------+
121| :command:`bdist_wininst` | wininst |
122+--------------------------+-----------------------+
123| :command:`bdist_msi` | msi |
124+--------------------------+-----------------------+
125
126The following sections give details on the individual :command:`bdist_\*`
127commands.
128
129
130.. _packaging-creating-dumb:
131
132Creating dumb built distributions
133=================================
134
135.. XXX Need to document absolute vs. prefix-relative packages here, but first
136 I have to implement it!
137
138
139.. _packaging-creating-wininst:
140
141Creating Windows Installers
142===========================
143
144Executable installers are the natural format for binary distributions on
145Windows. They display a nice graphical user interface, display some information
146about the module distribution to be installed taken from the metadata in the
147setup script, let the user select a few options, and start or cancel the
148installation.
149
150Since the metadata is taken from the setup script, creating Windows installers
151is usually as easy as running::
152
153 python setup.py bdist_wininst
154
155or the :command:`bdist` command with the :option:`--formats` option::
156
157 python setup.py bdist --formats=wininst
158
159If you have a pure module distribution (only containing pure Python modules and
160packages), the resulting installer will be version independent and have a name
161like :file:`foo-1.0.win32.exe`. These installers can even be created on Unix
162platforms or Mac OS X.
163
164If you have a non-pure distribution, the extensions can only be created on a
165Windows platform, and will be Python version dependent. The installer filename
166will reflect this and now has the form :file:`foo-1.0.win32-py2.0.exe`. You
167have to create a separate installer for every Python version you want to
168support.
169
170.. TODO Add :term: markup to bytecode when merging into the stdlib
171
172The installer will try to compile pure modules into bytecode after installation
173on the target system in normal and optimizing mode. If you don't want this to
174happen for some reason, you can run the :command:`bdist_wininst` command with
175the :option:`--no-target-compile` and/or the :option:`--no-target-optimize`
176option.
177
178By default the installer will display the cool "Python Powered" logo when it is
179run, but you can also supply your own 152x261 bitmap which must be a Windows
180:file:`.bmp` file with the :option:`--bitmap` option.
181
182The installer will also display a large title on the desktop background window
183when it is run, which is constructed from the name of your distribution and the
184version number. This can be changed to another text by using the
185:option:`--title` option.
186
187The installer file will be written to the "distribution directory" --- normally
188:file:`dist/`, but customizable with the :option:`--dist-dir` option.
189
190.. _packaging-cross-compile-windows:
191
192Cross-compiling on Windows
193==========================
194
195Starting with Python 2.6, packaging is capable of cross-compiling between
196Windows platforms. In practice, this means that with the correct tools
197installed, you can use a 32bit version of Windows to create 64bit extensions
198and vice-versa.
199
200To build for an alternate platform, specify the :option:`--plat-name` option
201to the build command. Valid values are currently 'win32', 'win-amd64' and
202'win-ia64'. For example, on a 32bit version of Windows, you could execute::
203
204 python setup.py build --plat-name=win-amd64
205
206to build a 64bit version of your extension. The Windows Installers also
207support this option, so the command::
208
209 python setup.py build --plat-name=win-amd64 bdist_wininst
210
211would create a 64bit installation executable on your 32bit version of Windows.
212
213To cross-compile, you must download the Python source code and cross-compile
214Python itself for the platform you are targetting - it is not possible from a
215binary installtion of Python (as the .lib etc file for other platforms are
216not included.) In practice, this means the user of a 32 bit operating
217system will need to use Visual Studio 2008 to open the
218:file:`PCBuild/PCbuild.sln` solution in the Python source tree and build the
219"x64" configuration of the 'pythoncore' project before cross-compiling
220extensions is possible.
221
222Note that by default, Visual Studio 2008 does not install 64bit compilers or
223tools. You may need to reexecute the Visual Studio setup process and select
224these tools (using Control Panel->[Add/Remove] Programs is a convenient way to
225check or modify your existing install.)
226
227.. _packaging-postinstallation-script:
228
229The Postinstallation script
230---------------------------
231
232Starting with Python 2.3, a postinstallation script can be specified with the
233:option:`--install-script` option. The basename of the script must be
234specified, and the script filename must also be listed in the scripts argument
235to the setup function.
236
237This script will be run at installation time on the target system after all the
238files have been copied, with ``argv[1]`` set to :option:`-install`, and again at
239uninstallation time before the files are removed with ``argv[1]`` set to
240:option:`-remove`.
241
242The installation script runs embedded in the windows installer, every output
243(``sys.stdout``, ``sys.stderr``) is redirected into a buffer and will be
244displayed in the GUI after the script has finished.
245
246Some functions especially useful in this context are available as additional
247built-in functions in the installation script.
248
249.. currentmodule:: bdist_wininst-postinst-script
250
251.. function:: directory_created(path)
252 file_created(path)
253
254 These functions should be called when a directory or file is created by the
255 postinstall script at installation time. It will register *path* with the
256 uninstaller, so that it will be removed when the distribution is uninstalled.
257 To be safe, directories are only removed if they are empty.
258
259
260.. function:: get_special_folder_path(csidl_string)
261
262 This function can be used to retrieve special folder locations on Windows like
263 the Start Menu or the Desktop. It returns the full path to the folder.
264 *csidl_string* must be one of the following strings::
265
266 "CSIDL_APPDATA"
267
268 "CSIDL_COMMON_STARTMENU"
269 "CSIDL_STARTMENU"
270
271 "CSIDL_COMMON_DESKTOPDIRECTORY"
272 "CSIDL_DESKTOPDIRECTORY"
273
274 "CSIDL_COMMON_STARTUP"
275 "CSIDL_STARTUP"
276
277 "CSIDL_COMMON_PROGRAMS"
278 "CSIDL_PROGRAMS"
279
280 "CSIDL_FONTS"
281
282 If the folder cannot be retrieved, :exc:`OSError` is raised.
283
284 Which folders are available depends on the exact Windows version, and probably
285 also the configuration. For details refer to Microsoft's documentation of the
Georg Brandld3b41c62011-07-09 11:48:50 +0200286 :c:func:`SHGetSpecialFolderPath` function.
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200287
288
289.. function:: create_shortcut(target, description, filename[, arguments[, workdir[, iconpath[, iconindex]]]])
290
291 This function creates a shortcut. *target* is the path to the program to be
292 started by the shortcut. *description* is the description of the shortcut.
293 *filename* is the title of the shortcut that the user will see. *arguments*
294 specifies the command-line arguments, if any. *workdir* is the working directory
295 for the program. *iconpath* is the file containing the icon for the shortcut,
296 and *iconindex* is the index of the icon in the file *iconpath*. Again, for
297 details consult the Microsoft documentation for the :class:`IShellLink`
298 interface.
299
300
301Vista User Access Control (UAC)
302===============================
303
304Starting with Python 2.6, bdist_wininst supports a :option:`--user-access-control`
305option. The default is 'none' (meaning no UAC handling is done), and other
306valid values are 'auto' (meaning prompt for UAC elevation if Python was
307installed for all users) and 'force' (meaning always prompt for elevation).