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