1112 lines · 44.6 KB · TeX
RawDownload
\documentclass{howto}\usepackage{distutils} % TODO:%   Fill in XXX comments \title{Installing Python Modules} % The audience for this document includes people who don't know anything % about Python and aren't about to learn the language just in order to% install and maintain it for their users, i.e. system administrators.% Thus, I have to be sure to explain the basics at some point:% sys.path and PYTHONPATH at least.  Should probably give pointers to% other docs on "import site", PYTHONSTARTUP, PYTHONHOME, etc.% % Finally, it might be useful to include all the material from my "Care% and Feeding of a Python Installation" talk in here somewhere.  Yow! \input{boilerplate} \author{Greg Ward}\authoraddress{	\strong{Python Software Foundation}\\	Email: \email{distutils-sig@python.org}} \makeindex \begin{document} \maketitle \begin{abstract}  \noindent  This document describes the Python Distribution Utilities  (``Distutils'') from the end-user's point-of-view, describing how to  extend the capabilities of a standard Python installation by building  and installing third-party Python modules and extensions.\end{abstract} %\begin{abstract}%\noindent%Abstract this!%\end{abstract}  % The ugly "%begin{latexonly}" pseudo-environment suppresses the table% of contents for HTML generation.%%begin{latexonly}\tableofcontents%end{latexonly}  \section{Introduction}\label{intro} Although Python's extensive standard library covers many programmingneeds, there often comes a time when you need to add some newfunctionality to your Python installation in the form of third-partymodules.  This might be necessary to support your own programming, or tosupport an application that you want to use and that happens to bewritten in Python. In the past, there has been little support for adding third-partymodules to an existing Python installation.  With the introduction ofthe Python Distribution Utilities (Distutils for short) in Python 2.0,this changed. This document is aimed primarily at the people who need to installthird-party Python modules: end-users and system administrators who justneed to get some Python application running, and existing Pythonprogrammers who want to add some new goodies to their toolbox.  Youdon't need to know Python to read this document; there will be somebrief forays into using Python's interactive mode to explore yourinstallation, but that's it.  If you're looking for information on howto distribute your own Python modules so that others may use them, seethe \citetitle[../dist/dist.html]{Distributing Python Modules} manual.  \subsection{Best case: trivial installation}\label{trivial-install} In the best case, someone will have prepared a special version of themodule distribution you want to install that is targeted specifically atyour platform and is installed just like any other software on yourplatform.  For example, the module developer might make an executableinstaller available for Windows users, an RPM package for users ofRPM-based Linux systems (Red Hat, SuSE, Mandrake, and many others), aDebian package for users of Debian-based Linux systems, and so forth. In that case, you would download the installer appropriate to yourplatform and do the obvious thing with it: run it if it's an executableinstaller, \code{rpm --install} it if it's an RPM, etc.  You don't needto run Python or a setup script, you don't need to compileanything---you might not even need to read any instructions (althoughit's always a good idea to do so anyways). Of course, things will not always be that easy.  You might be interestedin a module distribution that doesn't have an easy-to-use installer foryour platform.  In that case, you'll have to start with the sourcedistribution released by the module's author/maintainer.  Installingfrom a source distribution is not too hard, as long as the modules arepackaged in the standard way.  The bulk of this document is aboutbuilding and installing modules from standard source distributions.  \subsection{The new standard: Distutils}\label{new-standard} If you download a module source distribution, you can tell prettyquickly if it was packaged and distributed in the standard way, i.e.using the Distutils.  First, the distribution's name and version numberwill be featured prominently in the name of the downloaded archive, e.g.\file{foo-1.0.tar.gz} or \file{widget-0.9.7.zip}.  Next, the archivewill unpack into a similarly-named directory: \file{foo-1.0} or\file{widget-0.9.7}.  Additionally, the distribution will contain asetup script \file{setup.py}, and a file named \file{README.txt} or possiblyjust \file{README}, which should explain that building and installing themodule distribution is a simple matter of running \begin{verbatim}python setup.py install\end{verbatim} If all these things are true, then you already know how to build andinstall the modules you've just downloaded:  Run the command above.Unless you need to install things in a non-standard way or customize thebuild process, you don't really need this manual.  Or rather, the abovecommand is everything you need to get out of this manual.  \section{Standard Build and Install}\label{standard-install} As described in section~\ref{new-standard}, building and installinga module distribution using the Distutils is usually one simple command: \begin{verbatim}python setup.py install\end{verbatim} On \UNIX, you'd run this command from a shell prompt; on Windows, youhave to open a command prompt window (``DOS box'') and do it there; onMac OS X, you open a \command{Terminal} window to get a shell prompt.  \subsection{Platform variations}\label{platform-variations} You should always run the setup command from the distribution rootdirectory, i.e. the top-level subdirectory that the module sourcedistribution unpacks into.  For example, if you've just downloaded amodule source distribution \file{foo-1.0.tar.gz} onto a\UNIX{} system, the normal thing to do is: \begin{verbatim}gunzip -c foo-1.0.tar.gz | tar xf -    # unpacks into directory foo-1.0cd foo-1.0python setup.py install\end{verbatim} On Windows, you'd probably download \file{foo-1.0.zip}.  If youdownloaded the archive file to \file{C:\textbackslash{}Temp}, then itwould unpack into \file{C:\textbackslash{}Temp\textbackslash{}foo-1.0};you can use either a archive manipulator with a graphical user interface(such as WinZip) or a command-line tool (such as \program{unzip} or\program{pkunzip}) to unpack the archive.  Then, open a command promptwindow (``DOS box''), and run: \begin{verbatim}cd c:\Temp\foo-1.0python setup.py install\end{verbatim} \subsection{Splitting the job up}\label{splitting-up} Running \code{setup.py install} builds and installs all modules in onerun.  If you prefer to work incrementally---especially useful if youwant to customize the build process, or if things are going wrong---youcan use the setup script to do one thing at a time.  This isparticularly helpful when the build and install will be done bydifferent users---for example, you might want to build a module distributionand hand it off to a system administrator for installation (or do ityourself, with super-user privileges). For example, you can build everything in one step, and then installeverything in a second step, by invoking the setup script twice: \begin{verbatim}python setup.py buildpython setup.py install\end{verbatim} If you do this, you will notice that running the \command{install}command first runs the \command{build} command, which---in thiscase---quickly notices that it has nothing to do, since everything inthe \file{build} directory is up-to-date. You may not need this ability to break things down often if all you dois install modules downloaded off the 'net, but it's very handy for moreadvanced tasks.  If you get into distributing your own Python modulesand extensions, you'll run lots of individual Distutils commands ontheir own.  \subsection{How building works}\label{how-build-works} As implied above, the \command{build} command is responsible for puttingthe files to install into a \emph{build directory}.  By default, this is\file{build} under the distribution root; if you're excessivelyconcerned with speed, or want to keep the source tree pristine, you canchange the build directory with the \longprogramopt{build-base} option.For example: \begin{verbatim}python setup.py build --build-base=/tmp/pybuild/foo-1.0\end{verbatim} (Or you could do this permanently with a directive in your system orpersonal Distutils configuration file; seesection~\ref{config-files}.)  Normally, this isn't necessary. The default layout for the build tree is as follows: \begin{verbatim}--- build/ --- lib/or--- build/ --- lib.<plat>/               temp.<plat>/\end{verbatim} where \code{<plat>} expands to a brief description of the currentOS/hardware platform and Python version.  The first form, with just a\file{lib} directory, is used for ``pure module distributions''---thatis, module distributions that include only pure Python modules.  If amodule distribution contains any extensions (modules written in C/\Cpp),then the second form, with two \code{<plat>} directories, is used.  Inthat case, the \file{temp.\filevar{plat}} directory holds temporaryfiles generated by the compile/link process that don't actually getinstalled.  In either case, the \file{lib} (or\file{lib.\filevar{plat}}) directory contains all Python modules (purePython 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 handlethe job of installing Python modules and applications.  \subsection{How installation works}\label{how-install-works} After the \command{build} command runs (whether you run it explicitly,or the \command{install} command does it for you), the work of the\command{install} command is relatively simple: all it has to do is copyeverything under \file{build/lib} (or \file{build/lib.\filevar{plat}})to your chosen installation directory. If you don't choose an installation directory---i.e., if you just run\code{setup.py install}---then the \command{install} command installs tothe standard location for third-party Python modules.  This locationvaries by platform and by how you built/installed Python itself.  On\UNIX{} (and Mac OS X, which is also \UNIX-based),it also depends on whether the module distributionbeing installed is pure Python or contains extensions (``non-pure''):\begin{tableiv}{l|l|l|c}{textrm}%  {Platform}{Standard installation location}{Default value}{Notes}  \lineiv{\UNIX{} (pure)}          {\filenq{\filevar{prefix}/lib/python\shortversion/site-packages}}          {\filenq{/usr/local/lib/python\shortversion/site-packages}}          {(1)}  \lineiv{\UNIX{} (non-pure)}          {\filenq{\filevar{exec-prefix}/lib/python\shortversion/site-packages}}          {\filenq{/usr/local/lib/python\shortversion/site-packages}}          {(1)}  \lineiv{Windows}          {\filenq{\filevar{prefix}}}          {\filenq{C:\textbackslash{}Python}}          {(2)}\end{tableiv} \noindent Notes:\begin{description}\item[(1)] Most Linux distributions include Python as a standard part of  the system, so \filevar{prefix} and \filevar{exec-prefix} are usually  both \file{/usr} on Linux.  If you build Python yourself on Linux (or  any \UNIX-like system), the default \filevar{prefix} and  \filevar{exec-prefix} are \file{/usr/local}.\item[(2)] The default installation directory on Windows was  \file{C:\textbackslash{}Program Files\textbackslash{}Python} under  Python 1.6a1, 1.5.2, and earlier.\end{description} \filevar{prefix} and \filevar{exec-prefix} stand for the directoriesthat Python is installed to, and where it finds its libraries atrun-time.  They are always the same under Windows, and veryoften the same under \UNIX and Mac OS X.  You can find out what your Pythoninstallation uses for \filevar{prefix} and \filevar{exec-prefix} byrunning Python in interactive mode and typing a few simple commands.Under \UNIX, just type \code{python} at the shell prompt.  UnderWindows, choose \menuselection{Start \sub Programs \sub Python\shortversion \sub Python (command line)}.  Once the interpreter is started, you type Python code at theprompt.  For example, on my Linux system, I type the three Pythonstatements shown below, and get the output as shown, to find out my\filevar{prefix} and \filevar{exec-prefix}: \begin{verbatim}Python 2.4 (#26, Aug  7 2004, 17:19:02) Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.prefix'/usr'>>> sys.exec_prefix'/usr'\end{verbatim} If you don't want to install modules to the standard location, or if youdon't have permission to write there, then you need to read aboutalternate installations in section~\ref{alt-install}.  If you want tocustomize your installation directories more heavily, seesection~\ref{custom-install} on custom installations.  % This rather nasty macro is used to generate the tables that describe% each installation scheme.  It's nasty because it takes two arguments% for each "slot" in an installation scheme, there will soon be more% than five of these slots, and TeX has a limit of 10 arguments to a% macro.  Uh-oh. \newcommand{\installscheme}[8]  {\begin{tableiii}{l|l|l}{textrm}          {Type of file}          {Installation Directory}          {Override option}     \lineiii{pure module distribution}             {\filevar{#1}\filenq{#2}}             {\longprogramopt{install-purelib}}     \lineiii{non-pure module distribution}             {\filevar{#3}\filenq{#4}}             {\longprogramopt{install-platlib}}     \lineiii{scripts}             {\filevar{#5}\filenq{#6}}             {\longprogramopt{install-scripts}}     \lineiii{data}             {\filevar{#7}\filenq{#8}}             {\longprogramopt{install-data}}   \end{tableiii}}  \section{Alternate Installation}\label{alt-install} Often, it is necessary or desirable to install modules to a locationother than the standard location for third-party Python modules.  Forexample, on a \UNIX{} system you might not have permission to write to thestandard third-party module directory.  Or you might wish to try out amodule before making it a standard part of your local Pythoninstallation.  This is especially true when upgrading a distributionalready present: you want to make sure your existing base of scriptsstill works with the new version before actually upgrading. The Distutils \command{install} command is designed to make installingmodule distributions to an alternate location simple and painless.  Thebasic idea is that you supply a base directory for the installation, andthe \command{install} command picks a set of directories (called an\emph{installation scheme}) under this base directory in which toinstall files.  The details differ across platforms, so read whicheverof the following sections applies to you.  \subsection{Alternate installation: the home scheme}\label{alt-install-prefix} The idea behind the ``home scheme'' is that you build and maintain apersonal stash of Python modules.  This scheme's name is derived fromthe idea of a ``home'' directory on \UNIX, since it's not unusual fora \UNIX{} user to make their home directory have a layout similar to\file{/usr/} or \file{/usr/local/}.  This scheme can be used byanyone, regardless of the operating system their installing for. Installing a new module distribution is as simple as \begin{verbatim}python setup.py install --home=<dir>\end{verbatim} where you can supply any directory you like for the\longprogramopt{home} option.  On \UNIX, lazy typists can just type atilde (\code{\textasciitilde}); the \command{install} command willexpand this to your home directory: \begin{verbatim}python setup.py install --home=~\end{verbatim} The \longprogramopt{home} option defines the installation basedirectory.  Files are installed to the following directories under theinstallation base as follows:\installscheme{home}{/lib/python}              {home}{/lib/python}              {home}{/bin}              {home}{/share}  \versionchanged[The \longprogramopt{home} option used to be supported                only on \UNIX]{2.4}  \subsection{Alternate installation: \UNIX{} (the prefix scheme)}\label{alt-install-home} The ``prefix scheme'' is useful when you wish to use one Pythoninstallation to perform the build/install (i.e., to run the setupscript), but install modules into the third-party module directory of adifferent Python installation (or something that looks like a differentPython installation).  If this sounds a trifle unusual, it is---that'swhy the ``home scheme'' comes first.  However, there are at least twoknown cases where the prefix scheme will be useful. First, consider that many Linux distributions put Python in \file{/usr},rather than the more traditional \file{/usr/local}.  This is entirelyappropriate, since in those cases Python is part of ``the system''rather than a local add-on.  However, if you are installing Pythonmodules from source, you probably want them to go in\file{/usr/local/lib/python2.\filevar{X}} rather than\file{/usr/lib/python2.\filevar{X}}.  This can be done with \begin{verbatim}/usr/bin/python setup.py install --prefix=/usr/local\end{verbatim} Another possibility is a network filesystem where the name used to writeto a remote directory is different from the name used to read it: forexample, the Python interpreter accessed as \file{/usr/local/bin/python}might search for modules in \file{/usr/local/lib/python2.\filevar{X}},but those modules would have to be installed to, say,\file{/mnt/\filevar{@server}/export/lib/python2.\filevar{X}}.  Thiscould be done with \begin{verbatim}/usr/local/bin/python setup.py install --prefix=/mnt/@server/export\end{verbatim} In either case, the \longprogramopt{prefix} option defines theinstallation base, and the \longprogramopt{exec-prefix} option definesthe platform-specific installation base, which is used forplatform-specific files.  (Currently, this just means non-pure moduledistributions, but could be expanded to C libraries, binary executables,etc.)  If \longprogramopt{exec-prefix} is not supplied, it defaults to\longprogramopt{prefix}.  Files are installed as follows: \installscheme{prefix}{/lib/python2.\filevar{X}/site-packages}              {exec-prefix}{/lib/python2.\filevar{X}/site-packages}              {prefix}{/bin}              {prefix}{/share} There is no requirement that \longprogramopt{prefix} or\longprogramopt{exec-prefix} actually point to an alternate Pythoninstallation; if the directories listed above do not already exist, theyare created at installation time. Incidentally, the real reason the prefix scheme is important is simplythat a standard \UNIX{} installation uses the prefix scheme, but with\longprogramopt{prefix} and \longprogramopt{exec-prefix} supplied byPython itself as \code{sys.prefix} and \code{sys.exec\_prefix}.  Thus,you might think you'll never use the prefix scheme, but every time yourun \code{python setup.py install} without any other options, you'reusing it. Note that installing extensions to an alternate Python installation hasno effect on how those extensions are built: in particular, the Pythonheader files (\file{Python.h} and friends) installed with the Pythoninterpreter used to run the setup script will be used in compilingextensions.  It is your responsibility to ensure that the interpreterused to run extensions installed in this way is compatible with theinterpreter used to build them.  The best way to do this is to ensurethat the two interpreters are the same version of Python (possiblydifferent builds, or possibly copies of the same build).  (Of course, ifyour \longprogramopt{prefix} and \longprogramopt{exec-prefix} don't evenpoint to an alternate Python installation, this is immaterial.)  \subsection{Alternate installation: Windows (the prefix scheme)}\label{alt-install-windows} Windows has no concept of a user's home directory, and since thestandard Python installation under Windows is simpler than under\UNIX, the \longprogramopt{prefix} option has traditionally been usedto install additional packages in separate locations on Windows. \begin{verbatim}python setup.py install --prefix="\Temp\Python"\end{verbatim} to install modules to the\file{\textbackslash{}Temp\textbackslash{}Python} directory on thecurrent drive.

Lines 1–500 of 1112.