Install Python Imagin Library Mac

Author
  1. Jul 21, 2016  hello, I am really struggling to install dlib for python3.5 and boost1.60 on mac os x. I can install dlib for python 2.7 and i can install dlib using anaconda to get dlib 18.18 with python3.5 and boost 1.59. But i can't install dlib with.
  2. Aug 03, 2011  Python Imaging Library. Contribute to whatupdave/pil development by creating an account on GitHub. This source kit has been built and tested with Python 2.0 and newer, on Windows, Mac OS X, and major Unix platforms. Large parts of the library also work on 1.5.2 and 1.6. You're ready to install the library: $ python setup.py install.
Mac

Update: These instructions are over a year old, though they may still work for you. See the 'Install Python' page for the most up-to-date instructions. I've already written a post about installing Python, NumPy, SciPy, and matplotlib on Lion, but it involves a lot of working at the command line, modifying your.bashprofile and dealing with. To install the latest version of python 2, simply type: $ brew install python If you'd rather install the latest version of python 3, replace python with python3. There are a few package managers that are specific to Python, and pip is the preferred one.

Greg Ward

See also

Installing Python Modules

The up to date module installation documentations

This document describes the Python Distribution Utilities (“Distutils”) from theend-user’s point-of-view, describing how to extend the capabilities of astandard Python installation by building and installing third-party Pythonmodules and extensions.

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 the quickrecommendations sectionin the Python Packaging User Guide for more information.

Introduction¶

Although Python’s extensive standard library covers many programming needs,there often comes a time when you need to add some new functionality to yourPython installation in the form of third-party modules. This might be necessaryto support your own programming, or to support an application that you want touse and that happens to be written in Python.

In the past, there has been little support for adding third-party modules to anexisting Python installation. With the introduction of the Python DistributionUtilities (Distutils for short) in Python 2.0, this changed.

This document is aimed primarily at the people who need to install third-partyPython modules: end-users and system administrators who just need to get somePython application running, and existing Python programmers who want to add somenew goodies to their toolbox. You don’t need to know Python to read thisdocument; there will be some brief forays into using Python’s interactive modeto explore your installation, but that’s it. If you’re looking for informationon how to distribute your own Python modules so that others may use them, seethe Distributing Python Modules (Legacy version) manual. Debugging the setup script may also be ofinterest.

Best case: trivial installation¶

In the best case, someone will have prepared a special version of the moduledistribution you want to install that is targeted specifically at your platformand is installed just like any other software on your platform. For example,the module developer might make an executable installer available for Windowsusers, an RPM package for users of RPM-based Linux systems (Red Hat, SuSE,Mandrake, and many others), a Debian package for users of Debian-based Linuxsystems, and so forth.

In that case, you would download the installer appropriate to your platform anddo the obvious thing with it: run it if it’s an executable installer, rpm--install it if it’s an RPM, etc. You don’t need to run Python or a setupscript, you don’t need to compile anything—you might not even need to read anyinstructions (although it’s always a good idea to do so anyway).

Of course, things will not always be that easy. You might be interested in amodule distribution that doesn’t have an easy-to-use installer for yourplatform. In that case, you’ll have to start with the source distributionreleased by the module’s author/maintainer. Installing from a sourcedistribution is not too hard, as long as the modules are packaged in thestandard way. The bulk of this document is about building and installingmodules from standard source distributions.

The new standard: Distutils¶

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 or widget-0.9.7. Additionally, thedistribution will contain a setup script setup.py, and a file namedREADME.txt or possibly just README, which should explain thatbuilding and installing the module distribution is a simple matter of runningone command from a terminal:

For Windows, this command should be run from a command prompt window(Start ‣ Accessories):

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 section The new standard: Distutils, building and installing a moduledistribution using the Distutils is usually one simple command to run from aterminal:

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:

On Windows, you’d probably download foo-1.0.zip. If you downloaded thearchive file to C:Temp, then it would unpack intoC:Tempfoo-1.0; you can use either an archive manipulator with agraphical user interface (such as WinZip) or a command-line tool (such asunzip or pkunzip) to unpack the archive. Then, open acommand prompt window and run:

Splitting the job up¶

Running setup.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:

If you do this, you will notice that running the install commandfirst runs the build command, which—in this case—quickly noticesthat it has nothing to do, since everything in the build 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, the build command is responsible for putting thefiles to install into a build directory. By default, this is buildunder 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:

(Or you could do this permanently with a directive in your system or personalDistutils configuration file; see section Distutils Configuration Files.) Normally, thisisn’t necessary.

