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| ``msi`` | Microsoft Installer. | | 100+-------------+------------------------------+---------+ 101 102.. versionchanged:: 3.5 103 Added support for the ``xztar`` format. 104 105 106Notes: 107 108(1) 109 default on Unix 110 111(2) 112 default on Windows 113 114(3) 115 requires external :program:`compress` utility. 116 117(4) 118 requires either external :program:`zip` utility or :mod:`zipfile` module (part 119 of the standard Python library since Python 1.6) 120 121(5) 122 requires external :program:`rpm` utility, version 3.0.4 or better (use ``rpm 123 --version`` to find out which version you have) 124 125You don't have to use the :command:`bdist` command with the :option:`!--formats` 126option; you can also use the command that directly implements the format you're 127interested in. Some of these :command:`bdist` "sub-commands" actually generate 128several similar formats; for instance, the :command:`bdist_dumb` command 129generates all the "dumb" archive formats (``tar``, ``gztar``, ``bztar``, 130``xztar``, ``ztar``, and ``zip``), and :command:`bdist_rpm` generates both 131binary and source RPMs. The :command:`bdist` sub-commands, and the formats 132generated by each, are: 133 134+--------------------------+-------------------------------------+ 135| Command | Formats | 136+==========================+=====================================+ 137| :command:`bdist_dumb` | tar, gztar, bztar, xztar, ztar, zip | 138+--------------------------+-------------------------------------+ 139| :command:`bdist_rpm` | rpm, srpm | 140+--------------------------+-------------------------------------+ 141| :command:`bdist_msi` | msi | 142+--------------------------+-------------------------------------+ 143 144.. note:: 145 bdist_msi is deprecated since Python 3.9. 146 147The following sections give details on the individual :command:`bdist_\*` 148commands. 149 150 151.. .. _creating-dumb: 152 153.. Creating dumb built distributions 154.. ================================= 155 156.. XXX Need to document absolute vs. prefix-relative packages here, but first 157 I have to implement it! 158 159 160.. _creating-rpms: 161 162Creating RPM packages 163===================== 164 165The RPM format is used by many popular Linux distributions, including Red Hat, 166SuSE, and Mandrake. If one of these (or any of the other RPM-based Linux 167distributions) is your usual environment, creating RPM packages for other users 168of that same distribution is trivial. Depending on the complexity of your module 169distribution and differences between Linux distributions, you may also be able 170to create RPMs that work on different RPM-based distributions. 171 172The usual way to create an RPM of your module distribution is to run the 173:command:`bdist_rpm` command:: 174 175 python setup.py bdist_rpm 176 177or the :command:`bdist` command with the :option:`!--format` option:: 178 179 python setup.py bdist --formats=rpm 180 181The former allows you to specify RPM-specific options; the latter allows you to 182easily specify multiple formats in one run. If you need to do both, you can 183explicitly specify multiple :command:`bdist_\*` commands and their options:: 184 185 python setup.py bdist_rpm --packager="John Doe <jdoe@example.org>" 186 187Creating RPM packages is driven by a :file:`.spec` file, much as using the 188Distutils is driven by the setup script. To make your life easier, the 189:command:`bdist_rpm` command normally creates a :file:`.spec` file based on the 190information you supply in the setup script, on the command line, and in any 191Distutils configuration files. Various options and sections in the 192:file:`.spec` file are derived from options in the setup script as follows: 193 194+------------------------------------------+----------------------------------------------+ 195| RPM :file:`.spec` file option or section | Distutils setup script option | 196+==========================================+==============================================+ 197| Name | ``name`` | 198+------------------------------------------+----------------------------------------------+ 199| Summary (in preamble) | ``description`` | 200+------------------------------------------+----------------------------------------------+ 201| Version | ``version`` | 202+------------------------------------------+----------------------------------------------+ 203| Vendor | ``author`` and ``author_email``, | 204| | or --- & ``maintainer`` and | 205| | ``maintainer_email`` | 206+------------------------------------------+----------------------------------------------+ 207| Copyright | ``license`` | 208+------------------------------------------+----------------------------------------------+ 209| Url | ``url`` | 210+------------------------------------------+----------------------------------------------+ 211| %description (section) | ``long_description`` | 212+------------------------------------------+----------------------------------------------+ 213 214Additionally, there are many options in :file:`.spec` files that don't have 215corresponding options in the setup script. Most of these are handled through 216options to the :command:`bdist_rpm` command as follows: 217 218+-------------------------------+-----------------------------+-------------------------+ 219| RPM :file:`.spec` file option | :command:`bdist_rpm` option | default value | 220| or section | | | 221+===============================+=============================+=========================+ 222| Release | ``release`` | "1" | 223+-------------------------------+-----------------------------+-------------------------+ 224| Group | ``group`` | "Development/Libraries" | 225+-------------------------------+-----------------------------+-------------------------+ 226| Vendor | ``vendor`` | (see above) | 227+-------------------------------+-----------------------------+-------------------------+ 228| Packager | ``packager`` | (none) | 229+-------------------------------+-----------------------------+-------------------------+ 230| Provides | ``provides`` | (none) | 231+-------------------------------+-----------------------------+-------------------------+ 232| Requires | ``requires`` | (none) | 233+-------------------------------+-----------------------------+-------------------------+ 234| Conflicts | ``conflicts`` | (none) | 235+-------------------------------+-----------------------------+-------------------------+ 236| Obsoletes | ``obsoletes`` | (none) | 237+-------------------------------+-----------------------------+-------------------------+ 238| Distribution | ``distribution_name`` | (none) | 239+-------------------------------+-----------------------------+-------------------------+ 240| BuildRequires | ``build_requires`` | (none) | 241+-------------------------------+-----------------------------+-------------------------+ 242| Icon | ``icon`` | (none) | 243+-------------------------------+-----------------------------+-------------------------+ 244 245Obviously, supplying even a few of these options on the command-line would be 246tedious and error-prone, so it's usually best to put them in the setup 247configuration file, :file:`setup.cfg`\ ---see section :ref:`setup-config`. If 248you distribute or package many Python module distributions, you might want to 249put options that apply to all of them in your personal Distutils configuration 250file (:file:`~/.pydistutils.cfg`). If you want to temporarily disable 251this file, you can pass the :option:`!--no-user-cfg` option to :file:`setup.py`. 252 253There are three steps to building a binary RPM package, all of which are 254handled automatically by the Distutils: 255 256#. create a :file:`.spec` file, which describes the package (analogous to the 257 Distutils setup script; in fact, much of the information in the setup script 258 winds up in the :file:`.spec` file) 259 260#. create the source RPM 261 262#. create the "binary" RPM (which may or may not contain binary code, depending 263 on whether your module distribution contains Python extensions) 264 265Normally, RPM bundles the last two steps together; when you use the Distutils, 266all three steps are typically bundled together. 267 268If you wish, you can separate these three steps. You can use the 269:option:`!--spec-only` option to make :command:`bdist_rpm` just create the 270:file:`.spec` file and exit; in this case, the :file:`.spec` file will be 271written to the "distribution directory"---normally :file:`dist/`, but 272customizable with the :option:`!--dist-dir` option. (Normally, the :file:`.spec` 273file winds up deep in the "build tree," in a temporary directory created by 274:command:`bdist_rpm`.) 275 276.. % \XXX{this isn't implemented yet---is it needed?!} 277.. % You can also specify a custom \file{.spec} file with the 278.. % \longprogramopt{spec-file} option; used in conjunction with 279.. % \longprogramopt{spec-only}, this gives you an opportunity to customize 280.. % the \file{.spec} file manually: 281.. % 282.. % \ begin{verbatim} 283.. % > python setup.py bdist_rpm --spec-only 284.. % # ...edit dist/FooBar-1.0.spec 285.. % > python setup.py bdist_rpm --spec-file=dist/FooBar-1.0.spec 286.. % \ end{verbatim} 287.. % 288.. % (Although a better way to do this is probably to override the standard 289.. % \command{bdist\_rpm} command with one that writes whatever else you want 290.. % to the \file{.spec} file.) 291 292 293.. _cross-compile-windows: 294 295Cross-compiling on Windows 296========================== 297 298Starting with Python 2.6, distutils is capable of cross-compiling between 299Windows platforms. In practice, this means that with the correct tools 300installed, you can use a 32bit version of Windows to create 64bit extensions 301and vice-versa. 302 303To build for an alternate platform, specify the :option:`!--plat-name` option 304to the build command. Valid values are currently 'win32', and 'win-amd64'. 305For example, on a 32bit version of Windows, you could execute:: 306 307 python setup.py build --plat-name=win-amd64 308 309to build a 64bit version of your extension. 310 311would create a 64bit installation executable on your 32bit version of Windows. 312 313To cross-compile, you must download the Python source code and cross-compile 314Python itself for the platform you are targeting - it is not possible from a 315binary installation of Python (as the .lib etc file for other platforms are 316not included.) In practice, this means the user of a 32 bit operating 317system will need to use Visual Studio 2008 to open the 318:file:`PCbuild/PCbuild.sln` solution in the Python source tree and build the 319"x64" configuration of the 'pythoncore' project before cross-compiling 320extensions is possible. 321 322Note that by default, Visual Studio 2008 does not install 64bit compilers or 323tools. You may need to reexecute the Visual Studio setup process and select 324these tools (using Control Panel->[Add/Remove] Programs is a convenient way to 325check or modify your existing install.) 326 327.. _postinstallation-script: 328 329The Postinstallation script 330--------------------------- 331 332Starting with Python 2.3, a postinstallation script can be specified with the 333:option:`!--install-script` option. The basename of the script must be 334specified, and the script filename must also be listed in the scripts argument 335to the setup function. 336 337This script will be run at installation time on the target system after all the 338files have been copied, with ``argv[1]`` set to :option:`!-install`, and again at 339uninstallation time before the files are removed with ``argv[1]`` set to 340:option:`!-remove`. 341 342The installation script runs embedded in the windows installer, every output 343(``sys.stdout``, ``sys.stderr``) is redirected into a buffer and will be 344displayed in the GUI after the script has finished. 345 346Some functions especially useful in this context are available as additional 347built-in functions in the installation script. 348 349 350.. function:: directory_created(path) 351 file_created(path) 352 353 These functions should be called when a directory or file is created by the 354 postinstall script at installation time. It will register *path* with the 355 uninstaller, so that it will be removed when the distribution is uninstalled. 356 To be safe, directories are only removed if they are empty. 357 358 359.. function:: get_special_folder_path(csidl_string) 360 361 This function can be used to retrieve special folder locations on Windows like 362 the Start Menu or the Desktop. It returns the full path to the folder. 363 *csidl_string* must be one of the following strings:: 364 365 "CSIDL_APPDATA" 366 367 "CSIDL_COMMON_STARTMENU" 368 "CSIDL_STARTMENU" 369 370 "CSIDL_COMMON_DESKTOPDIRECTORY" 371 "CSIDL_DESKTOPDIRECTORY" 372 373 "CSIDL_COMMON_STARTUP" 374 "CSIDL_STARTUP" 375 376 "CSIDL_COMMON_PROGRAMS" 377 "CSIDL_PROGRAMS" 378 379 "CSIDL_FONTS" 380 381 If the folder cannot be retrieved, :exc:`OSError` is raised. 382 383 Which folders are available depends on the exact Windows version, and probably 384 also the configuration. For details refer to Microsoft's documentation of the 385 :c:func:`SHGetSpecialFolderPath` function. 386 387 388.. function:: create_shortcut(target, description, filename[, arguments[, workdir[, iconpath[, iconindex]]]]) 389 390 This function creates a shortcut. *target* is the path to the program to be 391 started by the shortcut. *description* is the description of the shortcut. 392 *filename* is the title of the shortcut that the user will see. *arguments* 393 specifies the command line arguments, if any. *workdir* is the working directory 394 for the program. *iconpath* is the file containing the icon for the shortcut, 395 and *iconindex* is the index of the icon in the file *iconpath*. Again, for 396 details consult the Microsoft documentation for the :class:`IShellLink` 397 interface. 398