Installing Python Modules (Legacy version)¶
- Author:
Greg Ward
Note
The entiredistutils package has been deprecated and will beremoved in Python 3.12. This documentation is retained as areference only, and will be removed with the package. See theWhat’s New entry for more information.
See also
- Installing Python Modules
The up to date module installation documentation. For regular Pythonusage, you almost certainly want that document rather than this one.
Note
This document is being retained solely until thesetuptools documentationathttps://setuptools.readthedocs.io/en/latest/setuptools.htmlindependently covers all of the relevant information currently included here.
Note
This guide only covers the basic tools for building and distributingextensions that are provided as part of this version of Python. Third partytools offer easier to use and more secure alternatives. Refer to thequickrecommendations sectionin the Python Packaging User Guide for more information.
Introduction¶
In Python 2.0, thedistutils API was first added to the standard library.This provided Linux distro maintainers with a standard way of convertingPython projects into Linux distro packages, and system administrators with astandard way of installing them directly onto target systems.
In the many years since Python 2.0 was released, tightly coupling the buildsystem and package installer to the language runtime release cycle has turnedout to be problematic, and it is now recommended that projects use thepip package installer and thesetuptools build system, rather thanusingdistutils directly.
SeeInstalling Python Modules andDistributing Python Modules for more details.
This legacy documentation is being retained only until we’re confident that thesetuptools documentation covers everything needed.
Distutils based source distributions¶
If you download a module source distribution, you can tell pretty quickly if itwas packaged and distributed in the standard way, i.e. using the Distutils.First, the distribution’s name and version number will be featured prominentlyin the name of the downloaded archive, e.g.foo-1.0.tar.gz orwidget-0.9.7.zip. Next, the archive will unpack into a similarly nameddirectory:foo-1.0 orwidget-0.9.7. Additionally, thedistribution will contain a setup scriptsetup.py, and a file namedREADME.txt or possibly justREADME, which should explain thatbuilding and installing the module distribution is a simple matter of runningone command from a terminal:
python setup.py install
For Windows, this command should be run from a command prompt window(Start ‣ Accessories):
setup.py install
If all these things are true, then you already know how to build and install themodules you’ve just downloaded: Run the command above. Unless you need toinstall things in a non-standard way or customize the build process, you don’treally need this manual. Or rather, the above command is everything you need toget out of this manual.
Standard Build and Install¶
As described in sectionDistutils based source distributions, building and installing a moduledistribution using the Distutils is usually one simple command to run from aterminal:
python setup.py install
Platform variations¶
You should always run the setup command from the distribution root directory,i.e. the top-level subdirectory that the module source distribution unpacksinto. For example, if you’ve just downloaded a module source distributionfoo-1.0.tar.gz onto a Unix system, the normal thing to do is:
gunzip -c foo-1.0.tar.gz | tar xf - # unpacks into directory foo-1.0cd foo-1.0python setup.py install
On Windows, you’d probably downloadfoo-1.0.zip. If you downloaded thearchive file toC:\Temp, then it would unpack intoC:\Temp\foo-1.0; you can use either an archive manipulator with agraphical user interface (such as WinZip) or a command-line tool (such asunzip orpkunzip) to unpack the archive. Then, open acommand prompt window and run:
cd c:\Temp\foo-1.0python setup.py install
Splitting the job up¶
Runningsetup.pyinstall builds and installs all modules in one run. If youprefer to work incrementally—especially useful if you want to customize thebuild process, or if things are going wrong—you can use the setup script to doone thing at a time. This is particularly helpful when the build and installwill be done by different users—for example, you might want to build a moduledistribution and hand it off to a system administrator for installation (or doit yourself, with super-user privileges).
For example, you can build everything in one step, and then install everythingin a second step, by invoking the setup script twice:
python setup.py buildpython setup.py install
If you do this, you will notice that running theinstall commandfirst runs thebuild command, which—in this case—quickly noticesthat it has nothing to do, since everything in thebuild directory isup-to-date.
You may not need this ability to break things down often if all you do isinstall modules downloaded off the ‘net, but it’s very handy for more advancedtasks. If you get into distributing your own Python modules and extensions,you’ll run lots of individual Distutils commands on their own.
How building works¶
As implied above, thebuild command is responsible for putting thefiles to install into abuild directory. By default, this isbuildunder the distribution root; if you’re excessively concerned with speed, or wantto keep the source tree pristine, you can change the build directory with the--build-base option. For example:
python setup.py build --build-base=/path/to/pybuild/foo-1.0
(Or you could do this permanently with a directive in your system or personalDistutils configuration file; see sectionDistutils Configuration Files.) Normally, thisisn’t necessary.
The default layout for the build tree is as follows:
--- build/ --- lib/or--- build/ --- lib.<plat>/ temp.<plat>/
where<plat> expands to a brief description of the current OS/hardwareplatform and Python version. The first form, with just alib directory,is used for “pure module distributions”—that is, module distributions thatinclude only pure Python modules. If a module distribution contains anyextensions (modules written in C/C++), then the second form, with two<plat>directories, is used. In that case, thetemp.plat directory holdstemporary files generated by the compile/link process that don’t actually getinstalled. In either case, thelib (orlib.plat) directorycontains all Python modules (pure Python and extensions) that will be installed.
In the future, more directories will be added to handle Python scripts,documentation, binary executables, and whatever else is needed to handle the jobof installing Python modules and applications.
How installation works¶
After thebuild command runs (whether you run it explicitly, or theinstall command does it for you), the work of theinstallcommand is relatively simple: all it has to do is copy everything underbuild/lib (orbuild/lib.plat) to your chosen installationdirectory.
If you don’t choose an installation directory—i.e., if you just runsetup.pyinstall—then theinstall command installs to the standardlocation for third-party Python modules. This location varies by platform andby how you built/installed Python itself. On Unix (and macOS, which is alsoUnix-based), it also depends on whether the module distribution being installedis pure Python or contains extensions (“non-pure”):
Platform | Standard installation location | Default value | Notes |
|---|---|---|---|
Unix (pure) |
|
| (1) |
Unix (non-pure) |
|
| (1) |
Windows |
|
| (2) |
Notes:
Most Linux distributions include Python as a standard part of the system, so
prefixandexec-prefixare usually both/usronLinux. If you build Python yourself on Linux (or any Unix-like system), thedefaultprefixandexec-prefixare/usr/local.The default installation directory on Windows was
C:\ProgramFiles\Pythonunder Python 1.6a1, 1.5.2, and earlier.
prefix andexec-prefix stand for the directories that Pythonis installed to, and where it finds its libraries at run-time. They are alwaysthe same under Windows, and very often the same under Unix and macOS. Youcan find out what your Python installation uses forprefix andexec-prefix by running Python in interactive mode and typing a fewsimple commands. Under Unix, just typepython at the shell prompt. UnderWindows, chooseStart ‣ Programs ‣ Python X.Y ‣Python (command line). Once the interpreter is started, you type Python codeat the prompt. For example, on my Linux system, I type the three Pythonstatements shown below, and get the output as shown, to find out myprefix andexec-prefix:
Python 2.4 (#26, Aug 7 2004, 17:19:02)Type "help", "copyright", "credits" or "license" for more information.>>>importsys>>>sys.prefix'/usr'>>>sys.exec_prefix'/usr'
A few other placeholders are used in this document:X.Y stands for theversion of Python, for example3.2;abiflags will be replaced bythe value ofsys.abiflags or the empty string for platforms which don’tdefine ABI flags;distname will be replaced by the name of the moduledistribution being installed. Dots and capitalization are important in thepaths; for example, a value that usespython3.2 on UNIX will typically usePython32 on Windows.
If you don’t want to install modules to the standard location, or if you don’thave permission to write there, then you need to read about alternateinstallations in sectionAlternate Installation. If you want to customize yourinstallation directories more heavily, see sectionCustom Installation oncustom installations.
Alternate Installation¶
Often, it is necessary or desirable to install modules to a location other thanthe standard location for third-party Python modules. For example, on a Unixsystem you might not have permission to write to the standard third-party moduledirectory. Or you might wish to try out a module before making it a standardpart of your local Python installation. This is especially true when upgradinga distribution already present: you want to make sure your existing base ofscripts still works with the new version before actually upgrading.
The Distutilsinstall command is designed to make installing moduledistributions to an alternate location simple and painless. The basic idea isthat you supply a base directory for the installation, and theinstall command picks a set of directories (called aninstallationscheme) under this base directory in which to install files. The detailsdiffer across platforms, so read whichever of the following sections applies toyou.
Note that the various alternate installation schemes are mutually exclusive: youcan pass--user, or--home, or--prefix and--exec-prefix, or--install-base and--install-platbase, but you can’t mix from thesegroups.
Alternate installation: the user scheme¶
This scheme is designed to be the most convenient solution for users that don’thave write permission to the global site-packages directory or don’t want toinstall into it. It is enabled with a simple option:
python setup.py install --user
Files will be installed into subdirectories ofsite.USER_BASE (writtenasuserbase hereafter). This scheme installs pure Python modules andextension modules in the same location (also known assite.USER_SITE).Here are the values for UNIX, including macOS:
Type of file | Installation directory |
|---|---|
modules |
|
scripts |
|
data |
|
C headers |
|
And here are the values used on Windows:
Type of file | Installation directory |
|---|---|
modules |
|
scripts |
|
data |
|
C headers |
|
The advantage of using this scheme compared to the other ones described below isthat the user site-packages directory is under normal conditions always includedinsys.path (seesite for more information), which means thatthere is no additional step to perform after running thesetup.py scriptto finalize the installation.
Thebuild_ext command also has a--user option to adduserbase/include to the compiler search path for header files anduserbase/lib to the compiler search path for libraries as well as tothe runtime search path for shared C libraries (rpath).
Alternate installation: the home scheme¶
The idea behind the “home scheme” is that you build and maintain a personalstash of Python modules. This scheme’s name is derived from the idea of a“home” directory on Unix, since it’s not unusual for a Unix user to make theirhome directory have a layout similar to/usr/ or/usr/local/.This scheme can be used by anyone, regardless of the operating system theyare installing for.
Installing a new module distribution is as simple as
python setup.py install --home=<dir>
where you can supply any directory you like for the--home option. OnUnix, lazy typists can just type a tilde (~); theinstall commandwill expand this to your home directory:
python setup.py install --home=~
To make Python find the distributions installed with this scheme, you may havetomodify Python’s search path or editsitecustomize (seesite) to callsite.addsitedir() or editsys.path.
The--home option defines the installation base directory. Files areinstalled to the following directories under the installation base as follows:
Type of file | Installation directory |
|---|---|
modules |
|
scripts |
|
data |
|
C headers |
|
(Mentally replace slashes with backslashes if you’re on Windows.)
Alternate installation: Unix (the prefix scheme)¶
The “prefix scheme” is useful when you wish to use one Python installation toperform the build/install (i.e., to run the setup script), but install modulesinto the third-party module directory of a different Python installation (orsomething that looks like a different Python installation). If this sounds atrifle unusual, it is—that’s why the user and home schemes come before. However,there are at least two known cases where the prefix scheme will be useful.
First, consider that many Linux distributions put Python in/usr, ratherthan the more traditional/usr/local. This is entirely appropriate,since in those cases Python is part of “the system” rather than a local add-on.However, if you are installing Python modules from source, you probably wantthem to go in/usr/local/lib/python2.X rather than/usr/lib/python2.X. This can be done with
/usr/bin/python setup.py install --prefix=/usr/local
Another possibility is a network filesystem where the name used to write to aremote directory is different from the name used to read it: for example, thePython interpreter accessed as/usr/local/bin/python might search formodules in/usr/local/lib/python2.X, but those modules would have tobe installed to, say,/mnt/@server/export/lib/python2.X. This couldbe done with
/usr/local/bin/python setup.py install --prefix=/mnt/@server/export
In either case, the--prefix option defines the installation base, andthe--exec-prefix option defines the platform-specific installationbase, which is used for platform-specific files. (Currently, this just meansnon-pure module distributions, but could be expanded to C libraries, binaryexecutables, etc.) If--exec-prefix is not supplied, it defaults to--prefix. Files are installed as follows:
Type of file | Installation directory |
|---|---|
Python modules |
|
extension modules |
|
scripts |
|
data |
|
C headers |
|
There is no requirement that--prefix or--exec-prefixactually point to an alternate Python installation; if the directories listedabove do not already exist, they are created at installation time.
Incidentally, the real reason the prefix scheme is important is simply that astandard Unix installation uses the prefix scheme, but with--prefixand--exec-prefix supplied by Python itself assys.prefix andsys.exec_prefix. Thus, you might think you’ll never use the prefix scheme,but every time you runpythonsetup.pyinstall without any other options,you’re using it.
Note that installing extensions to an alternate Python installation has noeffect on how those extensions are built: in particular, the Python header files(Python.h and friends) installed with the Python interpreter used to runthe setup script will be used in compiling extensions. It is yourresponsibility to ensure that the interpreter used to run extensions installedin this way is compatible with the interpreter used to build them. The best wayto do this is to ensure that the two interpreters are the same version of Python(possibly different builds, or possibly copies of the same build). (Of course,if your--prefix and--exec-prefix don’t even point to analternate Python installation, this is immaterial.)
Alternate installation: Windows (the prefix scheme)¶
Windows has no concept of a user’s home directory, and since the standard Pythoninstallation under Windows is simpler than under Unix, the--prefixoption has traditionally been used to install additional packages in separatelocations on Windows.
python setup.py install --prefix="\Temp\Python"
to install modules to the\Temp\Python directory on the current drive.
The installation base is defined by the--prefix option; the--exec-prefix option is not supported under Windows, which means thatpure Python modules and extension modules are installed into the same location.Files are installed as follows:
Type of file | Installation directory |
|---|---|
modules |
|
scripts |
|
data |
|
C headers |
|
Custom Installation¶
Sometimes, the alternate installation schemes described in sectionAlternate Installation just don’t do what you want. You might want to tweak justone or two directories while keeping everything under the same base directory,or you might want to completely redefine the installation scheme. In eithercase, you’re creating acustom installation scheme.
To create a custom installation scheme, you start with one of the alternateschemes and override some of the installation directories used for the varioustypes of files, using these options:
Type of file | Override option |
|---|---|
Python modules |
|
extension modules |
|
all modules |
|
scripts |
|
data |
|
C headers |
|
These override options can be relative, absolute,or explicitly defined in terms of one of the installation base directories.(There are two installation base directories, and they are normally thesame—they only differ when you use the Unix “prefix scheme” and supplydifferent--prefix and--exec-prefix options; using--install-libwill override values computed or given for--install-purelib and--install-platlib, and is recommended for schemes that don’t make adifference between Python and extension modules.)
For example, say you’re installing a module distribution to your home directoryunder Unix—but you want scripts to go in~/scripts rather than~/bin. As you might expect, you can override this directory with the--install-scripts option; in this case, it makes most sense to supplya relative path, which will be interpreted relative to the installation basedirectory (your home directory, in this case):
python setup.py install --home=~ --install-scripts=scripts
Another Unix example: suppose your Python installation was built and installedwith a prefix of/usr/local/python, so under a standard installationscripts will wind up in/usr/local/python/bin. If you want them in/usr/local/bin instead, you would supply this absolute directory for the--install-scripts option:
python setup.py install --install-scripts=/usr/local/bin
(This performs an installation using the “prefix scheme”, where the prefix iswhatever your Python interpreter was installed with—/usr/local/pythonin this case.)
If you maintain Python on Windows, you might want third-party modules to live ina subdirectory ofprefix, rather than right inprefixitself. This is almost as easy as customizing the script installationdirectory—you just have to remember that there are two types of modulesto worry about, Python and extension modules, which can conveniently be bothcontrolled by one option:
python setup.py install --install-lib=Site
The specified installation directory is relative toprefix. Ofcourse, you also have to ensure that this directory is in Python’s modulesearch path, such as by putting a.pth file in a site directory (seesite). See sectionModifying Python’s Search Path to find out how to modifyPython’s search path.
If you want to define an entire installation scheme, you just have to supply allof the installation directory options. The recommended way to do this is tosupply relative paths; for example, if you want to maintain all Pythonmodule-related files underpython in your home directory, and you want aseparate directory for each platform that you use your home directory from, youmight define the following installation scheme:
python setup.py install --home=~ \ --install-purelib=python/lib \ --install-platlib=python/lib.$PLAT \ --install-scripts=python/scripts --install-data=python/data
or, equivalently,
python setup.py install --home=~/python \ --install-purelib=lib \ --install-platlib='lib.$PLAT' \ --install-scripts=scripts --install-data=data
$PLAT is not (necessarily) an environment variable—it will be expanded bythe Distutils as it parses your command line options, just as it does whenparsing your configuration file(s).
Obviously, specifying the entire installation scheme every time you install anew module distribution would be very tedious. Thus, you can put these optionsinto your Distutils config file (see sectionDistutils Configuration Files):
[install]install-base=$HOMEinstall-purelib=python/libinstall-platlib=python/lib.$PLATinstall-scripts=python/scriptsinstall-data=python/data
or, equivalently,
[install]install-base=$HOME/pythoninstall-purelib=libinstall-platlib=lib.$PLATinstall-scripts=scriptsinstall-data=data
Note that these two arenot equivalent if you supply a different installationbase directory when you run the setup script. For example,
python setup.py install --install-base=/tmp
would install pure modules to/tmp/python/lib in the first case, andto/tmp/lib in the second case. (For the second case, you probablywant to supply an installation base of/tmp/python.)
You probably noticed the use of$HOME and$PLAT in the sampleconfiguration file input. These are Distutils configuration variables, whichbear a strong resemblance to environment variables. In fact, you can useenvironment variables in config files on platforms that have such a notion butthe Distutils additionally define a few extra variables that may not be in yourenvironment, such as$PLAT. (And of course, on systems that don’t haveenvironment variables, such as Mac OS 9, the configuration variables supplied bythe Distutils are the only ones you can use.) See sectionDistutils Configuration Filesfor details.
Note
When avirtual environment is activated, any optionsthat change the installation path will be ignored from all distutils configurationfiles to prevent inadvertently installing projects outside of the virtualenvironment.
Modifying Python’s Search Path¶
When the Python interpreter executes animport statement, it searchesfor both Python code and extension modules along a search path. A default valuefor the path is configured into the Python binary when the interpreter is built.You can determine the path by importing thesys module and printing thevalue ofsys.path.
$ pythonPython 2.2 (#11, Oct 3 2002, 13:31:27)[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.path['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2', '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload', '/usr/local/lib/python2.3/site-packages']>>>
The null string insys.path represents the current working directory.
The expected convention for locally installed packages is to put them in the.../site-packages/ directory, but you may want to install Pythonmodules into some arbitrary directory. For example, your site may have aconvention of keeping all software related to the web server under/www.Add-on Python modules might then belong in/www/python, and in order toimport them, this directory must be added tosys.path. There are severaldifferent ways to add the directory.
The most convenient way is to add a path configuration file to a directorythat’s already on Python’s path, usually to the.../site-packages/directory. Path configuration files have an extension of.pth, and eachline must contain a single path that will be appended tosys.path. (Becausethe new paths are appended tosys.path, modules in the added directorieswill not override standard modules. This means you can’t use this mechanism forinstalling fixed versions of standard modules.)
Paths can be absolute or relative, in which case they’re relative to thedirectory containing the.pth file. See the documentation ofthesite module for more information.
A slightly less convenient way is to edit thesite.py file in Python’sstandard library, and modifysys.path.site.py is automaticallyimported when the Python interpreter is executed, unless the-S switchis supplied to suppress this behaviour. So you could simply editsite.py and add two lines to it:
importsyssys.path.append('/www/python/')
However, if you reinstall the same minor version of Python (perhaps whenupgrading from 2.2 to 2.2.2, for example)site.py will be overwritten bythe stock version. You’d have to remember that it was modified and save a copybefore doing the installation.
There are two environment variables that can modifysys.path.PYTHONHOME sets an alternate value for the prefix of the Pythoninstallation. For example, ifPYTHONHOME is set to/www/python,the search path will be set to['','/www/python/lib/pythonX.Y/','/www/python/lib/pythonX.Y/plat-linux2',...].
ThePYTHONPATH variable can be set to a list of paths that will beadded to the beginning ofsys.path. For example, ifPYTHONPATH isset to/www/python:/opt/py, the search path will begin with['/www/python','/opt/py']. (Note that directories must exist in order tobe added tosys.path; thesite module removes paths that don’texist.)
Finally,sys.path is just a regular Python list, so any Python applicationcan modify it by adding or removing entries.
Distutils Configuration Files¶
As mentioned above, you can use Distutils configuration files to record personalor site preferences for any Distutils options. That is, any option to anycommand can be stored in one of two or three (depending on your platform)configuration files, which will be consulted before the command-line is parsed.This means that configuration files will override default values, and thecommand-line will in turn override configuration files. Furthermore, ifmultiple configuration files apply, values from “earlier” files are overriddenby “later” files.
Location and names of config files¶
The names and locations of the configuration files vary slightly acrossplatforms. On Unix and macOS, the three configuration files (in the orderthey are processed) are:
Type of file | Location and filename | Notes |
|---|---|---|
system |
| (1) |
personal |
| (2) |
local |
| (3) |
And on Windows, the configuration files are:
Type of file | Location and filename | Notes |
|---|---|---|
system |
| (4) |
personal |
| (5) |
local |
| (3) |
On all platforms, the “personal” file can be temporarily disabled bypassing the--no-user-cfg option.
Notes:
Strictly speaking, the system-wide configuration file lives in the directorywhere the Distutils are installed; under Python 1.6 and later on Unix, this isas shown. For Python 1.5.2, the Distutils will normally be installed to
prefix/lib/python1.5/site-packages/distutils, so the systemconfiguration file should be put there under Python 1.5.2.On Unix, if the
HOMEenvironment variable is not defined, the user’shome directory will be determined with thegetpwuid()function from thestandardpwdmodule. This is done by theos.path.expanduser()function used by Distutils.I.e., in the current directory (usually the location of the setup script).
(See also note (1).) Under Python 1.6 and later, Python’s default “installationprefix” is
C:\Python, so the system configuration file is normallyC:\Python\Lib\distutils\distutils.cfg. Under Python 1.5.2, thedefault prefix wasC:\ProgramFiles\Python, and the Distutils were notpart of the standard library—so the system configuration file would beC:\ProgramFiles\Python\distutils\distutils.cfgin a standard Python1.5.2 installation under Windows.On Windows, if the
HOMEenvironment variable is not defined,USERPROFILEthenHOMEDRIVEandHOMEPATHwillbe tried. This is done by theos.path.expanduser()function usedby Distutils.
Syntax of config files¶
The Distutils configuration files all have the same syntax. The config filesare grouped into sections. There is one section for each Distutils command,plus aglobal section for global options that affect every command. Eachsection consists of one option per line, specified asoption=value.
For example, the following is a complete config file that just forces allcommands to run quietly by default:
[global]verbose=0
If this is installed as the system config file, it will affect all processing ofany Python module distribution by any user on the current system. If it isinstalled as your personal config file (on systems that support them), it willaffect only module distributions processed by you. And if it is used as thesetup.cfg for a particular module distribution, it affects only thatdistribution.
You could override the default “build base” directory and make thebuild* commands always forcibly rebuild all files with thefollowing:
[build]build-base=blibforce=1
which corresponds to the command-line arguments
python setup.py build --build-base=blib --force
except that including thebuild command on the command-line meansthat command will be run. Including a particular command in config files has nosuch implication; it only means that if the command is run, the options in theconfig file will apply. (Or if other commands that derive values from it arerun, they will use the values in the config file.)
You can find out the complete list of options for any command using the--help option, e.g.:
python setup.py build --help
and you can find out the complete list of global options by using--help without a command:
python setup.py --help
See also the “Reference” section of the “Distributing Python Modules” manual.
Building Extensions: Tips and Tricks¶
Whenever possible, the Distutils try to use the configuration information madeavailable by the Python interpreter used to run thesetup.py script.For example, the same compiler and linker flags used to compile Python will alsobe used for compiling extensions. Usually this will work well, but incomplicated situations this might be inappropriate. This section discusses howto override the usual Distutils behaviour.
Tweaking compiler/linker flags¶
Compiling a Python extension written in C or C++ will sometimes requirespecifying custom flags for the compiler and linker in order to use a particularlibrary or produce a special kind of object code. This is especially true if theextension hasn’t been tested on your platform, or if you’re trying tocross-compile Python.
In the most general case, the extension author might have foreseen thatcompiling the extensions would be complicated, and provided aSetup filefor you to edit. This will likely only be done if the module distributioncontains many separate extension modules, or if they often require elaboratesets of compiler flags in order to work.
ASetup file, if present, is parsed in order to get a list of extensionsto build. Each line in aSetup describes a single module. Lines havethe following structure:
module ... [sourcefile ...] [cpparg ...] [library ...]
Let’s examine each of the fields in turn.
module is the name of the extension module to be built, and should be avalid Python identifier. You can’t just change this in order to rename a module(edits to the source code would also be needed), so this should be left alone.
sourcefile is anything that’s likely to be a source code file, at leastjudging by the filename. Filenames ending in
.care assumed to bewritten in C, filenames ending in.C,.cc, and.c++areassumed to be C++, and filenames ending in.mor.mmare assumedto be in Objective C.cpparg is an argument for the C preprocessor, and is anything starting with
-I,-D,-Uor-C.library is anything ending in
.aor beginning with-lor-L.
If a particular platform requires a special library on your platform, you canadd it by editing theSetup file and runningpythonsetup.pybuild.For example, if the module defined by the line
foo foomodule.c
must be linked with the math librarylibm.a on your platform, simply add-lm to the line:
foo foomodule.c -lm
Arbitrary switches intended for the compiler or the linker can be supplied withthe-Xcompilerarg and-Xlinkerarg options:
foo foomodule.c -Xcompiler -o32 -Xlinker -shared -lm
The next option after-Xcompiler and-Xlinker will beappended to the proper command line, so in the above example the compiler willbe passed the-o32 option, and the linker will be passed-shared. If a compiler option requires an argument, you’ll have tosupply multiple-Xcompiler options; for example, to pass-xc++theSetup file would have to contain-Xcompiler-x-Xcompilerc++.
Compiler flags can also be supplied through setting theCFLAGSenvironment variable. If set, the contents ofCFLAGS will be added tothe compiler flags specified in theSetup file.
Using non-Microsoft compilers on Windows¶
Borland/CodeGear C++¶
This subsection describes the necessary steps to use Distutils with the BorlandC++ compiler version 5.5. First you have to know that Borland’s object fileformat (OMF) is different from the format used by the Python version you candownload from the Python or ActiveState web site. (Python is built withMicrosoft Visual C++, which uses COFF as the object file format.) For thisreason you have to convert Python’s librarypython25.lib into theBorland format. You can do this as follows:
coff2omf python25.lib python25_bcpp.lib
Thecoff2omf program comes with the Borland compiler. The filepython25.lib is in theLibs directory of your Pythoninstallation. If your extension uses other libraries (zlib, …) you have toconvert them too.
The converted files have to reside in the same directories as the normallibraries.
How does Distutils manage to use these libraries with their changed names? Ifthe extension needs a library (eg.foo) Distutils checks first if itfinds a library with suffix_bcpp (eg.foo_bcpp.lib) and thenuses this library. In the case it doesn’t find such a special library it usesthe default name (foo.lib.)[1]
To let Distutils compile your extension with Borland C++ you now have to type:
python setup.py build --compiler=bcpp
If you want to use the Borland C++ compiler as the default, you could specifythis in your personal or system-wide configuration file for Distutils (seesectionDistutils Configuration Files.)
See also
- C++Builder Compiler
Information about the free C++ compiler from Borland, including links to thedownload pages.
- Creating Python Extensions Using Borland’s Free Compiler
Document describing how to use Borland’s free command-line C++ compiler to buildPython.
GNU C / Cygwin / MinGW¶
This section describes the necessary steps to use Distutils with the GNU C/C++compilers in their Cygwin and MinGW distributions.[2] For a Python interpreterthat was built with Cygwin, everything should work without any of thesefollowing steps.
Not all extensions can be built with MinGW or Cygwin, but many can. Extensionsmost likely to not work are those that use C++ or depend on Microsoft Visual Cextensions.
To let Distutils compile your extension with Cygwin you have to type:
python setup.py build --compiler=cygwin
and for Cygwin in no-cygwin mode[3] or for MinGW type:
python setup.py build --compiler=mingw32
If you want to use any of these options/compilers as default, you shouldconsider writing it in your personal or system-wide configuration file forDistutils (see sectionDistutils Configuration Files.)
Older Versions of Python and MinGW¶
The following instructions only apply if you’re using a version of Pythoninferior to 2.4.1 with a MinGW inferior to 3.0.0 (withbinutils-2.13.90-20030111-1).
These compilers require some special libraries. This task is more complex thanfor Borland’s C++, because there is no program to convert the library. Firstyou have to create a list of symbols which the Python DLL exports. (You can finda good program for this task athttps://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/).
pexports python25.dll >python25.def
The location of an installedpython25.dll will depend on theinstallation options and the version and language of Windows. In a “just forme” installation, it will appear in the root of the installation directory. Ina shared installation, it will be located in the system directory.
Then you can create from these information an import library for gcc.
/cygwin/bin/dlltool --dllname python25.dll --def python25.def --output-lib libpython25.a
The resulting library has to be placed in the same directory aspython25.lib. (Should be thelibs directory under your Pythoninstallation directory.)
If your extension uses other libraries (zlib,…) you might have to convertthem too. The converted files have to reside in the same directories as thenormal libraries do.
See also
- Building Python modules on MS Windows platform with MinGW
Information about building the required libraries for the MinGW environment.
Footnotes
[1]This also means you could replace all existing COFF-libraries with OMF-librariesof the same name.
[2]Checkhttps://www.sourceware.org/cygwin/ for more information
[3]Then you have no POSIX emulation available, but you also don’t needcygwin1.dll.