The default layout for the build tree is as follows:

where <plat> expands to a brief description of the current OS/hardwareplatform and Python version. The first form, with just a lib 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, the temp.plat directory holdstemporary files generated by the compile/link process that don’t actually getinstalled. In either case, the lib (or lib.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 the build command runs (whether you run it explicitly, or theinstall command does it for you), the work of the installcommand is relatively simple: all it has to do is copy everything underbuild/lib (or build/lib.plat) to your chosen installationdirectory.

If you don’t choose an installation directory—i.e., if you just run setup.pyinstall—then the install 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 Mac OS X, 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)

prefix/lib/pythonX.Y/site-packages

/usr/local/lib/pythonX.Y/site-packages

(1)

Unix (non-pure)

exec-prefix/lib/pythonX.Y/site-packages

/usr/local/lib/pythonX.Y/site-packages

(1)

Windows

prefixLibsite-packages

C:PythonXYLibsite-packages

(2)

Notes:

  1. Most Linux distributions include Python as a standard part of the system, soprefix and exec-prefix are usually both /usr onLinux. If you build Python yourself on Linux (or any Unix-like system), thedefault prefix and exec-prefix are /usr/local.

  2. The default installation directory on Windows was C:ProgramFilesPython under Python 1.6a1, 1.5.2, and earlier.

prefix and exec-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 Mac OS X. Youcan find out what your Python installation uses for prefix andexec-prefix by running Python in interactive mode and typing a fewsimple commands. Under Unix, just type python at the shell prompt. UnderWindows, choose Start ‣ 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 and exec-prefix:

A few other placeholders are used in this document: X.Y stands for theversion of Python, for example 2.7; distname will be replaced bythe name of the module distribution being installed. Dots and capitalizationare important in the paths; for example, a value that uses python2.7 on UNIXwill typically use Python27 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 section Alternate Installation. If you want to customize yourinstallation directories more heavily, see section Custom 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 Distutils install 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 an installationscheme) 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:

Files will be installed into subdirectories of site.USER_BASE (writtenas userbase hereafter). This scheme installs pure Python modules andextension modules in the same location (also known as site.USER_SITE).Here are the values for UNIX, including Mac OS X:

Type of file

Installation directory

modules

userbase/lib/pythonX.Y/site-packages

scripts

userbase/bin

data

userbase

C headers

userbase/include/pythonX.Y/distname

And here are the values used on Windows:

Type of file

Installation directory

modules

userbasePythonXYsite-packages

scripts

userbaseScripts

data

userbase

C headers

userbasePythonXYInclude{distname}

The advantage of using this scheme compared to the other ones described below isthat the user site-packages directory is under normal conditions always includedin sys.path (see site for more information), which means thatthere is no additional step to perform after running the setup.py scriptto finalize the installation.

The build_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

where you can supply any directory you like for the --home option. OnUnix, lazy typists can just type a tilde (~); the install commandwill expand this to your home directory:

To make Python find the distributions installed with this scheme, you may haveto modify Python’s search path or editsitecustomize (see site) to call site.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

home/lib/python

scripts

home/bin

data

home

C headers

home/include/python/distname

(Mentally replace slashes with backslashes if you’re on Windows.)

Changed in version 2.4: The --home option used to be supported only on Unix.

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

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

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

prefix/lib/pythonX.Y/site-packages

extension modules

exec-prefix/lib/pythonX.Y/site-packages

scripts

prefix/bin

data

prefix

C headers

prefix/include/pythonX.Y/distname

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 as sys.prefix andsys.exec_prefix. Thus, you might think you’ll never use the prefix scheme,but every time you run pythonsetup.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.

to install modules to the TempPython 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

prefixLibsite-packages

scripts

prefixScripts

data

prefix

C headers

prefixInclude{distname}

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 a custom 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

--install-purelib

extension modules

--install-platlib

all modules

--install-lib

scripts

--install-scripts

data

--install-data

C headers

--install-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 the same—they only differ when you use the Unix “prefix scheme” and supply different--prefix and --exec-prefix options; using --install-lib willoverride 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):

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:

(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 of prefix, rather than right in prefixitself. This is almost as easy as customizing the script installation directory—you just have to remember that there are two types of modules to worry about,Python and extension modules, which can conveniently be both controlled by oneoption:

The specified installation directory is relative to prefix. 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 section Modifying 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 under python 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:

or, equivalently,

$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 section Distutils Configuration Files):

or, equivalently,

Note that these two are not equivalent if you supply a different installationbase directory when you run the setup script. For example,

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 section Distutils Configuration Filesfor details.

