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