blob: a24b764e5ad3571f3b1367b6efde90f9084a2bf0 [file] [log] [blame]
Éric Araujo3a9f58f2011-06-01 20:42:49 +02001.. highlightlang:: none
2
3====================================
4Installing Python projects: overwiew
5====================================
6
Éric Araujof8bebf82011-07-02 16:58:25 +02007.. _packaging-install-intro:
Éric Araujo3a9f58f2011-06-01 20:42:49 +02008
9Introduction
10============
11
12Although Python's extensive standard library covers many programming needs,
13there often comes a time when you need to add new functionality to your Python
14installation in the form of third-party modules. This might be necessary to
15support your own programming, or to support an application that you want to use
16and that happens to be written in Python.
17
18In the past, there was little support for adding third-party modules to an
19existing Python installation. With the introduction of the Python Distribution
20Utilities (Distutils for short) in Python 2.0, this changed. However, not all
21problems were solved; end-users had to rely on ``easy_install`` or
22``pip`` to download third-party modules from PyPI, uninstall distributions or do
23other maintenance operations. Packaging is a more complete replacement for
24Distutils, in the standard library, with a backport named Distutils2 available
25for older Python versions.
26
27This document is aimed primarily at people who need to install third-party
28Python modules: end-users and system administrators who just need to get some
29Python application running, and existing Python programmers who want to add
30new goodies to their toolbox. You don't need to know Python to read this
31document; there will be some brief forays into using Python's interactive mode
32to explore your installation, but that's it. If you're looking for information
33on how to distribute your own Python modules so that others may use them, see
34the :ref:`packaging-index` manual.
35
36
37.. _packaging-trivial-install:
38
39Best case: trivial installation
40-------------------------------
41
42In the best case, someone will have prepared a special version of the module
43distribution you want to install that is targeted specifically at your platform
44and can be installed just like any other software on your platform. For example,
45the module's developer might make an executable installer available for Windows
46users, an RPM package for users of RPM-based Linux systems (Red Hat, SuSE,
47Mandrake, and many others), a Debian package for users of Debian and derivative
48systems, and so forth.
49
50In that case, you would use the standard system tools to download and install
51the specific installer for your platform and its dependencies.
52
53Of course, things will not always be that easy. You might be interested in a
54module whose distribution doesn't have an easy-to-use installer for your
55platform. In that case, you'll have to start with the source distribution
56released by the module's author/maintainer. Installing from a source
57distribution is not too hard, as long as the modules are packaged in the
58standard way. The bulk of this document addresses the building and installing
59of modules from standard source distributions.
60
61
62.. _packaging-distutils:
63
64The Python standard: Distutils
65------------------------------
66
67If you download a source distribution of a module, it will be obvious whether
68it was packaged and distributed using Distutils. First, the distribution's name
69and version number will be featured prominently in the name of the downloaded
70archive, e.g. :file:`foo-1.0.tar.gz` or :file:`widget-0.9.7.zip`. Next, the
71archive will unpack into a similarly-named directory: :file:`foo-1.0` or
72:file:`widget-0.9.7`. Additionally, the distribution may contain a
73:file:`setup.cfg` file and a file named :file:`README.txt` ---or possibly just
74:file:`README`--- explaining that building and installing the module
75distribution is a simple matter of issuing the following command at your shell's
76prompt::
77
78 python setup.py install
79
80Third-party projects have extended Distutils to work around its limitations or
81add functionality. After some years of near-inactivity in Distutils, a new
82maintainer has started to standardize good ideas in PEPs and implement them in a
83new, improved version of Distutils, called Distutils2 or Packaging.
84
85
86.. _packaging-new-standard:
87
88The new standard: Packaging
89---------------------------
90
91The rules described in the first paragraph above apply to Packaging-based
92projects too: a source distribution will have a name like
93:file:`widget-0.9.7.zip`. One of the main differences with Distutils is that
94distributions no longer have a :file:`setup.py` script; it used to cause a
95number of issues. Now there is a unique script installed with Python itself::
96
97 pysetup install widget-0.9.7.zip
98
99Running this command is enough to build and install projects (Python modules or
100packages, scripts or whole applications), without even having to unpack the
101archive. It is also compatible with Distutils-based distributions.
102
103Unless you have to perform non-standard installations or customize the build
104process, you can stop reading this manual ---the above command is everything you
105need to get out of it.
106
107With :program:`pysetup`, you won't even have to manually download a distribution
108before installing it; see :ref:`packaging-pysetup`.
109
110
111.. _packaging-standard-install:
112
113Standard build and install
114==========================
115
116As described in section :ref:`packaging-new-standard`, building and installing
117a module distribution using Packaging usually comes down to one simple
118command::
119
120 pysetup run install_dist
121
122How you actually run this command depends on the platform and the command line
123interface it provides:
124
125* **Unix**: Use a shell prompt.
126* **Windows**: Open a command prompt ("DOS console") or use :command:`Powershell`.
127* **OS X**: Open a :command:`Terminal`.
128
129
130.. _packaging-platform-variations:
131
132Platform variations
133-------------------
134
135The setup command is meant to be run from the root directory of the source
136distribution, i.e. the top-level subdirectory that the module source
137distribution unpacks into. For example, if you've just downloaded a module
138source distribution :file:`foo-1.0.tar.gz` onto a Unix system, the normal
139steps to follow are these::
140
141 gunzip -c foo-1.0.tar.gz | tar xf - # unpacks into directory foo-1.0
142 cd foo-1.0
143 pysetup run install_dist
144
145On Windows, you'd probably download :file:`foo-1.0.zip`. If you downloaded the
146archive file to :file:`C:\\Temp`, then it would unpack into
147:file:`C:\\Temp\\foo-1.0`. To actually unpack the archive, you can use either
148an archive manipulator with a graphical user interface (such as WinZip or 7-Zip)
149or a command-line tool (such as :program:`unzip`, :program:`pkunzip` or, again,
150:program:`7z`). Then, open a command prompt window ("DOS box" or
151Powershell), and run::
152
153 cd c:\Temp\foo-1.0
154 pysetup run install_dist
155
156
157.. _packaging-splitting-up:
158
159Splitting the job up
160--------------------
161
162Running ``pysetup run install_dist`` builds and installs all modules in one go. If you
163prefer to work incrementally ---especially useful if you want to customize the
164build process, or if things are going wrong--- you can use the setup script to
165do one thing at a time. This is a valuable tool when different users will perform
166separately the build and install steps. For example, you might want to build a
167module distribution and hand it off to a system administrator for installation
168(or do it yourself, but with super-user or admin privileges).
169
170For example, to build everything in one step and then install everything
171in a second step, you aptly invoke two distinct Packaging commands::
172
173 pysetup run build
174 pysetup run install_dist
175
176If you do this, you will notice that invoking the :command:`install_dist` command
177first runs the :command:`build` command, which ---in this case--- quickly
178notices it can spare itself the work, since everything in the :file:`build`
179directory is up-to-date.
180
181You may often ignore this ability to divide the process in steps if all you do
182is installing modules downloaded from the Internet, but it's very handy for
183more advanced tasks. If you find yourself in the need for distributing your own
184Python modules and extensions, though, you'll most likely run many individual
185Packaging commands.
186
187
188.. _packaging-how-build-works:
189
190How building works
191------------------
192
193As implied above, the :command:`build` command is responsible for collecting
194and placing the files to be installed into a *build directory*. By default,
195this is :file:`build`, under the distribution root. If you're excessively
196concerned with speed, or want to keep the source tree pristine, you can specify
197a different build directory with the :option:`--build-base` option. For example::
198
199 pysetup run build --build-base /tmp/pybuild/foo-1.0
200
201(Or you could do this permanently with a directive in your system or personal
202Packaging configuration file; see section :ref:`packaging-config-files`.)
203In the usual case, however, all this is unnecessary.
204
205The build tree's default layout looks like so::
206
207 --- build/ --- lib/
208 or
209 --- build/ --- lib.<plat>/
210 temp.<plat>/
211
212where ``<plat>`` expands to a brief description of the current OS/hardware
213platform and Python version. The first form, with just a :file:`lib` directory,
214is used for pure module distributions (module distributions that
215include only pure Python modules). If a module distribution contains any
216extensions (modules written in C/C++), then the second form, with two ``<plat>``
217directories, is used. In that case, the :file:`temp.{plat}` directory holds
218temporary files generated during the compile/link process which are not intended
219to be installed. In either case, the :file:`lib` (or :file:`lib.{plat}`) directory
220contains all Python modules (pure Python and extensions) to be installed.
221
222In the future, more directories will be added to handle Python scripts,
223documentation, binary executables, and whatever else is required to install
224Python modules and applications.
225
226
227.. _packaging-how-install-works:
228
229How installation works
230----------------------
231
232After the :command:`build` command is run (whether explicitly or by the
233:command:`install_dist` command on your behalf), the work of the :command:`install_dist`
234command is relatively simple: all it has to do is copy the contents of
235:file:`build/lib` (or :file:`build/lib.{plat}`) to the installation directory
236of your choice.
237
238If you don't choose an installation directory ---i.e., if you just run
239``pysetup run install_dist``\ --- then the :command:`install_dist` command
240installs to the standard location for third-party Python modules. This location
241varies by platform and depending on how you built/installed Python itself. On
242Unix (and Mac OS X, which is also Unix-based), it also depends on whether the
243module distribution being installed is pure Python or contains extensions
244("non-pure"):
245
246+-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
247| Platform | Standard installation location | Default value | Notes |
248+=================+=====================================================+==================================================+=======+
249| Unix (pure) | :file:`{prefix}/lib/python{X.Y}/site-packages` | :file:`/usr/local/lib/python{X.Y}/site-packages` | \(1) |
250+-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
251| Unix (non-pure) | :file:`{exec-prefix}/lib/python{X.Y}/site-packages` | :file:`/usr/local/lib/python{X.Y}/site-packages` | \(1) |
252+-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
253| Windows | :file:`{prefix}\\Lib\\site-packages` | :file:`C:\\Python{XY}\\Lib\\site-packages` | \(2) |
254+-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
255
256Notes:
257
258(1)
259 Most Linux distributions include Python as a standard part of the system, so
260 :file:`{prefix}` and :file:`{exec-prefix}` are usually both :file:`/usr` on
261 Linux. If you build Python yourself on Linux (or any Unix-like system), the
262 default :file:`{prefix}` and :file:`{exec-prefix}` are :file:`/usr/local`.
263
264(2)
265 The default installation directory on Windows was :file:`C:\\Program
266 Files\\Python` under Python 1.6a1, 1.5.2, and earlier.
267
268:file:`{prefix}` and :file:`{exec-prefix}` stand for the directories that Python
269is installed to, and where it finds its libraries at run-time. They are always
270the same under Windows, and very often the same under Unix and Mac OS X. You
271can find out what your Python installation uses for :file:`{prefix}` and
272:file:`{exec-prefix}` by running Python in interactive mode and typing a few
273simple commands.
274
275.. TODO link to Doc/using instead of duplicating
276
277To start the interactive Python interpreter, you need to follow a slightly
278different recipe for each platform. Under Unix, just type :command:`python` at
279the shell prompt. Under Windows (assuming the Python executable is on your
280:envvar:`PATH`, which is the usual case), you can choose :menuselection:`Start --> Run`,
281type ``python`` and press ``enter``. Alternatively, you can simply execute
282:command:`python` at a command prompt ("DOS console" or Powershell).
283
284Once the interpreter is started, you type Python code at the prompt. For
285example, on my Linux system, I type the three Python statements shown below,
286and get the output as shown, to find out my :file:`{prefix}` and :file:`{exec-prefix}`::
287
288 Python 3.3 (r32:88445, Apr 2 2011, 10:43:54)
289 Type "help", "copyright", "credits" or "license" for more information.
290 >>> import sys
291 >>> sys.prefix
292 '/usr'
293 >>> sys.exec_prefix
294 '/usr'
295
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200296A few other placeholders are used in this document: :file:`{X.Y}` stands for the
297version of Python, for example ``3.2``; :file:`{abiflags}` will be replaced by
298the value of :data:`sys.abiflags` or the empty string for platforms which don't
299define ABI flags; :file:`{distname}` will be replaced by the name of the module
300distribution being installed. Dots and capitalization are important in the
301paths; for example, a value that uses ``python3.2`` on UNIX will typically use
302``Python32`` on Windows.
303
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200304If you don't want to install modules to the standard location, or if you don't
305have permission to write there, then you need to read about alternate
306installations in section :ref:`packaging-alt-install`. If you want to customize your
307installation directories more heavily, see section :ref:`packaging-custom-install`.
308
309
310.. _packaging-alt-install:
311
312Alternate installation
313======================
314
315Often, it is necessary or desirable to install modules to a location other than
316the standard location for third-party Python modules. For example, on a Unix
317system you might not have permission to write to the standard third-party module
318directory. Or you might wish to try out a module before making it a standard
319part of your local Python installation. This is especially true when upgrading
320a distribution already present: you want to make sure your existing base of
321scripts still works with the new version before actually upgrading.
322
323The Packaging :command:`install_dist` command is designed to make installing module
324distributions to an alternate location simple and painless. The basic idea is
325that you supply a base directory for the installation, and the
326:command:`install_dist` command picks a set of directories (called an *installation
327scheme*) under this base directory in which to install files. The details
328differ across platforms, so read whichever of the following sections applies to
329you.
330
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200331Note that the various alternate installation schemes are mutually exclusive: you
332can pass ``--user``, or ``--home``, or ``--prefix`` and ``--exec-prefix``, or
333``--install-base`` and ``--install-platbase``, but you can't mix from these
334groups.
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200335
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200336
337.. _packaging-alt-install-user:
338
339Alternate installation: the user scheme
340---------------------------------------
341
342This scheme is designed to be the most convenient solution for users that don't
343have write permission to the global site-packages directory or don't want to
344install into it. It is enabled with a simple option::
345
346 pysetup run install_dist --user
347
348Files will be installed into subdirectories of :data:`site.USER_BASE` (written
349as :file:`{userbase}` hereafter). This scheme installs pure Python modules and
350extension modules in the same location (also known as :data:`site.USER_SITE`).
351Here are the values for UNIX, including non-framework builds on Mac OS X:
352
353=============== ===========================================================
354Type of file Installation directory
355=============== ===========================================================
356modules :file:`{userbase}/lib/python{X.Y}/site-packages`
357scripts :file:`{userbase}/bin`
358data :file:`{userbase}`
359C headers :file:`{userbase}/include/python{X.Y}`
360=============== ===========================================================
361
362Framework builds on Mac OS X use these paths:
363
364=============== ===========================================================
365Type of file Installation directory
366=============== ===========================================================
367modules :file:`{userbase}/lib/python/site-packages`
368scripts :file:`{userbase}/bin`
369data :file:`{userbase}`
370C headers :file:`{userbase}/include/python`
371=============== ===========================================================
372
373And here are the values used on Windows:
374
375=============== ===========================================================
376Type of file Installation directory
377=============== ===========================================================
378modules :file:`{userbase}\\Python{XY}\\site-packages`
379scripts :file:`{userbase}\\Scripts`
380data :file:`{userbase}`
381C headers :file:`{userbase}\\Python{XY}\\Include`
382=============== ===========================================================
383
384The advantage of using this scheme compared to the other ones described below is
385that the user site-packages directory is under normal conditions always included
386in :data:`sys.path` (see :mod:`site` for more information), which means that
387there is no additional step to perform after running ``pysetup`` to finalize the
388installation.
389
390The :command:`build_ext` command also has a ``--user`` option to add
391:file:`{userbase}/include` to the compiler search path for header files and
392:file:`{userbase}/lib` to the compiler search path for libraries as well as to
393the runtime search path for shared C libraries (rpath).
394
395
396.. _packaging-alt-install-home:
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200397
398Alternate installation: the home scheme
399---------------------------------------
400
401The idea behind the "home scheme" is that you build and maintain a personal
402stash of Python modules. This scheme's name is derived from the concept of a
403"home" directory on Unix, since it's not unusual for a Unix user to make their
404home directory have a layout similar to :file:`/usr/` or :file:`/usr/local/`.
405In spite of its name's origin, this scheme can be used by anyone, regardless
406of the operating system.
407
408Installing a new module distribution in this way is as simple as ::
409
410 pysetup run install_dist --home <dir>
411
412where you can supply any directory you like for the :option:`--home` option. On
413Unix, lazy typists can just type a tilde (``~``); the :command:`install_dist` command
414will expand this to your home directory::
415
416 pysetup run install_dist --home ~
417
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200418To make Python find the distributions installed with this scheme, you may have
419to :ref:`modify Python's search path <inst-search-path>` or edit
420:mod:`sitecustomize` (see :mod:`site`) to call :func:`site.addsitedir` or edit
421:data:`sys.path`.
422
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200423The :option:`--home` option defines the base directory for the installation.
424Under it, files are installed to the following directories:
425
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200426=============== ===========================================================
427Type of file Installation directory
428=============== ===========================================================
429modules :file:`{home}/lib/python`
430scripts :file:`{home}/bin`
431data :file:`{home}`
432C headers :file:`{home}/include/python`
433=============== ===========================================================
434
435(Mentally replace slashes with backslashes if you're on Windows.)
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200436
437
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200438.. _packaging-alt-install-prefix-unix:
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200439
440Alternate installation: Unix (the prefix scheme)
441------------------------------------------------
442
443The "prefix scheme" is useful when you wish to use one Python installation to
444run the build command, but install modules into the third-party module directory
445of a different Python installation (or something that looks like a different
446Python installation). If this sounds a trifle unusual, it is ---that's why the
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200447user and home schemes come before. However, there are at least two known cases
448where the prefix scheme will be useful.
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200449
450First, consider that many Linux distributions put Python in :file:`/usr`, rather
451than the more traditional :file:`/usr/local`. This is entirely appropriate,
452since in those cases Python is part of "the system" rather than a local add-on.
453However, if you are installing Python modules from source, you probably want
454them to go in :file:`/usr/local/lib/python2.{X}` rather than
455:file:`/usr/lib/python2.{X}`. This can be done with ::
456
457 pysetup run install_dist --prefix /usr/local
458
459Another possibility is a network filesystem where the name used to write to a
460remote directory is different from the name used to read it: for example, the
461Python interpreter accessed as :file:`/usr/local/bin/python` might search for
462modules in :file:`/usr/local/lib/python2.{X}`, but those modules would have to
463be installed to, say, :file:`/mnt/{@server}/export/lib/python2.{X}`. This could
464be done with ::
465
466 pysetup run install_dist --prefix=/mnt/@server/export
467
468In either case, the :option:`--prefix` option defines the installation base, and
469the :option:`--exec-prefix` option defines the platform-specific installation
470base, which is used for platform-specific files. (Currently, this just means
471non-pure module distributions, but could be expanded to C libraries, binary
472executables, etc.) If :option:`--exec-prefix` is not supplied, it defaults to
473:option:`--prefix`. Files are installed as follows:
474
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200475================= ==========================================================
476Type of file Installation directory
477================= ==========================================================
478Python modules :file:`{prefix}/lib/python{X.Y}/site-packages`
479extension modules :file:`{exec-prefix}/lib/python{X.Y}/site-packages`
480scripts :file:`{prefix}/bin`
481data :file:`{prefix}`
482C headers :file:`{prefix}/include/python{X.Y}{abiflags}`
483================= ==========================================================
484
485.. XXX misses an entry for platinclude
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200486
487There is no requirement that :option:`--prefix` or :option:`--exec-prefix`
488actually point to an alternate Python installation; if the directories listed
489above do not already exist, they are created at installation time.
490
491Incidentally, the real reason the prefix scheme is important is simply that a
492standard Unix installation uses the prefix scheme, but with :option:`--prefix`
493and :option:`--exec-prefix` supplied by Python itself as ``sys.prefix`` and
494``sys.exec_prefix``. Thus, you might think you'll never use the prefix scheme,
495but every time you run ``pysetup run install_dist`` without any other
496options, you're using it.
497
498Note that installing extensions to an alternate Python installation doesn't have
499anything to do with how those extensions are built: in particular, extensions
500will be compiled using the Python header files (:file:`Python.h` and friends)
501installed with the Python interpreter used to run the build command. It is
502therefore your responsibility to ensure compatibility between the interpreter
503intended to run extensions installed in this way and the interpreter used to
504build these same extensions. To avoid problems, it is best to make sure that
505the two interpreters are the same version of Python (possibly different builds,
506or possibly copies of the same build). (Of course, if your :option:`--prefix`
507and :option:`--exec-prefix` don't even point to an alternate Python installation,
508this is immaterial.)
509
510
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200511.. _packaging-alt-install-prefix-windows:
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200512
513Alternate installation: Windows (the prefix scheme)
514---------------------------------------------------
515
516Windows has a different and vaguer notion of home directories than Unix, and
517since its standard Python installation is simpler, the :option:`--prefix` option
518has traditionally been used to install additional packages to arbitrary
519locations. ::
520
521 pysetup run install_dist --prefix "\Temp\Python"
522
523to install modules to the :file:`\\Temp\\Python` directory on the current drive.
524
525The installation base is defined by the :option:`--prefix` option; the
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200526:option:`--exec-prefix` option is not supported under Windows, which means that
527pure Python modules and extension modules are installed into the same location.
528Files are installed as follows:
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200529
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200530=============== ==========================================================
531Type of file Installation directory
532=============== ==========================================================
533modules :file:`{prefix}\\Lib\\site-packages`
534scripts :file:`{prefix}\\Scripts`
535data :file:`{prefix}`
536C headers :file:`{prefix}\\Include`
537=============== ==========================================================
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200538
539
540.. _packaging-custom-install:
541
542Custom installation
543===================
544
545Sometimes, the alternate installation schemes described in section
546:ref:`packaging-alt-install` just don't do what you want. You might want to tweak
547just one or two directories while keeping everything under the same base
548directory, or you might want to completely redefine the installation scheme.
549In either case, you're creating a *custom installation scheme*.
550
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200551To create a custom installation scheme, you start with one of the alternate
552schemes and override some of the installation directories used for the various
553types of files, using these options:
554
555====================== =======================
556Type of file Override option
557====================== =======================
558Python modules ``--install-purelib``
559extension modules ``--install-platlib``
560all modules ``--install-lib``
561scripts ``--install-scripts``
562data ``--install-data``
563C headers ``--install-headers``
564====================== =======================
565
566These override options can be relative, absolute,
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200567or explicitly defined in terms of one of the installation base directories.
568(There are two installation base directories, and they are normally the same
569---they only differ when you use the Unix "prefix scheme" and supply different
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200570``--prefix`` and ``--exec-prefix`` options; using ``--install-lib`` will
571override values computed or given for ``--install-purelib`` and
572``--install-platlib``, and is recommended for schemes that don't make a
573difference between Python and extension modules.)
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200574
575For example, say you're installing a module distribution to your home directory
576under Unix, but you want scripts to go in :file:`~/scripts` rather than
577:file:`~/bin`. As you might expect, you can override this directory with the
578:option:`--install-scripts` option and, in this case, it makes most sense to supply
579a relative path, which will be interpreted relative to the installation base
580directory (in our example, your home directory)::
581
582 pysetup run install_dist --home ~ --install-scripts scripts
583
584Another Unix example: suppose your Python installation was built and installed
585with a prefix of :file:`/usr/local/python`. Thus, in a standard installation,
586scripts will wind up in :file:`/usr/local/python/bin`. If you want them in
587:file:`/usr/local/bin` instead, you would supply this absolute directory for
588the :option:`--install-scripts` option::
589
590 pysetup run install_dist --install-scripts /usr/local/bin
591
592This command performs an installation using the "prefix scheme", where the
593prefix is whatever your Python interpreter was installed with ---in this case,
594:file:`/usr/local/python`.
595
596If you maintain Python on Windows, you might want third-party modules to live in
597a subdirectory of :file:`{prefix}`, rather than right in :file:`{prefix}`
598itself. This is almost as easy as customizing the script installation directory
599---you just have to remember that there are two types of modules to worry about,
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200600Python and extension modules, which can conveniently be both controlled by one
601option::
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200602
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200603 pysetup run install_dist --install-lib Site
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200604
605.. XXX Nothing is installed right under prefix in windows, is it??
606
Éric Araujo7dc76fd2011-08-06 16:58:15 +0200607The specified installation directory is relative to :file:`{prefix}`. Of
608course, you also have to ensure that this directory is in Python's module
609search path, such as by putting a :file:`.pth` file in a site directory (see
610:mod:`site`). See section :ref:`packaging-search-path` to find out how to modify
611Python's search path.
Éric Araujo3a9f58f2011-06-01 20:42:49 +0200612
613If you want to define an entire installation scheme, you just have to supply all
614of the installation directory options. Using relative paths is recommended here.
615For example, if you want to maintain all Python module-related files under
616:file:`python` in your home directory, and you want a separate directory for
617each platform that you use your home directory from, you might define the
618following installation scheme::
619
620 pysetup run install_dist --home ~ \
621 --install-purelib python/lib \
622 --install-platlib python/'lib.$PLAT' \
623 --install-scripts python/scripts \
624 --install-data python/data
625
626or, equivalently, ::
627
628 pysetup run install_dist --home ~/python \
629 --install-purelib lib \
630 --install-platlib 'lib.$PLAT' \
631 --install-scripts scripts \
632 --install-data data
633
634``$PLAT`` doesn't need to be defined as an environment variable ---it will also
635be expanded by Packaging as it parses your command line options, just as it
636does when parsing your configuration file(s). (More on that later.)
637
638Obviously, specifying the entire installation scheme every time you install a
639new module distribution would be very tedious. To spare you all that work, you
640can store it in a Packaging configuration file instead (see section
641:ref:`packaging-config-files`), like so::
642
643 [install_dist]
644 install-base = $HOME
645 install-purelib = python/lib
646 install-platlib = python/lib.$PLAT
647 install-scripts = python/scripts
648 install-data = python/data
649
650or, equivalently, ::
651
652 [install_dist]
653 install-base = $HOME/python
654 install-purelib = lib
655 install-platlib = lib.$PLAT
656 install-scripts = scripts
657 install-data = data
658
659Note that these two are *not* equivalent if you override their installation
660base directory when running the setup script. For example, ::
661
662 pysetup run install_dist --install-base /tmp
663
664would install pure modules to :file:`/tmp/python/lib` in the first case, and
665to :file:`/tmp/lib` in the second case. (For the second case, you'd probably
666want to supply an installation base of :file:`/tmp/python`.)
667
668You may have noticed the use of ``$HOME`` and ``$PLAT`` in the sample
669configuration file. These are Packaging configuration variables, which
670bear a strong resemblance to environment variables. In fact, you can use
671environment variables in configuration files on platforms that have such a notion, but
672Packaging additionally defines a few extra variables that may not be in your
673environment, such as ``$PLAT``. Of course, on systems that don't have
674environment variables, such as Mac OS 9, the configuration variables supplied by
675the Packaging are the only ones you can use. See section :ref:`packaging-config-files`
676for details.
677
678.. XXX which vars win out eventually in case of clash env or Packaging?
679
680.. XXX need some Windows examples---when would custom installation schemes be
681 needed on those platforms?
682
683
684.. XXX Move this section to Doc/using
685
686.. _packaging-search-path:
687
688Modifying Python's search path
689------------------------------
690
691When the Python interpreter executes an :keyword:`import` statement, it searches
692for both Python code and extension modules along a search path. A default value
693for this path is configured into the Python binary when the interpreter is built.
694You can obtain the search path by importing the :mod:`sys` module and printing
695the value of ``sys.path``. ::
696
697 $ python
698 Python 2.2 (#11, Oct 3 2002, 13:31:27)
699 [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
700 Type "help", "copyright", "credits" or "license" for more information.
701 >>> import sys
702 >>> sys.path
703 ['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
704 '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
705 '/usr/local/lib/python2.3/site-packages']
706 >>>
707
708The null string in ``sys.path`` represents the current working directory.
709
710The expected convention for locally installed packages is to put them in the
711:file:`{...}/site-packages/` directory, but you may want to choose a different
712location for some reason. For example, if your site kept by convention all web
713server-related software under :file:`/www`. Add-on Python modules might then
714belong in :file:`/www/python`, and in order to import them, this directory would
715have to be added to ``sys.path``. There are several ways to solve this problem.
716
717The most convenient way is to add a path configuration file to a directory
718that's already on Python's path, usually to the :file:`.../site-packages/`
719directory. Path configuration files have an extension of :file:`.pth`, and each
720line must contain a single path that will be appended to ``sys.path``. (Because
721the new paths are appended to ``sys.path``, modules in the added directories
722will not override standard modules. This means you can't use this mechanism for
723installing fixed versions of standard modules.)
724
725Paths can be absolute or relative, in which case they're relative to the
726directory containing the :file:`.pth` file. See the documentation of
727the :mod:`site` module for more information.
728
729A slightly less convenient way is to edit the :file:`site.py` file in Python's
730standard library, and modify ``sys.path``. :file:`site.py` is automatically
731imported when the Python interpreter is executed, unless the :option:`-S` switch
732is supplied to suppress this behaviour. So you could simply edit
733:file:`site.py` and add two lines to it::
734
735 import sys
736 sys.path.append('/www/python/')
737
738However, if you reinstall the same major version of Python (perhaps when
739upgrading from 3.3 to 3.3.1, for example) :file:`site.py` will be overwritten by
740the stock version. You'd have to remember that it was modified and save a copy
741before doing the installation.
742
743Alternatively, there are two environment variables that can modify ``sys.path``.
744:envvar:`PYTHONHOME` sets an alternate value for the prefix of the Python
745installation. For example, if :envvar:`PYTHONHOME` is set to ``/www/python``,
746the search path will be set to ``['', '/www/python/lib/pythonX.Y/',
747'/www/python/lib/pythonX.Y/plat-linux2', ...]``.
748
749The :envvar:`PYTHONPATH` variable can be set to a list of paths that will be
750added to the beginning of ``sys.path``. For example, if :envvar:`PYTHONPATH` is
751set to ``/www/python:/opt/py``, the search path will begin with
752``['/www/python', '/opt/py']``. (Note that directories must exist in order to
753be added to ``sys.path``; the :mod:`site` module removes non-existent paths.)
754
755Finally, ``sys.path`` is just a regular Python list, so any Python application
756can modify it by adding or removing entries.
757
758
759.. _packaging-config-files:
760
761Configuration files for Packaging
762=================================
763
764As mentioned above, you can use configuration files to store personal or site
765preferences for any option supported by any Packaging command. Depending on your
766platform, you can use one of two or three possible configuration files. These
767files will be read before parsing the command-line, so they take precedence over
768default values. In turn, the command-line will override configuration files.
769Lastly, if there are multiple configuration files, values from files read
770earlier will be overridden by values from files read later.
771
772.. XXX "one of two or three possible..." seems wrong info. Below always 3 files
773 are indicated in the tables.
774
775
776.. _packaging-config-filenames:
777
778Location and names of configuration files
779-----------------------------------------
780
781The name and location of the configuration files vary slightly across
782platforms. On Unix and Mac OS X, these are the three configuration files listed
783in the order they are processed:
784
785+--------------+----------------------------------------------------------+-------+
786| Type of file | Location and filename | Notes |
787+==============+==========================================================+=======+
788| system | :file:`{prefix}/lib/python{ver}/packaging/packaging.cfg` | \(1) |
789+--------------+----------------------------------------------------------+-------+
790| personal | :file:`$HOME/.pydistutils.cfg` | \(2) |
791+--------------+----------------------------------------------------------+-------+
792| local | :file:`setup.cfg` | \(3) |
793+--------------+----------------------------------------------------------+-------+
794
795Similarly, the configuration files on Windows ---also listed in the order they
796are processed--- are these:
797
798+--------------+-------------------------------------------------+-------+
799| Type of file | Location and filename | Notes |
800+==============+=================================================+=======+
801| system | :file:`{prefix}\\Lib\\packaging\\packaging.cfg` | \(4) |
802+--------------+-------------------------------------------------+-------+
803| personal | :file:`%HOME%\\pydistutils.cfg` | \(5) |
804+--------------+-------------------------------------------------+-------+
805| local | :file:`setup.cfg` | \(3) |
806+--------------+-------------------------------------------------+-------+
807
808On all platforms, the *personal* file can be temporarily disabled by
809means of the `--no-user-cfg` option.
810
811Notes:
812
813(1)
814 Strictly speaking, the system-wide configuration file lives in the directory
815 where Packaging is installed.
816
817(2)
818 On Unix, if the :envvar:`HOME` environment variable is not defined, the
819 user's home directory will be determined with the :func:`getpwuid` function
820 from the standard :mod:`pwd` module. Packaging uses the
821 :func:`os.path.expanduser` function to do this.
822
823(3)
824 I.e., in the current directory (usually the location of the setup script).
825
826(4)
827 (See also note (1).) Python's default installation prefix is
828 :file:`C:\\Python`, so the system configuration file is normally
829 :file:`C:\\Python\\Lib\\packaging\\packaging.cfg`.
830
831(5)
832 On Windows, if the :envvar:`HOME` environment variable is not defined,
833 :envvar:`USERPROFILE` then :envvar:`HOMEDRIVE` and :envvar:`HOMEPATH` will
834 be tried. Packaging uses the :func:`os.path.expanduser` function to do this.
835
836
837.. _packaging-config-syntax:
838
839Syntax of configuration files
840-----------------------------
841
842All Packaging configuration files share the same syntax. Options defined in
843them are grouped into sections, and each Packaging command gets its own section.
844Additionally, there's a ``global`` section for options that affect every command.
845Sections consist of one or more lines containing a single option specified as
846``option = value``.
847
848For example, here's a complete configuration file that forces all commands to
849run quietly by default::
850
851 [global]
852 verbose = 0
853
854If this was the system configuration file, it would affect all processing
855of any Python module distribution by any user on the current system. If it was
856installed as your personal configuration file (on systems that support them),
857it would affect only module distributions processed by you. Lastly, if it was
858used as the :file:`setup.cfg` for a particular module distribution, it would
859affect that distribution only.
860
861.. XXX "(on systems that support them)" seems wrong info
862
863If you wanted to, you could override the default "build base" directory and
864make the :command:`build\*` commands always forcibly rebuild all files with
865the following::
866
867 [build]
868 build-base = blib
869 force = 1
870
871which corresponds to the command-line arguments::
872
873 pysetup run build --build-base blib --force
874
875except that including the :command:`build` command on the command-line means
876that command will be run. Including a particular command in configuration files
877has no such implication; it only means that if the command is run, the options
878for it in the configuration file will apply. (This is also true if you run
879other commands that derive values from it.)
880
881You can find out the complete list of options for any command using the
882:option:`--help` option, e.g.::
883
884 pysetup run build --help
885
886and you can find out the complete list of global options by using
887:option:`--help` without a command::
888
889 pysetup run --help
890
891See also the "Reference" section of the "Distributing Python Modules" manual.
892
893.. XXX no links to the relevant section exist.
894
895
896.. _packaging-building-ext:
897
898Building extensions: tips and tricks
899====================================
900
901Whenever possible, Packaging tries to use the configuration information made
902available by the Python interpreter used to run `pysetup`.
903For example, the same compiler and linker flags used to compile Python will also
904be used for compiling extensions. Usually this will work well, but in
905complicated situations this might be inappropriate. This section discusses how
906to override the usual Packaging behaviour.
907
908
909.. _packaging-tweak-flags:
910
911Tweaking compiler/linker flags
912------------------------------
913
914Compiling a Python extension written in C or C++ will sometimes require
915specifying custom flags for the compiler and linker in order to use a particular
916library or produce a special kind of object code. This is especially true if the
917extension hasn't been tested on your platform, or if you're trying to
918cross-compile Python.
919
920.. TODO update to new setup.cfg
921
922In the most general case, the extension author might have foreseen that
923compiling the extensions would be complicated, and provided a :file:`Setup` file
924for you to edit. This will likely only be done if the module distribution
925contains many separate extension modules, or if they often require elaborate
926sets of compiler flags in order to work.
927
928A :file:`Setup` file, if present, is parsed in order to get a list of extensions
929to build. Each line in a :file:`Setup` describes a single module. Lines have
930the following structure::
931
932 module ... [sourcefile ...] [cpparg ...] [library ...]
933
934
935Let's examine each of the fields in turn.
936
937* *module* is the name of the extension module to be built, and should be a
938 valid Python identifier. You can't just change this in order to rename a module
939 (edits to the source code would also be needed), so this should be left alone.
940
941* *sourcefile* is anything that's likely to be a source code file, at least
942 judging by the filename. Filenames ending in :file:`.c` are assumed to be
943 written in C, filenames ending in :file:`.C`, :file:`.cc`, and :file:`.c++` are
944 assumed to be C++, and filenames ending in :file:`.m` or :file:`.mm` are assumed
945 to be in Objective C.
946
947* *cpparg* is an argument for the C preprocessor, and is anything starting with
948 :option:`-I`, :option:`-D`, :option:`-U` or :option:`-C`.
949
950* *library* is anything ending in :file:`.a` or beginning with :option:`-l` or
951 :option:`-L`.
952
953If a particular platform requires a special library on your platform, you can
954add it by editing the :file:`Setup` file and running ``pysetup run build``.
955For example, if the module defined by the line ::
956
957 foo foomodule.c
958
959must be linked with the math library :file:`libm.a` on your platform, simply add
960:option:`-lm` to the line::
961
962 foo foomodule.c -lm
963
964Arbitrary switches intended for the compiler or the linker can be supplied with
965the :option:`-Xcompiler` *arg* and :option:`-Xlinker` *arg* options::
966
967 foo foomodule.c -Xcompiler -o32 -Xlinker -shared -lm
968
969The next option after :option:`-Xcompiler` and :option:`-Xlinker` will be
970appended to the proper command line, so in the above example the compiler will
971be passed the :option:`-o32` option, and the linker will be passed
972:option:`-shared`. If a compiler option requires an argument, you'll have to
973supply multiple :option:`-Xcompiler` options; for example, to pass ``-x c++``
974the :file:`Setup` file would have to contain ``-Xcompiler -x -Xcompiler c++``.
975
976Compiler flags can also be supplied through setting the :envvar:`CFLAGS`
977environment variable. If set, the contents of :envvar:`CFLAGS` will be added to
978the compiler flags specified in the :file:`Setup` file.
979
980
981.. _packaging-non-ms-compilers:
982
983Using non-Microsoft compilers on Windows
984----------------------------------------
985
986.. sectionauthor:: Rene Liebscher <R.Liebscher@gmx.de>
987
988
989
990Borland/CodeGear C++
991^^^^^^^^^^^^^^^^^^^^
992
993This subsection describes the necessary steps to use Packaging with the Borland
994C++ compiler version 5.5. First you have to know that Borland's object file
995format (OMF) is different from the format used by the Python version you can
996download from the Python or ActiveState Web site. (Python is built with
997Microsoft Visual C++, which uses COFF as the object file format.) For this
998reason, you have to convert Python's library :file:`python25.lib` into the
999Borland format. You can do this as follows:
1000
1001.. Should we mention that users have to create cfg-files for the compiler?
1002.. see also http://community.borland.com/article/0,1410,21205,00.html
1003
1004::
1005
1006 coff2omf python25.lib python25_bcpp.lib
1007
1008The :file:`coff2omf` program comes with the Borland compiler. The file
1009:file:`python25.lib` is in the :file:`Libs` directory of your Python
1010installation. If your extension uses other libraries (zlib, ...) you have to
1011convert them too.
1012
1013The converted files have to reside in the same directories as the normal
1014libraries.
1015
1016How does Packaging manage to use these libraries with their changed names? If
1017the extension needs a library (eg. :file:`foo`) Packaging checks first if it
1018finds a library with suffix :file:`_bcpp` (eg. :file:`foo_bcpp.lib`) and then
1019uses this library. In the case it doesn't find such a special library it uses
1020the default name (:file:`foo.lib`.) [#]_
1021
1022To let Packaging compile your extension with Borland, C++ you now have to
1023type::
1024
1025 pysetup run build --compiler bcpp
1026
1027If you want to use the Borland C++ compiler as the default, you could specify
1028this in your personal or system-wide configuration file for Packaging (see
1029section :ref:`packaging-config-files`.)
1030
1031
1032.. seealso::
1033
1034 `C++Builder Compiler <http://www.codegear.com/downloads/free/cppbuilder>`_
1035 Information about the free C++ compiler from Borland, including links to the
1036 download pages.
1037
1038 `Creating Python Extensions Using Borland's Free Compiler <http://www.cyberus.ca/~g_will/pyExtenDL.shtml>`_
1039 Document describing how to use Borland's free command-line C++ compiler to build
1040 Python.
1041
1042
1043GNU C / Cygwin / MinGW
1044^^^^^^^^^^^^^^^^^^^^^^
1045
1046This section describes the necessary steps to use Packaging with the GNU C/C++
1047compilers in their Cygwin and MinGW distributions. [#]_ For a Python interpreter
1048that was built with Cygwin, everything should work without any of these
1049following steps.
1050
1051Not all extensions can be built with MinGW or Cygwin, but many can. Extensions
1052most likely to not work are those that use C++ or depend on Microsoft Visual C
1053extensions.
1054
1055To let Packaging compile your extension with Cygwin, you have to type::
1056
1057 pysetup run build --compiler=cygwin
1058
1059and for Cygwin in no-cygwin mode [#]_ or for MinGW, type::
1060
1061 pysetup run build --compiler=mingw32
1062
1063If you want to use any of these options/compilers as default, you should
1064consider writing it in your personal or system-wide configuration file for
1065Packaging (see section :ref:`packaging-config-files`.)
1066
1067Older Versions of Python and MinGW
1068""""""""""""""""""""""""""""""""""
1069The following instructions only apply if you're using a version of Python
1070inferior to 2.4.1 with a MinGW inferior to 3.0.0 (with
1071:file:`binutils-2.13.90-20030111-1`).
1072
1073These compilers require some special libraries. This task is more complex than
1074for Borland's C++, because there is no program to convert the library. First
1075you have to create a list of symbols which the Python DLL exports. (You can find
1076a good program for this task at
1077http://www.emmestech.com/software/pexports-0.43/download_pexports.html).
1078
1079.. I don't understand what the next line means. --amk
1080 (inclusive the references on data structures.)
1081
1082::
1083
1084 pexports python25.dll > python25.def
1085
1086The location of an installed :file:`python25.dll` will depend on the
1087installation options and the version and language of Windows. In a "just for
1088me" installation, it will appear in the root of the installation directory. In
1089a shared installation, it will be located in the system directory.
1090
1091Then you can create from these information an import library for gcc. ::
1092
1093 /cygwin/bin/dlltool --dllname python25.dll --def python25.def --output-lib libpython25.a
1094
1095The resulting library has to be placed in the same directory as
1096:file:`python25.lib`. (Should be the :file:`libs` directory under your Python
1097installation directory.)
1098
1099If your extension uses other libraries (zlib,...) you might have to convert
1100them too. The converted files have to reside in the same directories as the
1101normal libraries do.
1102
1103
1104.. seealso::
1105
1106 `Building Python modules on MS Windows platform with MinGW <http://www.zope.org/Members/als/tips/win32_mingw_modules>`_
1107 Information about building the required libraries for the MinGW
1108 environment.
1109
1110
1111.. rubric:: Footnotes
1112
1113.. [#] This also means you could replace all existing COFF-libraries with
1114 OMF-libraries of the same name.
1115
1116.. [#] Check http://sources.redhat.com/cygwin/ and http://www.mingw.org/ for
1117 more information.
1118
1119.. [#] Then you have no POSIX emulation available, but you also don't need
1120 :file:`cygwin1.dll`.