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