blob: e7442df9078cd077a780bf6c8a0f47c19d3b32fc [file] [log] [blame]
Greg Ward7c1e5f62000-03-10 01:56:58 +00001\documentclass{howto}
2\usepackage{ltxmarkup}
3\usepackage{times}
4
5\title{Installing Python Modules}
6
7% The audience for this document includes people who don't know anything
8% about Python and aren't about to learn the language just in order to
9% install and maintain it for their users, i.e. system administrators.
10% Thus, I have to be sure to explain the basics at some point:
11% sys.path and PYTHONPATH at least. Should probably give pointers to
12% other docs on "import site", PYTHONSTARTUP, PYTHONHOME, etc.
13%
14% Also, I need to take into account that most modules out there don't
15% (yet) use Distutils: briefly explain the old Makefile.pre.in
16% convention (maybe move material from the E&E manual to here?), and
17% explain where to copy .py and .so files manually if the distribution
18% doesn't provide a mechanism for doing so.
19%
20% Finally, it might be useful to include all the material from my "Care
21% and Feeding of a Python Installation" talk in here somewhere. Yow!
22
23% Hey wow, Guido didn't write this one either!
24\author{Greg Ward}
25\authoraddress{E-mail: \email{gward@python.net}}
26
27% Should these be added to the standard Python doc tools? (They'll be
28% needed for my "Distributing Python Modules" guide, too.)
29\newcommand{\command}[1]{\code{#1}}
30\newcommand{\option}[1]{\code{#1}}
31\newcommand{\filevar}[1]{{\textsl{\filenq{#1}}}}
32\newcommand{\comingsoon}{\emph{Coming soon$\ \ldots$}}
33
34% And how about these? Very handy for writing pathnames (tilde for
35% Unix, backslash for DOS/Windows).
36\renewcommand{\tilde}{\raisebox{-0.5ex}{\symbol{126}}}
37\newcommand{\bslash}{\symbol{92}}
38
39
40\begin{document}
41
42\maketitle
43
44%\begin{abstract}
45%\noindent
46%Abstract this!
47%\end{abstract}
48
49\tableofcontents
50
51\section{Introduction}
52\label{sec:intro}
53
54\comingsoon
55
56\subsection{The new way: Distutils}
57\label{sec:new-way}
58
59
60\subsection{The old way (pure Python): whatever you feel like}
61\label{sec:old-way-pure}
62
63
64\subsection{The old way (extensions, \UNIX{} only): Makefile.pre.in}
65\label{sec:old-way-ext}
66
67
68
69
70
71\section{Normal Build and Install}
72\label{sec:normal-install}
73
74% This will cover:
75% * setup.py install (the usual thing)
76% * setup.py build (if you like doing things one-at-a-time)
77% * setup.py build install (not necessary unless you need to supply
78% build options--ref. next section)
79% * where things are installed, on Unix and Windows (Mac...?)
80% * simple custom install: "install --prefix=$HOME"
81\comingsoon
82
83
84\section{Custom Extension Building}
85\label{sec:custom-ext}
86
87% This will cover:
88% * normal extension build -- stress that it doesn't matter, you
89% do the same thing whether there are extensions or not
90% * what you might want to customize: compiler and compiler
91% flags (warn of the dangers); per-file compiler flags
92% (not handled yet!)
93% * when things go wrong: I don't know! (and I don't know what
94% to do, either!)
95\comingsoon
96
97
98\section{Custom Installation (\UNIX)}
99\label{sec:custom-install-unix}
100
101% XXX probably should banish mentions of Windows here to the
102% separate "Non-standard installation (Windows)" section.
103
104A \dfn{custom installation} is where you install modules to a location
105that's not in Python's default module search path. There are a couple
106of reasons you might want to do this; the most typical is simply that
107you don't have permission to write to the standard Python library
108directory. Or, even if you do have write permission to the standard
109library, you might wish to install a module distribution into a
110non-standard place for testing or experimentation. (This is especially
111useful when upgrading an existing module distribution: you might want to
112make sure that your existing scripts continue to work as before, and
113only then install the upgrade ``for real.'')
114
115(XXX terminology: I keep saying ``standard Python library directory''
116when I really mean ``the site-packages directory under the standard
117Python library directory''. Is there a better way?)
118
119In any event, you can easily install to non-standard locations with a
120couple of options to the \command{install} command:
121
122\begin{tableii}{ll}{option}{Option}{Description}
123 \lineii{prefix}{base dir for pure Python distributions
124 (overrides \code{sys.prefix})}
125 \lineii{exec-prefix}{base dir for distributions with extensions
126 (overrides \code{sys.exec_prefix})}
127 \lineii{install-lib}{install dir for top-level modules from pure
128 Python distributions}
129 \lineii{install-platlib}{install dir for top-level modules from
130 distributions with extensions}
131 \lineii{install-path}{extra path under \option{install-lib} or
132 \option{install-platlib} to install to}
133\end{tableii}
134
135
136\subsection{Prefix options}
137\label{sec:prefix-options}
138
139There are a lot of picky little rules that govern the interactions of
140these five options. As usual, it's easier to explain things with
141examples, so we'll save all the picky rules for later, after you've seen
142a bunch of examples. However, we really have to establish some ground
143rules before we can dive into the examples:
144\begin{itemize}
145\item in a normal \UNIX{} installation, \code{sys.prefix} and
146 \code{sys.exec\_prefix} are both \file{/usr/local}.
147\item in a multi-platform \UNIX{} installation, \code{sys.prefix} and
148 \code{sys.exec\_prefix} are different, and are selected when you
149 configure and build Python itself. Our canonical example of a
150 multi-platform installation will have a \code{sys.prefix} of
151 \file{/usr/local} and a \code{sys.exec\_prefix} of
152 \file{/usr/local.\filevar{plat}} (for whatever value of \filevar{plat}
153 is appropriate).
154\item the canonical place to install third-party modules is
155 either \file{\filevar{prefix}/lib/python1.\filevar{X}/site-packages}
156 or \file{\filevar{exec\_prefix}/lib/python1.\filevar{X}/site-packages}.
157 These will be referred to as ``the site-packages directories.''
158\end{itemize}
159
160
161\subsubsection{Pure Python module distribution}
162
163To demonstrate, consider a hypothetical module distribution that
164contains one top-level module and a package with two modules:
165\begin{tableii}{ll}{module}{Module}{Filename}
166 \lineii{mymod}{\filenq{mymod.py}}
167 \lineii{mypkg.mod1}{\filenq{mypkg/mod1.py}}
168 \lineii{mypkg.mod2}{\filenq{mypkg/mod2.py}}
169\end{tableii}
170where the filenames are relative to \file{build/lib} after building, or
171to some directory in \code{sys.path} after installation.
172
173The goal of installation is to copy these files into a directory in
174\code{sys.path} without interfering with the standard Python library.
175The canonical, preferred, and most obvious thing to do is to put them in
176the ``site-packages'' directory, which is exactly what the
177\command{install} comand does by default: under a normal \UNIX{} Python
178installation,
179\begin{verbatim}
180 python setup.py install
181\end{verbatim}
182installs \file{/usr/local/lib/python1.\filevar{X}/lib/site-packages/mymod.py},
183with the \module{mypkg} package in a \file{mypkg} directory under
184\file{site-packages}.
185
186However, if you were interested in a standard installation, you wouldn't
187be reading this section. The next-most-standard thing to do is to
188specify a custom prefix to override \code{sys.prefix}. For example:
189\begin{verbatim}
190 python setup.py install --prefix=/home/greg
191\end{verbatim}
192is a sensible way to install Python modules to your home directory: this
193results in the installation of \file{/home/greg/lib/python/mymod.py},
194with the \module{mypkg} modules in \file{/home/greg/lib/python/mypkg/}.
195An important point here is that in both this example and the ``plain
196vanilla'' example above, the actual installation directory is derived
197from the \option{prefix} option. However, when \option{prefix} differs
198from \code{sys.prefix}, the installation directory is derived
199differently: the Python version and \file{site-packages} are omitted.
200(The version number is part of the standard library directory name to
201describe the version of the standard library, so it doesn't make sense
202to include it in the name of a non-standard-library directory; likewise,
203\file{site-packages} is meant to denote non-standard modules living in
204the same area as the standard library, so it doesn't make sense to
205include it when installing to a non-standard library. [XXX check with
206Guido that this reasoning is valid and correct; Fred disagrees!])
207
208
209\subsubsection{Module distribution with extensions}
210
211Now let's consider a different hypothetical module distribution, which
212consists of a single package, \module{foo}, containing one pure Python
213module and one extension module:
214\begin{tableii}{ll}{module}{Module}{Filename}
215 \lineii{foo.pure}{\filenq{foo/pure.py}}
216 \lineii{foo.ext}{\filenq{foo/ext.so} (or \file{foo/extmodule.so})}
217\end{tableii}
218In this case, the two modules will be in different locations in the
219build tree: \file{build/lib/foo/pure.py} and
220\file{build/platlib/foo/ext.so}. (The \file{.so} (``shared object'')
221extension isn't universal, but it's the norm on \UNIX-like systems;
222under Windows, the extension module will be in \file{foo/ext.pyd} or
223\file{foo/extmodule.pyd}.)
224
225Consider again a standard, plain-vanilla installation:
226\begin{verbatim}
227 python setup.py install
228\end{verbatim}
229In this case, \emph{both} modules will be installed to the site-packages
230directory under \code{sys.exec\_prefix}, e.g. to
231\file{/usr/local.\filevar{plat}/lib/python1.\filevar{X}/site-packages}
232on a \UNIX{} system where Python was configured with
233\samp{--exec-prefix=/usr/local.plat}. (On Windows, again, there is no
234site-packages directory and \code{sys.prefix} and
235\code{sys.exec\_prefix} are the same---so both modules will just be
236installed to \code{sys.prefix}.)
237
238Of course, we've already established that you're not interested in
239standard installations. If you just want to install these modules to
240your home directory, and you don't maintain a multi-platform home
241directory, no problem---just set the prefix as before:
242\begin{verbatim}
243python setup.py install --prefix=/home/greg
244\end{verbatim}
245and both modules will be installed to \file{/home/greg/lib/python}.
246
247Now let's say your Python installation is in \file{/usr}---as is the
248case in many Linux distributions---but your local policy is to install
249third-party software to a network-wide \file{/usr/local} and
250\file{/usr/local.\filevar{plat}}. That is, \code{sys.prefix} and
251\code{sys.exec\_prefix} are both \file{/usr}, and you want Python
252modules to be installed to either \file{/usr/local/lib/python} or
253\file{/usr/local.\filevar{plat}/lib/python}. This is one case where you
254want to specify both \option{prefix} and \option{exec-prefix}:
255\begin{verbatim}
256python setup.py install --prefix=/usr/local \
257 --exec-prefix=/usr/local.plat
258\end{verbatim}
259An oddity of this situation is that for any given module distribution,
260you only have to supply \emph{one} of \option{prefix} and
261\option{exec-prefix}, because pure Python distributions are always
262installed under \option{prefix}, and extension-containing distributions
263are always installed under \option{exec-prefix}. For consistency's
264sake, though, it's best always to supply both---and the best way to do
265that is by using a system-wide configuration file (see
266Section~\ref{sec:config-files}).
267
268You could use a similar scheme to maintain a multi-platform personal
269Python library. For example, if you install lots of stuff to your home
270directory (not just Python modules), you might have a complete
271\file{\tilde/usr} with \file{include}, \file{man}, \file{lib}, and so
272forth. (The advantage of this scheme is that it keeps those mock system
273directories out of your home directory and makes it easier to support a
274multi-platform personal \file{usr} tree.) If you don't care about a
275multi-platform installation, you can just install with
276\begin{verbatim}
277python setup.py install --prefix=$HOME/usr
278\end{verbatim}
279But if you want to keep separate \file{usr} trees for each architecture
280that you use, you could say
281\begin{verbatim}
282python setup.py install --prefix=$HOME/usr \
283 --exec-prefix=$HOME/usr.plat
284\end{verbatim}
285for various values of \file{plat}.
286
287% this paragraph is for Michel Sanner ;-)
288(Perceptive readers will note that on a multi-platform Python
289installation, multiple identical copies of \file{foo/pure.py} will be
290installed, one for each platform. This is deliberate. First, it makes
291Python's module search algorithm simpler (XXX check this): when you say
292\samp{import foo.pure}, Python searches \code{sys.path} until it finds a
293directory containing \file{foo/__init__.py}. When it finds one, that
294directory is deemed to be the directory containing the \module{foo}
295package for this import. Even if the search algorithm were changed
296(necessitating a trip back in time to ``fix'' Python 1.5), the only way
297to make multiple candidate \module{foo} directories (one for pure
298Python, one for extension modules) would be to make copies of
299\file{__init__.py}---in which case, why not make copies of all the pure
300Python modules? Second, if you kept pure Python modules related to
301extension modules in a platform-shared directory, what happens while you
302are upgrading your favourite extension from version 1.0 to 1.1 on
303platforms X and Y? After you install 1.1 for platform X, the 1.1
304\file{.py} files will be in the platform-shared directory---but the 1.0
305extensions will still be in the platform Y directory. If the interval
306between installing 1.1 for platform X and for platform Y is long---e.g.,
307there are portability problems with platform Y---then there's a good
308probability of a version mismatch between the 1.1 Python modules and the
3091.0 extensions on platform Y. The solution to both problems is to
310install separate copies of the pure Python modules for every platform.
311In this day and age, unnecessary disk use is no argument.)
312
313Other ways to support a multi-platform personal Python library are
314discussed below, when we cover the \option{install-lib} and
315\option{install-platlib} options.
316
317
318% Gory details on the prefix options (still need to work these into the
319% surrounding text):
320XXX need to finish these rules and give them some context!
321\begin{itemize}
322\item \code{sys.exec\_prefix} (and the \option{exec-prefix} option)
323 only matters on a multi-platform installation. If you don't have a
324 multi-platform installation (or even know what that is), then you
325 don't care about \option{exec-prefix}.
326\item in a normal Windows installation, \code{sys.prefix} and
327 \code{sys.exec\_prefix} are both \file{C:\bslash Program Files\bslash
328 Python}; they are never different under Windows (XXX check!).
329\item you may supply \emph{both} of \option{prefix} and
330 \option{exec-prefix}, or \emph{neither} of them, or \emph{just}
331 \option{prefix}---but you may not supply just \option{exec-prefix}.
332\end{itemize}
333
334
335\subsection{Installation directory options}
336\label{sec:install-dirs}
337
338Most of the time, it's enough to specify just \option{prefix} (and
339possibly \option{exec-prefix})---your modules are installed to
340\file{lib/python} under one or the other, you add the appropriate
341directory to \code{sys.path}, and that's it.
342
343However, there will inevitably be times when you want finer control over
344the installation directories, and that is when the \option{install-lib},
345\option{install-platlib}, and \option{install-path} options are
346essential. Normally, \option{install-lib} and \option{install-platlib}
347are simply the directories where pure Python modules and extension
348modules, respectively, are installed. That is, top-level modules
349(modules not in a package) are installed straight to
350\option{install-lib} (or \option{install-platlib} if there are any
351extensions in the module distribution). (If \option{install-path} is
352supplied, then things are a bit more complex; we'll deal with that
353below.)
354
355Normally, \option{install-lib} and \option{install-platlib} are derived
356from \option{prefix} and/or \option{exec-prefix}. For example, if you
357don't supply anything, then \option{prefix} defaults to
358\code{sys.prefix}, and \option{install-lib} defaults to
359\file{\filevar{prefix}/lib/python1.\filevar{X}/site-packages}. If you
360supply \option{prefix} but not \option{install-lib}, then
361\option{install-lib} defaults to \file{\filevar{prefix}/lib/python}
362(unless you just happen to supply a prefix which equals
363\code{sys.prefix}, which is treated the same as if you don't supply
364\option{prefix} at all). (The rules for \option{exec-prefix} and
365\option{install-platlib} are a bit more complex; the following examples
366should clarify. Consult the Distutils source for the gory details.)
367
368To illustrate, let's go back to our hypothetical pure-Python module
369distribution containing \module{mymod}, \module{mypkg.mod1}, and
370\module{mypkg.mod2}. If you maintain a personal stash of Python modules
371in your home directory, but don't like the \file{\tilde/lib/python}
372convention, no problem---you can put the modules right in a
373\file{\tilde/python} directory with
374\begin{verbatim}
375python setup.py install --install-lib=$HOME/python
376\end{verbatim}
377which will install \file{\$HOME/python/mymod.py},
378\file{\$HOME/python/mypkg/mod1.py}, and
379\file{\$HOME/python/mypkg/mod2.py}.
380
381If you happen to install a module distribution that contains extensions,
382again that's no problem---in the absence of \option{exec-prefix},
383\option{install-platlib} defaults to \option{install-lib}, so the above
384example will also put extension modules in \file{\$HOME/python}.
385(XXX is this correct? is this the best way to describe it? should it be
386implemented this way or some other way? how should it be described?)
387
388This may not be what you want, though, if you maintain a multi-platform
389stash of Python modules in your home directory. In that case, you need
390to specify \option{install-platlib}---this is the directory where module
391distributions with extensions will be installed. For example, if you
392keep pure Python module distributions in \file{\tilde/python} and
393extension distributions in \file{\tilde/python.plat}:
394\begin{verbatim}
395python setup.py install --install-lib=$HOME/python \
396 --install-platlib=$HOME/python.plat
397\end{verbatim}
398(Just as with \option{prefix} and \option{exec-prefix}, it's only
399necessary to supply one of \option{install-lib} and
400\option{install-platlib} for any given module distribution, but to
401ensure consistency you should always supply them both using a
402configuration file (section~\ref{sec:config-files}).)
403
404An alternate way to maintain a multi-platform personal Python library is
405in \file{\tilde/lib/python} and \file{\tilde/lib/python.plat}. In that
406case, you can get away with supplying \option{prefix} and
407\option{install-platlib}:
408\begin{verbatim}
409python setup.py install --prefix=$HOME \
410 --install-platlib=$HOME/lib/python.plat
411\end{verbatim}
412
413Finally, the \option{install-path} option, which exists mainly to gum up
414the whole works---but in a productive (and important) way.
415Specifically, \option{install-path} exists to give a directory of their
416own to module distributions that wouldn't otherwise have one, i.e.\ that
417are not distributed as a (Python) package.
418
419Consider a module distribution, Foo, that consists of (pure Python)
420modules \module{foobar}, \module{foobaz}, and \module{fooqux}.
421Obviously these are related, and if the project had started in the
422Python 1.5 era (and doesn't worry about backwards compatibility with
423Python 1.4), they probably would be packaged up and called
424\module{foo.bar}, \module{foo.baz}, and \module{foo.qux}.
425Unfortunately, they aren't, but we still want the Foo modules to go into
426a directory of their own.
427
428Normally, this will be taken care of by the module developer: he adds a
429line \samp{install_path = 'Foo'} to his setup script, which has the
430following consequences:
431\begin{enumerate}
432\item instead of \option{install-lib} the modules would be installed in
433 \file{\filevar{install-lib}/Foo}
434\item if \option{install-lib} is the same as the default
435 \option{install-lib}---e.g., you supplied neither \option{prefix} or
436 \option{install-lib}---then a \file{Foo.pth} will be created in
437 \option{install-lib}, so that Python adds
438 \file{\filevar{install-lib}/Foo} to \code{sys.path}
439\item if \option{install-lib} is not the default, then a warning will be
440 printed, reminding you to add \file{\filevar{install-lib}/Foo} to
441 \code{sys.path} yourself, such as with the \code{PYTHONPATH}
442 environment variable
443\end{enumerate}
444
445Thus, you as a module installer have to be aware of the
446\option{install-path} option---especially if you maintain a personal
447stash of Python modules and don't have write permission to the standard
448library, so Distutils can't create \file{.pth} files for you---but you
449don't often have to supply it yourself. There are situations in which
450you might want to supply it, though:
451\begin{itemize}
452\item a module developer forgot to include it (the distribution really
453 should go in a directory of its own, but it won't unless you make it)
454\item you want to override the \option{install-path} supplied by the
455 developer (e.g., you'd rather have a huge jumble of files in
456 \file{site-packages} than make Python wade through a bunch of
457 \file{.pth} files at startup)
458\end{itemize}
459
460The first case is easy: say we're dealing with the Foo distribution
461again, but the developer forgot to include \option{install-path}. No
462problem, you can supply it on the command line:
463\begin{verbatim}
464python setup.py install --install-path=Foo
465\end{verbatim}
466Note that this will work just fine if you supply \option{prefix} or
467\option{install-lib}---but of course, you'll probably have to ensure
468that the \file{Foo} directory is in \code{sys.path} yourself.
469
470If you're really fanatical about keeping track of what you have
471installed, you might want to supply your own \option{install-path} that
472records the version as well as the name of the module distribution; this
473overrides any \option{install-path} included by the module developer in
474the setup script:
475\begin{verbatim}
476python setup.py install --install-path=Foo-1.3
477\end{verbatim}
478
479Finally, you can disable \option{install-path} entirely:
480\begin{verbatim}
481python setup.py install --install-path=''
482\end{verbatim}
483...but the mess that will result (modules from many different
484distributions in the same \option{install-lib} and
485\option{install-platlib} directories) is your own problem.
486
487% Points to make
488% * only one of prefix or exec_prefix matters
489% * don't have to specify exec_prefix unless != prefix
490% * thus, usually enough to supply prefix
491% * only have to supply install_lib if you don't like
492% "prefix/lib/python"
493% * likewise for install_platlib and exec_prefix
494% * don't have to supply install_platlib unless != install_lib (??)
495% * in the absence of install_path, top-level modules wind up in
496% install_lib or install_platlib
497In case you're interested, here are the exact rules for how
498\option{install-lib} and \option{install-platlib} are initialized, and
499how they and \option{install-path} affect where modules (pure Python and
500extensions) are installed to:
501\begin{itemize}
502\item If you don't supply \option{prefix} (and possibly
503 \option{exec-prefix}), then \option{install-lib} and
504 \option{install-platlib} will be, respectively,
505 \file{\filevar{\$prefix}/lib/python1.\filevar{X}/site-packages} and
506 \file{\filevar{\$exec\_prefix}/lib/python1.\filevar{X}/site-packages}. In a
507 normal \UNIX{} installation, both of these resolve to
508 \file{/usr/local/lib/python1.\filevar{X}/site-packages}.
509\item in the absence of an \option{install-path} option, top-level
510 modules and packages from a pure Python distribution are installed to
511 \option{install-lib}
512\item in the absence of an \option{install-path} option, top-level
513 modules and packages from a distribution that contains \emph{any}
514 extension modules are installed to \option{install-platlib}.
515\item \emph{there're more, but I don't remember everything offhand}
516%\item \option{install-lib} is initialized from \option{prefix} (which
517% in turn is initialized from \code{sys.prefix})---so you should
518\end{itemize}
519
520
521\section{Custom Installation (Windows)}
522\label{sec:custom-install-windows}
523
524\comingsoon
525
526
527\section{Configuration Files}
528\label{sec:config-files}
529
530\comingsoon
531
532
533
534\end{document}