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