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