blob: 159f84816da4fe6fc32e585473e9a907b8777e15 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. highlightlang:: none
2
3.. _install-index:
4
5*****************************
Georg Brandlc62ef8b2009-01-03 20:55:06 +00006 Installing Python Modules
Georg Brandl8ec7f652007-08-15 14:28:01 +00007*****************************
8
9:Author: Greg Ward
Georg Brandl8ec7f652007-08-15 14:28:01 +000010
Georg Brandlb19be572007-12-29 10:57:00 +000011.. TODO: Fill in XXX comments
Georg Brandl8ec7f652007-08-15 14:28:01 +000012
Georg Brandlb19be572007-12-29 10:57:00 +000013.. The audience for this document includes people who don't know anything
14 about Python and aren't about to learn the language just in order to
15 install and maintain it for their users, i.e. system administrators.
16 Thus, I have to be sure to explain the basics at some point:
17 sys.path and PYTHONPATH at least. Should probably give pointers to
18 other docs on "import site", PYTHONSTARTUP, PYTHONHOME, etc.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000019
Georg Brandlb19be572007-12-29 10:57:00 +000020 Finally, it might be useful to include all the material from my "Care
21 and Feeding of a Python Installation" talk in here somewhere. Yow!
Georg Brandl8ec7f652007-08-15 14:28:01 +000022
23.. topic:: Abstract
24
25 This document describes the Python Distribution Utilities ("Distutils") from the
26 end-user's point-of-view, describing how to extend the capabilities of a
27 standard Python installation by building and installing third-party Python
28 modules and extensions.
29
30
31.. _inst-intro:
32
33Introduction
34============
35
36Although Python's extensive standard library covers many programming needs,
37there often comes a time when you need to add some new functionality to your
38Python installation in the form of third-party modules. This might be necessary
39to support your own programming, or to support an application that you want to
40use and that happens to be written in Python.
41
42In the past, there has been little support for adding third-party modules to an
43existing Python installation. With the introduction of the Python Distribution
44Utilities (Distutils for short) in Python 2.0, this changed.
45
46This document is aimed primarily at the people who need to install third-party
47Python modules: end-users and system administrators who just need to get some
48Python application running, and existing Python programmers who want to add some
49new goodies to their toolbox. You don't need to know Python to read this
50document; there will be some brief forays into using Python's interactive mode
51to explore your installation, but that's it. If you're looking for information
52on how to distribute your own Python modules so that others may use them, see
53the :ref:`distutils-index` manual.
54
55
56.. _inst-trivial-install:
57
58Best case: trivial installation
59-------------------------------
60
61In the best case, someone will have prepared a special version of the module
62distribution you want to install that is targeted specifically at your platform
63and is installed just like any other software on your platform. For example,
64the module developer might make an executable installer available for Windows
65users, an RPM package for users of RPM-based Linux systems (Red Hat, SuSE,
66Mandrake, and many others), a Debian package for users of Debian-based Linux
67systems, and so forth.
68
69In that case, you would download the installer appropriate to your platform and
70do the obvious thing with it: run it if it's an executable installer, ``rpm
71--install`` it if it's an RPM, etc. You don't need to run Python or a setup
72script, you don't need to compile anything---you might not even need to read any
Éric Araujo2e1c2942011-07-29 11:57:50 +020073instructions (although it's always a good idea to do so anyway).
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75Of course, things will not always be that easy. You might be interested in a
76module distribution that doesn't have an easy-to-use installer for your
77platform. In that case, you'll have to start with the source distribution
78released by the module's author/maintainer. Installing from a source
79distribution is not too hard, as long as the modules are packaged in the
80standard way. The bulk of this document is about building and installing
81modules from standard source distributions.
82
83
84.. _inst-new-standard:
85
86The new standard: Distutils
87---------------------------
88
89If you download a module source distribution, you can tell pretty quickly if it
90was packaged and distributed in the standard way, i.e. using the Distutils.
91First, the distribution's name and version number will be featured prominently
92in the name of the downloaded archive, e.g. :file:`foo-1.0.tar.gz` or
93:file:`widget-0.9.7.zip`. Next, the archive will unpack into a similarly-named
94directory: :file:`foo-1.0` or :file:`widget-0.9.7`. Additionally, the
95distribution will contain a setup script :file:`setup.py`, and a file named
96:file:`README.txt` or possibly just :file:`README`, which should explain that
Éric Araujoa65a8802011-06-08 01:11:36 +020097building and installing the module distribution is a simple matter of running
98one command from a terminal::
Georg Brandl8ec7f652007-08-15 14:28:01 +000099
100 python setup.py install
101
Éric Araujoa2596a42011-08-19 09:29:56 +0200102For Windows, this command should be run from a command prompt window
103(:menuselection:`Start --> Accessories`)::
Éric Araujoa65a8802011-06-08 01:11:36 +0200104
105 setup.py install
106
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107If all these things are true, then you already know how to build and install the
108modules you've just downloaded: Run the command above. Unless you need to
109install things in a non-standard way or customize the build process, you don't
110really need this manual. Or rather, the above command is everything you need to
111get out of this manual.
112
113
114.. _inst-standard-install:
115
116Standard Build and Install
117==========================
118
119As described in section :ref:`inst-new-standard`, building and installing a module
Éric Araujoa65a8802011-06-08 01:11:36 +0200120distribution using the Distutils is usually one simple command to run from a
121terminal::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122
123 python setup.py install
124
Georg Brandl8ec7f652007-08-15 14:28:01 +0000125
126.. _inst-platform-variations:
127
128Platform variations
129-------------------
130
131You should always run the setup command from the distribution root directory,
132i.e. the top-level subdirectory that the module source distribution unpacks
133into. For example, if you've just downloaded a module source distribution
134:file:`foo-1.0.tar.gz` onto a Unix system, the normal thing to do is::
135
136 gunzip -c foo-1.0.tar.gz | tar xf - # unpacks into directory foo-1.0
137 cd foo-1.0
138 python setup.py install
139
140On Windows, you'd probably download :file:`foo-1.0.zip`. If you downloaded the
141archive file to :file:`C:\\Temp`, then it would unpack into
142:file:`C:\\Temp\\foo-1.0`; you can use either a archive manipulator with a
143graphical user interface (such as WinZip) or a command-line tool (such as
144:program:`unzip` or :program:`pkunzip`) to unpack the archive. Then, open a
Éric Araujoa2596a42011-08-19 09:29:56 +0200145command prompt window and run::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
147 cd c:\Temp\foo-1.0
148 python setup.py install
149
150
151.. _inst-splitting-up:
152
153Splitting the job up
154--------------------
155
156Running ``setup.py install`` builds and installs all modules in one run. If you
157prefer to work incrementally---especially useful if you want to customize the
158build process, or if things are going wrong---you can use the setup script to do
159one thing at a time. This is particularly helpful when the build and install
160will be done by different users---for example, you might want to build a module
161distribution and hand it off to a system administrator for installation (or do
162it yourself, with super-user privileges).
163
164For example, you can build everything in one step, and then install everything
165in a second step, by invoking the setup script twice::
166
167 python setup.py build
168 python setup.py install
169
170If you do this, you will notice that running the :command:`install` command
171first runs the :command:`build` command, which---in this case---quickly notices
172that it has nothing to do, since everything in the :file:`build` directory is
173up-to-date.
174
175You may not need this ability to break things down often if all you do is
176install modules downloaded off the 'net, but it's very handy for more advanced
177tasks. If you get into distributing your own Python modules and extensions,
178you'll run lots of individual Distutils commands on their own.
179
180
181.. _inst-how-build-works:
182
183How building works
184------------------
185
186As implied above, the :command:`build` command is responsible for putting the
187files to install into a *build directory*. By default, this is :file:`build`
188under the distribution root; if you're excessively concerned with speed, or want
189to keep the source tree pristine, you can change the build directory with the
190:option:`--build-base` option. For example::
191
Petri Lehtinen0b785032013-02-23 19:24:08 +0100192 python setup.py build --build-base=/path/to/pybuild/foo-1.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000193
194(Or you could do this permanently with a directive in your system or personal
195Distutils configuration file; see section :ref:`inst-config-files`.) Normally, this
196isn't necessary.
197
198The default layout for the build tree is as follows::
199
200 --- build/ --- lib/
201 or
202 --- build/ --- lib.<plat>/
203 temp.<plat>/
204
205where ``<plat>`` expands to a brief description of the current OS/hardware
206platform and Python version. The first form, with just a :file:`lib` directory,
207is used for "pure module distributions"---that is, module distributions that
208include only pure Python modules. If a module distribution contains any
209extensions (modules written in C/C++), then the second form, with two ``<plat>``
210directories, is used. In that case, the :file:`temp.{plat}` directory holds
211temporary files generated by the compile/link process that don't actually get
212installed. In either case, the :file:`lib` (or :file:`lib.{plat}`) directory
213contains all Python modules (pure Python and extensions) that will be installed.
214
215In the future, more directories will be added to handle Python scripts,
216documentation, binary executables, and whatever else is needed to handle the job
217of installing Python modules and applications.
218
219
220.. _inst-how-install-works:
221
222How installation works
223----------------------
224
225After the :command:`build` command runs (whether you run it explicitly, or the
226:command:`install` command does it for you), the work of the :command:`install`
227command is relatively simple: all it has to do is copy everything under
228:file:`build/lib` (or :file:`build/lib.{plat}`) to your chosen installation
229directory.
230
231If you don't choose an installation directory---i.e., if you just run ``setup.py
232install``\ ---then the :command:`install` command installs to the standard
233location for third-party Python modules. This location varies by platform and
234by how you built/installed Python itself. On Unix (and Mac OS X, which is also
235Unix-based), it also depends on whether the module distribution being installed
236is pure Python or contains extensions ("non-pure"):
237
Georg Brandl44ea77b2013-03-28 13:28:44 +0100238.. tabularcolumns:: |l|l|l|l|
239
Georg Brandl8ec7f652007-08-15 14:28:01 +0000240+-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
241| Platform | Standard installation location | Default value | Notes |
242+=================+=====================================================+==================================================+=======+
243| Unix (pure) | :file:`{prefix}/lib/python{X.Y}/site-packages` | :file:`/usr/local/lib/python{X.Y}/site-packages` | \(1) |
244+-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
245| Unix (non-pure) | :file:`{exec-prefix}/lib/python{X.Y}/site-packages` | :file:`/usr/local/lib/python{X.Y}/site-packages` | \(1) |
246+-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
Brian Curtin1602ec12011-05-03 22:01:53 -0500247| Windows | :file:`{prefix}\\Lib\\site-packages` | :file:`C:\\Python{XY}\\Lib\\site-packages` | \(2) |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000248+-----------------+-----------------------------------------------------+--------------------------------------------------+-------+
249
250Notes:
251
252(1)
253 Most Linux distributions include Python as a standard part of the system, so
254 :file:`{prefix}` and :file:`{exec-prefix}` are usually both :file:`/usr` on
255 Linux. If you build Python yourself on Linux (or any Unix-like system), the
256 default :file:`{prefix}` and :file:`{exec-prefix}` are :file:`/usr/local`.
257
258(2)
259 The default installation directory on Windows was :file:`C:\\Program
260 Files\\Python` under Python 1.6a1, 1.5.2, and earlier.
261
262:file:`{prefix}` and :file:`{exec-prefix}` stand for the directories that Python
263is installed to, and where it finds its libraries at run-time. They are always
264the same under Windows, and very often the same under Unix and Mac OS X. You
265can find out what your Python installation uses for :file:`{prefix}` and
266:file:`{exec-prefix}` by running Python in interactive mode and typing a few
267simple commands. Under Unix, just type ``python`` at the shell prompt. Under
268Windows, choose :menuselection:`Start --> Programs --> Python X.Y -->
269Python (command line)`. Once the interpreter is started, you type Python code
270at the prompt. For example, on my Linux system, I type the three Python
271statements shown below, and get the output as shown, to find out my
272:file:`{prefix}` and :file:`{exec-prefix}`::
273
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000274 Python 2.4 (#26, Aug 7 2004, 17:19:02)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000275 Type "help", "copyright", "credits" or "license" for more information.
276 >>> import sys
277 >>> sys.prefix
278 '/usr'
279 >>> sys.exec_prefix
280 '/usr'
281
Éric Araujoe68d4502011-08-19 08:34:52 +0200282A few other placeholders are used in this document: :file:`{X.Y}` stands for the
283version of Python, for example ``2.7``; :file:`{distname}` will be replaced by
284the name of the module distribution being installed. Dots and capitalization
285are important in the paths; for example, a value that uses ``python2.7`` on UNIX
286will typically use ``Python27`` on Windows.
287
Georg Brandl8ec7f652007-08-15 14:28:01 +0000288If you don't want to install modules to the standard location, or if you don't
289have permission to write there, then you need to read about alternate
290installations in section :ref:`inst-alt-install`. If you want to customize your
291installation directories more heavily, see section :ref:`inst-custom-install` on
292custom installations.
293
294
295.. _inst-alt-install:
296
297Alternate Installation
298======================
299
300Often, it is necessary or desirable to install modules to a location other than
301the standard location for third-party Python modules. For example, on a Unix
302system you might not have permission to write to the standard third-party module
303directory. Or you might wish to try out a module before making it a standard
304part of your local Python installation. This is especially true when upgrading
305a distribution already present: you want to make sure your existing base of
306scripts still works with the new version before actually upgrading.
307
308The Distutils :command:`install` command is designed to make installing module
309distributions to an alternate location simple and painless. The basic idea is
310that you supply a base directory for the installation, and the
311:command:`install` command picks a set of directories (called an *installation
312scheme*) under this base directory in which to install files. The details
313differ across platforms, so read whichever of the following sections applies to
314you.
315
Éric Araujoe68d4502011-08-19 08:34:52 +0200316Note that the various alternate installation schemes are mutually exclusive: you
317can pass ``--user``, or ``--home``, or ``--prefix`` and ``--exec-prefix``, or
318``--install-base`` and ``--install-platbase``, but you can't mix from these
319groups.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000320
Éric Araujoe68d4502011-08-19 08:34:52 +0200321
322.. _inst-alt-install-user:
323
324Alternate installation: the user scheme
325---------------------------------------
326
327This scheme is designed to be the most convenient solution for users that don't
328have write permission to the global site-packages directory or don't want to
329install into it. It is enabled with a simple option::
330
331 python setup.py install --user
332
333Files will be installed into subdirectories of :data:`site.USER_BASE` (written
334as :file:`{userbase}` hereafter). This scheme installs pure Python modules and
335extension modules in the same location (also known as :data:`site.USER_SITE`).
336Here are the values for UNIX, including Mac OS X:
337
338=============== ===========================================================
339Type of file Installation directory
340=============== ===========================================================
341modules :file:`{userbase}/lib/python{X.Y}/site-packages`
342scripts :file:`{userbase}/bin`
343data :file:`{userbase}`
344C headers :file:`{userbase}/include/python{X.Y}/{distname}`
345=============== ===========================================================
346
347And here are the values used on Windows:
348
349=============== ===========================================================
350Type of file Installation directory
351=============== ===========================================================
352modules :file:`{userbase}\\Python{XY}\\site-packages`
353scripts :file:`{userbase}\\Scripts`
354data :file:`{userbase}`
355C headers :file:`{userbase}\\Python{XY}\\Include\\{distname}`
356=============== ===========================================================
357
358The advantage of using this scheme compared to the other ones described below is
359that the user site-packages directory is under normal conditions always included
360in :data:`sys.path` (see :mod:`site` for more information), which means that
361there is no additional step to perform after running the :file:`setup.py` script
362to finalize the installation.
363
364The :command:`build_ext` command also has a ``--user`` option to add
365:file:`{userbase}/include` to the compiler search path for header files and
366:file:`{userbase}/lib` to the compiler search path for libraries as well as to
367the runtime search path for shared C libraries (rpath).
368
369
370.. _inst-alt-install-home:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000371
372Alternate installation: the home scheme
373---------------------------------------
374
375The idea behind the "home scheme" is that you build and maintain a personal
376stash of Python modules. This scheme's name is derived from the idea of a
377"home" directory on Unix, since it's not unusual for a Unix user to make their
378home directory have a layout similar to :file:`/usr/` or :file:`/usr/local/`.
Georg Brandl8891e232010-08-01 21:23:50 +0000379This scheme can be used by anyone, regardless of the operating system they
380are installing for.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000381
382Installing a new module distribution is as simple as ::
383
384 python setup.py install --home=<dir>
385
386where you can supply any directory you like for the :option:`--home` option. On
387Unix, lazy typists can just type a tilde (``~``); the :command:`install` command
388will expand this to your home directory::
389
390 python setup.py install --home=~
391
Éric Araujoe68d4502011-08-19 08:34:52 +0200392To make Python find the distributions installed with this scheme, you may have
393to :ref:`modify Python's search path <inst-search-path>` or edit
394:mod:`sitecustomize` (see :mod:`site`) to call :func:`site.addsitedir` or edit
395:data:`sys.path`.
396
Georg Brandl8ec7f652007-08-15 14:28:01 +0000397The :option:`--home` option defines the installation base directory. Files are
398installed to the following directories under the installation base as follows:
399
Éric Araujoe68d4502011-08-19 08:34:52 +0200400=============== ===========================================================
401Type of file Installation directory
402=============== ===========================================================
403modules :file:`{home}/lib/python`
404scripts :file:`{home}/bin`
405data :file:`{home}`
406C headers :file:`{home}/include/python/{distname}`
407=============== ===========================================================
408
409(Mentally replace slashes with backslashes if you're on Windows.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000410
411.. versionchanged:: 2.4
412 The :option:`--home` option used to be supported only on Unix.
413
414
Éric Araujoe68d4502011-08-19 08:34:52 +0200415.. _inst-alt-install-prefix-unix:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000416
417Alternate installation: Unix (the prefix scheme)
418------------------------------------------------
419
420The "prefix scheme" is useful when you wish to use one Python installation to
421perform the build/install (i.e., to run the setup script), but install modules
422into the third-party module directory of a different Python installation (or
423something that looks like a different Python installation). If this sounds a
Éric Araujoe68d4502011-08-19 08:34:52 +0200424trifle unusual, it is---that's why the user and home schemes come before. However,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000425there are at least two known cases where the prefix scheme will be useful.
426
427First, consider that many Linux distributions put Python in :file:`/usr`, rather
428than the more traditional :file:`/usr/local`. This is entirely appropriate,
429since in those cases Python is part of "the system" rather than a local add-on.
430However, if you are installing Python modules from source, you probably want
431them to go in :file:`/usr/local/lib/python2.{X}` rather than
432:file:`/usr/lib/python2.{X}`. This can be done with ::
433
434 /usr/bin/python setup.py install --prefix=/usr/local
435
436Another possibility is a network filesystem where the name used to write to a
437remote directory is different from the name used to read it: for example, the
438Python interpreter accessed as :file:`/usr/local/bin/python` might search for
439modules in :file:`/usr/local/lib/python2.{X}`, but those modules would have to
440be installed to, say, :file:`/mnt/{@server}/export/lib/python2.{X}`. This could
441be done with ::
442
443 /usr/local/bin/python setup.py install --prefix=/mnt/@server/export
444
445In either case, the :option:`--prefix` option defines the installation base, and
446the :option:`--exec-prefix` option defines the platform-specific installation
447base, which is used for platform-specific files. (Currently, this just means
448non-pure module distributions, but could be expanded to C libraries, binary
449executables, etc.) If :option:`--exec-prefix` is not supplied, it defaults to
450:option:`--prefix`. Files are installed as follows:
451
Éric Araujoe68d4502011-08-19 08:34:52 +0200452================= ==========================================================
453Type of file Installation directory
454================= ==========================================================
455Python modules :file:`{prefix}/lib/python{X.Y}/site-packages`
456extension modules :file:`{exec-prefix}/lib/python{X.Y}/site-packages`
457scripts :file:`{prefix}/bin`
458data :file:`{prefix}`
459C headers :file:`{prefix}/include/python{X.Y}/{distname}`
460================= ==========================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000461
462There is no requirement that :option:`--prefix` or :option:`--exec-prefix`
463actually point to an alternate Python installation; if the directories listed
464above do not already exist, they are created at installation time.
465
466Incidentally, the real reason the prefix scheme is important is simply that a
467standard Unix installation uses the prefix scheme, but with :option:`--prefix`
468and :option:`--exec-prefix` supplied by Python itself as ``sys.prefix`` and
469``sys.exec_prefix``. Thus, you might think you'll never use the prefix scheme,
470but every time you run ``python setup.py install`` without any other options,
471you're using it.
472
473Note that installing extensions to an alternate Python installation has no
474effect on how those extensions are built: in particular, the Python header files
475(:file:`Python.h` and friends) installed with the Python interpreter used to run
476the setup script will be used in compiling extensions. It is your
477responsibility to ensure that the interpreter used to run extensions installed
478in this way is compatible with the interpreter used to build them. The best way
479to do this is to ensure that the two interpreters are the same version of Python
480(possibly different builds, or possibly copies of the same build). (Of course,
481if your :option:`--prefix` and :option:`--exec-prefix` don't even point to an
482alternate Python installation, this is immaterial.)
483
484
Éric Araujoe68d4502011-08-19 08:34:52 +0200485.. _inst-alt-install-prefix-windows:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000486
487Alternate installation: Windows (the prefix scheme)
488---------------------------------------------------
489
490Windows has no concept of a user's home directory, and since the standard Python
491installation under Windows is simpler than under Unix, the :option:`--prefix`
492option has traditionally been used to install additional packages in separate
493locations on Windows. ::
494
495 python setup.py install --prefix="\Temp\Python"
496
497to install modules to the :file:`\\Temp\\Python` directory on the current drive.
498
499The installation base is defined by the :option:`--prefix` option; the
Éric Araujoe68d4502011-08-19 08:34:52 +0200500:option:`--exec-prefix` option is not supported under Windows, which means that
501pure Python modules and extension modules are installed into the same location.
502Files are installed as follows:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000503
Éric Araujoe68d4502011-08-19 08:34:52 +0200504=============== ==========================================================
505Type of file Installation directory
506=============== ==========================================================
507modules :file:`{prefix}\\Lib\\site-packages`
508scripts :file:`{prefix}\\Scripts`
509data :file:`{prefix}`
510C headers :file:`{prefix}\\Include\\{distname}`
511=============== ==========================================================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000512
513
514.. _inst-custom-install:
515
516Custom Installation
517===================
518
519Sometimes, the alternate installation schemes described in section
520:ref:`inst-alt-install` just don't do what you want. You might want to tweak just
521one or two directories while keeping everything under the same base directory,
522or you might want to completely redefine the installation scheme. In either
523case, you're creating a *custom installation scheme*.
524
Éric Araujoe68d4502011-08-19 08:34:52 +0200525To create a custom installation scheme, you start with one of the alternate
526schemes and override some of the installation directories used for the various
527types of files, using these options:
528
529====================== =======================
530Type of file Override option
531====================== =======================
532Python modules ``--install-purelib``
533extension modules ``--install-platlib``
534all modules ``--install-lib``
535scripts ``--install-scripts``
536data ``--install-data``
537C headers ``--install-headers``
538====================== =======================
539
540These override options can be relative, absolute,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000541or explicitly defined in terms of one of the installation base directories.
542(There are two installation base directories, and they are normally the same---
543they only differ when you use the Unix "prefix scheme" and supply different
Éric Araujoe68d4502011-08-19 08:34:52 +0200544``--prefix`` and ``--exec-prefix`` options; using ``--install-lib`` will
545override values computed or given for ``--install-purelib`` and
546``--install-platlib``, and is recommended for schemes that don't make a
547difference between Python and extension modules.)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000548
549For example, say you're installing a module distribution to your home directory
550under Unix---but you want scripts to go in :file:`~/scripts` rather than
551:file:`~/bin`. As you might expect, you can override this directory with the
552:option:`--install-scripts` option; in this case, it makes most sense to supply
553a relative path, which will be interpreted relative to the installation base
554directory (your home directory, in this case)::
555
556 python setup.py install --home=~ --install-scripts=scripts
557
558Another Unix example: suppose your Python installation was built and installed
559with a prefix of :file:`/usr/local/python`, so under a standard installation
560scripts will wind up in :file:`/usr/local/python/bin`. If you want them in
561:file:`/usr/local/bin` instead, you would supply this absolute directory for the
562:option:`--install-scripts` option::
563
564 python setup.py install --install-scripts=/usr/local/bin
565
566(This performs an installation using the "prefix scheme," where the prefix is
567whatever your Python interpreter was installed with--- :file:`/usr/local/python`
568in this case.)
569
570If you maintain Python on Windows, you might want third-party modules to live in
571a subdirectory of :file:`{prefix}`, rather than right in :file:`{prefix}`
572itself. This is almost as easy as customizing the script installation directory
573---you just have to remember that there are two types of modules to worry about,
Éric Araujoe68d4502011-08-19 08:34:52 +0200574Python and extension modules, which can conveniently be both controlled by one
575option::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000576
Éric Araujoe68d4502011-08-19 08:34:52 +0200577 python setup.py install --install-lib=Site
Georg Brandl8ec7f652007-08-15 14:28:01 +0000578
Éric Araujoe68d4502011-08-19 08:34:52 +0200579The specified installation directory is relative to :file:`{prefix}`. Of
580course, you also have to ensure that this directory is in Python's module
581search path, such as by putting a :file:`.pth` file in a site directory (see
582:mod:`site`). See section :ref:`inst-search-path` to find out how to modify
583Python's search path.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000584
585If you want to define an entire installation scheme, you just have to supply all
586of the installation directory options. The recommended way to do this is to
587supply relative paths; for example, if you want to maintain all Python
588module-related files under :file:`python` in your home directory, and you want a
589separate directory for each platform that you use your home directory from, you
590might define the following installation scheme::
591
592 python setup.py install --home=~ \
593 --install-purelib=python/lib \
594 --install-platlib=python/lib.$PLAT \
595 --install-scripts=python/scripts
596 --install-data=python/data
597
Georg Brandlb19be572007-12-29 10:57:00 +0000598or, equivalently, ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000599
600 python setup.py install --home=~/python \
601 --install-purelib=lib \
602 --install-platlib='lib.$PLAT' \
603 --install-scripts=scripts
604 --install-data=data
605
606``$PLAT`` is not (necessarily) an environment variable---it will be expanded by
607the Distutils as it parses your command line options, just as it does when
608parsing your configuration file(s).
609
Georg Brandl8ec7f652007-08-15 14:28:01 +0000610Obviously, specifying the entire installation scheme every time you install a
611new module distribution would be very tedious. Thus, you can put these options
612into your Distutils config file (see section :ref:`inst-config-files`)::
613
614 [install]
615 install-base=$HOME
616 install-purelib=python/lib
617 install-platlib=python/lib.$PLAT
618 install-scripts=python/scripts
619 install-data=python/data
620
621or, equivalently, ::
622
623 [install]
624 install-base=$HOME/python
625 install-purelib=lib
626 install-platlib=lib.$PLAT
627 install-scripts=scripts
628 install-data=data
629
630Note that these two are *not* equivalent if you supply a different installation
631base directory when you run the setup script. For example, ::
632
633 python setup.py install --install-base=/tmp
634
Éric Araujoe68d4502011-08-19 08:34:52 +0200635would install pure modules to :file:`/tmp/python/lib` in the first case, and
636to :file:`/tmp/lib` in the second case. (For the second case, you probably
Georg Brandl8ec7f652007-08-15 14:28:01 +0000637want to supply an installation base of :file:`/tmp/python`.)
638
639You probably noticed the use of ``$HOME`` and ``$PLAT`` in the sample
640configuration file input. These are Distutils configuration variables, which
641bear a strong resemblance to environment variables. In fact, you can use
642environment variables in config files on platforms that have such a notion but
643the Distutils additionally define a few extra variables that may not be in your
644environment, such as ``$PLAT``. (And of course, on systems that don't have
645environment variables, such as Mac OS 9, the configuration variables supplied by
646the Distutils are the only ones you can use.) See section :ref:`inst-config-files`
647for details.
648
Georg Brandlb19be572007-12-29 10:57:00 +0000649.. XXX need some Windows examples---when would custom installation schemes be
650 needed on those platforms?
Georg Brandl8ec7f652007-08-15 14:28:01 +0000651
Georg Brandl8ec7f652007-08-15 14:28:01 +0000652
Éric Araujoe68d4502011-08-19 08:34:52 +0200653.. XXX Move this to Doc/using
Georg Brandl8ec7f652007-08-15 14:28:01 +0000654
655.. _inst-search-path:
656
657Modifying Python's Search Path
658------------------------------
659
660When the Python interpreter executes an :keyword:`import` statement, it searches
661for both Python code and extension modules along a search path. A default value
662for the path is configured into the Python binary when the interpreter is built.
663You can determine the path by importing the :mod:`sys` module and printing the
664value of ``sys.path``. ::
665
666 $ python
667 Python 2.2 (#11, Oct 3 2002, 13:31:27)
668 [GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
Georg Brandlfc29f272009-01-02 20:25:14 +0000669 Type "help", "copyright", "credits" or "license" for more information.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000670 >>> import sys
671 >>> sys.path
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000672 ['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
673 '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
Georg Brandl8ec7f652007-08-15 14:28:01 +0000674 '/usr/local/lib/python2.3/site-packages']
675 >>>
676
677The null string in ``sys.path`` represents the current working directory.
678
Georg Brandl8ec7f652007-08-15 14:28:01 +0000679The expected convention for locally installed packages is to put them in the
680:file:`{...}/site-packages/` directory, but you may want to install Python
681modules into some arbitrary directory. For example, your site may have a
682convention of keeping all software related to the web server under :file:`/www`.
683Add-on Python modules might then belong in :file:`/www/python`, and in order to
684import them, this directory must be added to ``sys.path``. There are several
685different ways to add the directory.
686
687The most convenient way is to add a path configuration file to a directory
688that's already on Python's path, usually to the :file:`.../site-packages/`
689directory. Path configuration files have an extension of :file:`.pth`, and each
690line must contain a single path that will be appended to ``sys.path``. (Because
691the new paths are appended to ``sys.path``, modules in the added directories
692will not override standard modules. This means you can't use this mechanism for
693installing fixed versions of standard modules.)
694
695Paths can be absolute or relative, in which case they're relative to the
Brett Cannon5cf449c2007-11-17 07:07:29 +0000696directory containing the :file:`.pth` file. See the documentation of
Georg Brandlb3c572b2007-08-24 17:46:54 +0000697the :mod:`site` module for more information.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000698
699A slightly less convenient way is to edit the :file:`site.py` file in Python's
700standard library, and modify ``sys.path``. :file:`site.py` is automatically
701imported when the Python interpreter is executed, unless the :option:`-S` switch
702is supplied to suppress this behaviour. So you could simply edit
703:file:`site.py` and add two lines to it::
704
705 import sys
706 sys.path.append('/www/python/')
707
708However, if you reinstall the same major version of Python (perhaps when
709upgrading from 2.2 to 2.2.2, for example) :file:`site.py` will be overwritten by
710the stock version. You'd have to remember that it was modified and save a copy
711before doing the installation.
712
713There are two environment variables that can modify ``sys.path``.
714:envvar:`PYTHONHOME` sets an alternate value for the prefix of the Python
715installation. For example, if :envvar:`PYTHONHOME` is set to ``/www/python``,
716the search path will be set to ``['', '/www/python/lib/pythonX.Y/',
717'/www/python/lib/pythonX.Y/plat-linux2', ...]``.
718
719The :envvar:`PYTHONPATH` variable can be set to a list of paths that will be
720added to the beginning of ``sys.path``. For example, if :envvar:`PYTHONPATH` is
721set to ``/www/python:/opt/py``, the search path will begin with
722``['/www/python', '/opt/py']``. (Note that directories must exist in order to
723be added to ``sys.path``; the :mod:`site` module removes paths that don't
724exist.)
725
726Finally, ``sys.path`` is just a regular Python list, so any Python application
727can modify it by adding or removing entries.
728
729
730.. _inst-config-files:
731
732Distutils Configuration Files
733=============================
734
735As mentioned above, you can use Distutils configuration files to record personal
736or site preferences for any Distutils options. That is, any option to any
737command can be stored in one of two or three (depending on your platform)
738configuration files, which will be consulted before the command-line is parsed.
739This means that configuration files will override default values, and the
740command-line will in turn override configuration files. Furthermore, if
741multiple configuration files apply, values from "earlier" files are overridden
742by "later" files.
743
744
745.. _inst-config-filenames:
746
747Location and names of config files
748----------------------------------
749
750The names and locations of the configuration files vary slightly across
751platforms. On Unix and Mac OS X, the three configuration files (in the order
752they are processed) are:
753
754+--------------+----------------------------------------------------------+-------+
755| Type of file | Location and filename | Notes |
756+==============+==========================================================+=======+
757| system | :file:`{prefix}/lib/python{ver}/distutils/distutils.cfg` | \(1) |
758+--------------+----------------------------------------------------------+-------+
759| personal | :file:`$HOME/.pydistutils.cfg` | \(2) |
760+--------------+----------------------------------------------------------+-------+
761| local | :file:`setup.cfg` | \(3) |
762+--------------+----------------------------------------------------------+-------+
763
764And on Windows, the configuration files are:
765
766+--------------+-------------------------------------------------+-------+
767| Type of file | Location and filename | Notes |
768+==============+=================================================+=======+
769| system | :file:`{prefix}\\Lib\\distutils\\distutils.cfg` | \(4) |
770+--------------+-------------------------------------------------+-------+
771| personal | :file:`%HOME%\\pydistutils.cfg` | \(5) |
772+--------------+-------------------------------------------------+-------+
773| local | :file:`setup.cfg` | \(3) |
774+--------------+-------------------------------------------------+-------+
775
Tarek Ziadé40b998b2009-10-27 23:06:10 +0000776On all platforms, the "personal" file can be temporarily disabled by
777passing the `--no-user-cfg` option.
778
Georg Brandl8ec7f652007-08-15 14:28:01 +0000779Notes:
780
781(1)
782 Strictly speaking, the system-wide configuration file lives in the directory
783 where the Distutils are installed; under Python 1.6 and later on Unix, this is
784 as shown. For Python 1.5.2, the Distutils will normally be installed to
785 :file:`{prefix}/lib/python1.5/site-packages/distutils`, so the system
786 configuration file should be put there under Python 1.5.2.
787
788(2)
789 On Unix, if the :envvar:`HOME` environment variable is not defined, the user's
790 home directory will be determined with the :func:`getpwuid` function from the
Tarek Ziadéf7216662009-08-21 14:11:26 +0000791 standard :mod:`pwd` module. This is done by the :func:`os.path.expanduser`
792 function used by Distutils.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000793
794(3)
795 I.e., in the current directory (usually the location of the setup script).
796
797(4)
798 (See also note (1).) Under Python 1.6 and later, Python's default "installation
799 prefix" is :file:`C:\\Python`, so the system configuration file is normally
800 :file:`C:\\Python\\Lib\\distutils\\distutils.cfg`. Under Python 1.5.2, the
801 default prefix was :file:`C:\\Program Files\\Python`, and the Distutils were not
802 part of the standard library---so the system configuration file would be
803 :file:`C:\\Program Files\\Python\\distutils\\distutils.cfg` in a standard Python
804 1.5.2 installation under Windows.
805
806(5)
Tarek Ziadéf7216662009-08-21 14:11:26 +0000807 On Windows, if the :envvar:`HOME` environment variable is not defined,
808 :envvar:`USERPROFILE` then :envvar:`HOMEDRIVE` and :envvar:`HOMEPATH` will
809 be tried. This is done by the :func:`os.path.expanduser` function used
810 by Distutils.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000811
812
813.. _inst-config-syntax:
814
815Syntax of config files
816----------------------
817
818The Distutils configuration files all have the same syntax. The config files
819are grouped into sections. There is one section for each Distutils command,
820plus a ``global`` section for global options that affect every command. Each
821section consists of one option per line, specified as ``option=value``.
822
823For example, the following is a complete config file that just forces all
824commands to run quietly by default::
825
826 [global]
827 verbose=0
828
829If this is installed as the system config file, it will affect all processing of
830any Python module distribution by any user on the current system. If it is
831installed as your personal config file (on systems that support them), it will
832affect only module distributions processed by you. And if it is used as the
833:file:`setup.cfg` for a particular module distribution, it affects only that
834distribution.
835
836You could override the default "build base" directory and make the
837:command:`build\*` commands always forcibly rebuild all files with the
838following::
839
840 [build]
841 build-base=blib
842 force=1
843
844which corresponds to the command-line arguments ::
845
846 python setup.py build --build-base=blib --force
847
848except that including the :command:`build` command on the command-line means
849that command will be run. Including a particular command in config files has no
850such implication; it only means that if the command is run, the options in the
851config file will apply. (Or if other commands that derive values from it are
852run, they will use the values in the config file.)
853
854You can find out the complete list of options for any command using the
855:option:`--help` option, e.g.::
856
857 python setup.py build --help
858
859and you can find out the complete list of global options by using
860:option:`--help` without a command::
861
862 python setup.py --help
863
864See also the "Reference" section of the "Distributing Python Modules" manual.
865
866
867.. _inst-building-ext:
868
869Building Extensions: Tips and Tricks
870====================================
871
872Whenever possible, the Distutils try to use the configuration information made
873available by the Python interpreter used to run the :file:`setup.py` script.
874For example, the same compiler and linker flags used to compile Python will also
875be used for compiling extensions. Usually this will work well, but in
876complicated situations this might be inappropriate. This section discusses how
877to override the usual Distutils behaviour.
878
879
880.. _inst-tweak-flags:
881
882Tweaking compiler/linker flags
883------------------------------
884
885Compiling a Python extension written in C or C++ will sometimes require
886specifying custom flags for the compiler and linker in order to use a particular
887library or produce a special kind of object code. This is especially true if the
888extension hasn't been tested on your platform, or if you're trying to
889cross-compile Python.
890
891In the most general case, the extension author might have foreseen that
892compiling the extensions would be complicated, and provided a :file:`Setup` file
893for you to edit. This will likely only be done if the module distribution
894contains many separate extension modules, or if they often require elaborate
895sets of compiler flags in order to work.
896
897A :file:`Setup` file, if present, is parsed in order to get a list of extensions
898to build. Each line in a :file:`Setup` describes a single module. Lines have
899the following structure::
900
901 module ... [sourcefile ...] [cpparg ...] [library ...]
902
903
904Let's examine each of the fields in turn.
905
906* *module* is the name of the extension module to be built, and should be a
907 valid Python identifier. You can't just change this in order to rename a module
908 (edits to the source code would also be needed), so this should be left alone.
909
910* *sourcefile* is anything that's likely to be a source code file, at least
911 judging by the filename. Filenames ending in :file:`.c` are assumed to be
912 written in C, filenames ending in :file:`.C`, :file:`.cc`, and :file:`.c++` are
913 assumed to be C++, and filenames ending in :file:`.m` or :file:`.mm` are assumed
914 to be in Objective C.
915
916* *cpparg* is an argument for the C preprocessor, and is anything starting with
917 :option:`-I`, :option:`-D`, :option:`-U` or :option:`-C`.
918
919* *library* is anything ending in :file:`.a` or beginning with :option:`-l` or
920 :option:`-L`.
921
922If a particular platform requires a special library on your platform, you can
923add it by editing the :file:`Setup` file and running ``python setup.py build``.
924For example, if the module defined by the line ::
925
926 foo foomodule.c
927
928must be linked with the math library :file:`libm.a` on your platform, simply add
929:option:`-lm` to the line::
930
931 foo foomodule.c -lm
932
933Arbitrary switches intended for the compiler or the linker can be supplied with
934the :option:`-Xcompiler` *arg* and :option:`-Xlinker` *arg* options::
935
936 foo foomodule.c -Xcompiler -o32 -Xlinker -shared -lm
937
938The next option after :option:`-Xcompiler` and :option:`-Xlinker` will be
939appended to the proper command line, so in the above example the compiler will
940be passed the :option:`-o32` option, and the linker will be passed
941:option:`-shared`. If a compiler option requires an argument, you'll have to
942supply multiple :option:`-Xcompiler` options; for example, to pass ``-x c++``
943the :file:`Setup` file would have to contain ``-Xcompiler -x -Xcompiler c++``.
944
945Compiler flags can also be supplied through setting the :envvar:`CFLAGS`
946environment variable. If set, the contents of :envvar:`CFLAGS` will be added to
947the compiler flags specified in the :file:`Setup` file.
948
949
950.. _inst-non-ms-compilers:
951
952Using non-Microsoft compilers on Windows
953----------------------------------------
954
955.. sectionauthor:: Rene Liebscher <R.Liebscher@gmx.de>
956
957
958
Georg Brandl02677812008-03-15 00:20:19 +0000959Borland/CodeGear C++
960^^^^^^^^^^^^^^^^^^^^
Georg Brandl8ec7f652007-08-15 14:28:01 +0000961
Georg Brandl02677812008-03-15 00:20:19 +0000962This subsection describes the necessary steps to use Distutils with the Borland
Georg Brandl8ec7f652007-08-15 14:28:01 +0000963C++ compiler version 5.5. First you have to know that Borland's object file
964format (OMF) is different from the format used by the Python version you can
965download from the Python or ActiveState Web site. (Python is built with
966Microsoft Visual C++, which uses COFF as the object file format.) For this
967reason you have to convert Python's library :file:`python25.lib` into the
968Borland format. You can do this as follows:
969
Georg Brandlb19be572007-12-29 10:57:00 +0000970.. Should we mention that users have to create cfg-files for the compiler?
971.. see also http://community.borland.com/article/0,1410,21205,00.html
Georg Brandl8ec7f652007-08-15 14:28:01 +0000972
973::
974
975 coff2omf python25.lib python25_bcpp.lib
976
977The :file:`coff2omf` program comes with the Borland compiler. The file
978:file:`python25.lib` is in the :file:`Libs` directory of your Python
979installation. If your extension uses other libraries (zlib, ...) you have to
980convert them too.
981
982The converted files have to reside in the same directories as the normal
983libraries.
984
985How does Distutils manage to use these libraries with their changed names? If
986the extension needs a library (eg. :file:`foo`) Distutils checks first if it
987finds a library with suffix :file:`_bcpp` (eg. :file:`foo_bcpp.lib`) and then
988uses this library. In the case it doesn't find such a special library it uses
989the default name (:file:`foo.lib`.) [#]_
990
991To let Distutils compile your extension with Borland C++ you now have to type::
992
993 python setup.py build --compiler=bcpp
994
995If you want to use the Borland C++ compiler as the default, you could specify
996this in your personal or system-wide configuration file for Distutils (see
997section :ref:`inst-config-files`.)
998
999
1000.. seealso::
1001
Georg Brandl02677812008-03-15 00:20:19 +00001002 `C++Builder Compiler <http://www.codegear.com/downloads/free/cppbuilder>`_
Georg Brandl8ec7f652007-08-15 14:28:01 +00001003 Information about the free C++ compiler from Borland, including links to the
1004 download pages.
1005
1006 `Creating Python Extensions Using Borland's Free Compiler <http://www.cyberus.ca/~g_will/pyExtenDL.shtml>`_
1007 Document describing how to use Borland's free command-line C++ compiler to build
1008 Python.
1009
1010
1011GNU C / Cygwin / MinGW
1012^^^^^^^^^^^^^^^^^^^^^^
1013
Georg Brandl8ec7f652007-08-15 14:28:01 +00001014This section describes the necessary steps to use Distutils with the GNU C/C++
1015compilers in their Cygwin and MinGW distributions. [#]_ For a Python interpreter
1016that was built with Cygwin, everything should work without any of these
1017following steps.
1018
Éric Araujobe98fab2010-12-15 22:20:15 +00001019Not all extensions can be built with MinGW or Cygwin, but many can. Extensions
1020most likely to not work are those that use C++ or depend on Microsoft Visual C
1021extensions.
1022
1023To let Distutils compile your extension with Cygwin you have to type::
1024
1025 python setup.py build --compiler=cygwin
1026
1027and for Cygwin in no-cygwin mode [#]_ or for MinGW type::
1028
1029 python setup.py build --compiler=mingw32
1030
1031If you want to use any of these options/compilers as default, you should
1032consider writing it in your personal or system-wide configuration file for
1033Distutils (see section :ref:`inst-config-files`.)
1034
1035Older Versions of Python and MinGW
1036""""""""""""""""""""""""""""""""""
1037The following instructions only apply if you're using a version of Python
1038inferior to 2.4.1 with a MinGW inferior to 3.0.0 (with
1039binutils-2.13.90-20030111-1).
1040
1041These compilers require some special libraries. This task is more complex than
Georg Brandl8ec7f652007-08-15 14:28:01 +00001042for Borland's C++, because there is no program to convert the library. First
1043you have to create a list of symbols which the Python DLL exports. (You can find
Georg Brandla4314c22009-10-11 20:16:16 +00001044a good program for this task at
Georg Brandle0f6d592013-10-06 13:20:49 +02001045http://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/).
Georg Brandl8ec7f652007-08-15 14:28:01 +00001046
Georg Brandlb19be572007-12-29 10:57:00 +00001047.. I don't understand what the next line means. --amk
1048.. (inclusive the references on data structures.)
Georg Brandl8ec7f652007-08-15 14:28:01 +00001049
1050::
1051
1052 pexports python25.dll >python25.def
1053
1054The location of an installed :file:`python25.dll` will depend on the
1055installation options and the version and language of Windows. In a "just for
1056me" installation, it will appear in the root of the installation directory. In
1057a shared installation, it will be located in the system directory.
1058
1059Then you can create from these information an import library for gcc. ::
1060
1061 /cygwin/bin/dlltool --dllname python25.dll --def python25.def --output-lib libpython25.a
1062
1063The resulting library has to be placed in the same directory as
1064:file:`python25.lib`. (Should be the :file:`libs` directory under your Python
1065installation directory.)
1066
1067If your extension uses other libraries (zlib,...) you might have to convert
1068them too. The converted files have to reside in the same directories as the
1069normal libraries do.
1070
Georg Brandl8ec7f652007-08-15 14:28:01 +00001071
1072.. seealso::
1073
1074 `Building Python modules on MS Windows platform with MinGW <http://www.zope.org/Members/als/tips/win32_mingw_modules>`_
1075 Information about building the required libraries for the MinGW environment.
1076
Georg Brandl8ec7f652007-08-15 14:28:01 +00001077
1078.. rubric:: Footnotes
1079
1080.. [#] This also means you could replace all existing COFF-libraries with OMF-libraries
1081 of the same name.
1082
1083.. [#] Check http://sources.redhat.com/cygwin/ and http://www.mingw.org/ for more
1084 information
1085
1086.. [#] Then you have no POSIX emulation available, but you also don't need
1087 :file:`cygwin1.dll`.