Install Python Imagine Library Mac Os

Modifying Python’s Search Path¶

When the Python interpreter executes an import 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 the sys module and printing thevalue of sys.path.

The null string in sys.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 to sys.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 to sys.path. (Becausethe new paths are appended to sys.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 ofthe site module for more information.

A slightly less convenient way is to edit the site.py file in Python’sstandard library, and modify sys.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:

However, if you reinstall the same major 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 modify sys.path.PYTHONHOME sets an alternate value for the prefix of the Pythoninstallation. For example, if PYTHONHOME is set to /www/python,the search path will be set to [','/www/python/lib/pythonX.Y/','/www/python/lib/pythonX.Y/plat-linux2',...].

The PYTHONPATH variable can be set to a list of paths that will beadded to the beginning of sys.path. For example, if PYTHONPATH 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 to sys.path; the site 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 Mac OS X, the three configuration files (in the orderthey are processed) are:

Type of file

Location and filename

Notes

system

prefix/lib/pythonver/distutils/distutils.cfg

(1)

personal

$HOME/.pydistutils.cfg

(2)

local

setup.cfg

(3)

And on Windows, the configuration files are:

Type of file

Location and filename

Notes

system

prefixLibdistutilsdistutils.cfg

(4)

personal

%HOME%pydistutils.cfg

(5)

local

setup.cfg

(3)

On all platforms, the “personal” file can be temporarily disabled bypassing the –no-user-cfg option.

Notes:

  1. 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 toprefix/lib/python1.5/site-packages/distutils, so the systemconfiguration file should be put there under Python 1.5.2.

  2. On Unix, if the HOME environment variable is not defined, the user’shome directory will be determined with the getpwuid() function from thestandard pwd module. This is done by the os.path.expanduser()function used by Distutils.

  3. I.e., in the current directory (usually the location of the setup script).

  4. (See also note (1).) Under Python 1.6 and later, Python’s default “installationprefix” is C:Python, so the system configuration file is normallyC:PythonLibdistutilsdistutils.cfg. Under Python 1.5.2, thedefault prefix was C:ProgramFilesPython, and the Distutils were notpart of the standard library—so the system configuration file would beC:ProgramFilesPythondistutilsdistutils.cfg in a standard Python1.5.2 installation under Windows.

  5. On Windows, if the HOME environment variable is not defined,USERPROFILE then HOMEDRIVE and HOMEPATH willbe tried. This is done by the os.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 a global section for global options that affect every command. Eachsection consists of one option per line, specified as option=value.

For example, the following is a complete config file that just forces allcommands to run quietly by default:

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:

which corresponds to the command-line arguments

except that including the build 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.:

and you can find out the complete list of global options by using--help without a command:

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 the setup.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 a Setup 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.

A Setup file, if present, is parsed in order to get a list of extensionsto build. Each line in a Setup describes a single module. Lines havethe following structure:

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 .c are assumed to bewritten in C, filenames ending in .C, .cc, and .c++ areassumed to be C++, and filenames ending in .m or .mm are assumedto be in Objective C.

  • cpparg is an argument for the C preprocessor, and is anything starting with-I, -D, -U or -C.

  • library is anything ending in .a or beginning with -l or-L.

If a particular platform requires a special library on your platform, you canadd it by editing the Setup file and running pythonsetup.pybuild.For example, if the module defined by the line

must be linked with the math library libm.a on your platform, simply add-lm to the line:

Arbitrary switches intended for the compiler or the linker can be supplied withthe -Xcompilerarg and -Xlinkerarg options:

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++the Setup file would have to contain -Xcompiler-x-Xcompilerc++.

Compiler flags can also be supplied through setting the CFLAGSenvironment variable. If set, the contents of CFLAGS will be added tothe compiler flags specified in the Setup 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 library python25.lib into theBorland format. You can do this as follows:

Install Python Imagine Library Machine

The coff2omf program comes with the Borland compiler. The filepython25.lib is in the Libs 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:

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 (seesection Distutils 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:

and for Cygwin in no-cygwin mode 3 or for MinGW type:

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 section Distutils 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).

Install Python Imaging Library Mac

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/).

The location of an installed python25.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.

The resulting library has to be placed in the same directory aspython25.lib. (Should be the libs 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.

Install Python Imagine Library Mac Download

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

Check https://www.sourceware.org/cygwin/ and http://www.mingw.org/ for moreinformation

3

Install Python Imagine Library Mac Torrent

Then you have no POSIX emulation available, but you also don’t needcygwin1.dll.