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