blob: bfc1bcf8673d2f5d1a17e36042d2097d0bde1f84 [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}}
Greg Ward169f91b2000-03-10 01:57:51 +000030\newcommand{\option}[1]{\textsf{\small{#1}}}
Greg Ward7c1e5f62000-03-10 01:56:58 +000031\newcommand{\filevar}[1]{{\textsl{\filenq{#1}}}}
Greg Ward169f91b2000-03-10 01:57:51 +000032\newcommand{\homefile}[1]{\file{\tilde/#1}}
Greg Ward7c1e5f62000-03-10 01:56:58 +000033\newcommand{\comingsoon}{\emph{Coming soon$\ \ldots$}}
34
35% And how about these? Very handy for writing pathnames (tilde for
36% Unix, backslash for DOS/Windows).
37\renewcommand{\tilde}{\raisebox{-0.5ex}{\symbol{126}}}
38\newcommand{\bslash}{\symbol{92}}
39
40
41\begin{document}
42
43\maketitle
44
45%\begin{abstract}
46%\noindent
47%Abstract this!
48%\end{abstract}
49
50\tableofcontents
51
52\section{Introduction}
53\label{sec:intro}
54
55\comingsoon
56
57\subsection{The new way: Distutils}
58\label{sec:new-way}
59
60
61\subsection{The old way (pure Python): whatever you feel like}
62\label{sec:old-way-pure}
63
64
65\subsection{The old way (extensions, \UNIX{} only): Makefile.pre.in}
66\label{sec:old-way-ext}
67
68
69
70
71
72\section{Normal Build and Install}
73\label{sec:normal-install}
74
75% This will cover:
76% * setup.py install (the usual thing)
77% * setup.py build (if you like doing things one-at-a-time)
78% * setup.py build install (not necessary unless you need to supply
79% build options--ref. next section)
80% * where things are installed, on Unix and Windows (Mac...?)
81% * simple custom install: "install --prefix=$HOME"
82\comingsoon
83
84
85\section{Custom Extension Building}
86\label{sec:custom-ext}
87
88% This will cover:
89% * normal extension build -- stress that it doesn't matter, you
90% do the same thing whether there are extensions or not
91% * what you might want to customize: compiler and compiler
92% flags (warn of the dangers); per-file compiler flags
93% (not handled yet!)
94% * when things go wrong: I don't know! (and I don't know what
95% to do, either!)
96\comingsoon
97
98
99\section{Custom Installation (\UNIX)}
100\label{sec:custom-install-unix}
101
102% XXX probably should banish mentions of Windows here to the
103% separate "Non-standard installation (Windows)" section.
104
105A \dfn{custom installation} is where you install modules to a location
106that's not in Python's default module search path. There are a couple
107of reasons you might want to do this; the most typical is simply that
108you don't have permission to write to the standard Python library
109directory. Or, even if you do have write permission to the standard
110library, you might wish to install a module distribution into a
111non-standard place for testing or experimentation. (This is especially
112useful when upgrading an existing module distribution: you might want to
113make sure that your existing scripts continue to work as before, and
114only then install the upgrade ``for real.'')
115
116(XXX terminology: I keep saying ``standard Python library directory''
117when I really mean ``the site-packages directory under the standard
118Python library directory''. Is there a better way?)
119
120In any event, you can easily install to non-standard locations with a
121couple of options to the \command{install} command:
122
123\begin{tableii}{ll}{option}{Option}{Description}
Greg Ward169f91b2000-03-10 01:57:51 +0000124 \lineii {install-lib}
125 {install directory for modules from pure Python distributions}
126 \lineii {install-platlib}
127 {install directory for modules from distributions with extensions}
128 \lineii {prefix}
129 {override \code{sys.prefix}:
130 point to an alternate Python installation}
131 \lineii {exec-prefix}
132 {override \code{sys.exec_prefix}:
133 point to an alternate Python installation}
134 \lineii {install-path}
135 {extra sub-path to append to \option{install-lib} (for
136 non-package-ized distributions)}
Greg Ward7c1e5f62000-03-10 01:56:58 +0000137\end{tableii}
138
Greg Ward169f91b2000-03-10 01:57:51 +0000139Of these, the most commonly used will probably be \option{install-lib}
140and \option{install-platlib}: you use them to point module installation
141right at a particular directory. (You'll only need
142\option{install-platlib} if you maintain a multi-platform installation,
143which is often done on \UNIX{} networks with different architectures and
144operating systems.) The two prefix options are intended for the
145somewhat arcane purpose of installing modules into a different Python
146installation than the Python binary used to perform the installation.
147The last, \option{install-path}, is mainly used for module developers to
148ensure that their module will go into a directory of their own, but it
149may occasionally be useful to you as a module installer.
Greg Ward7c1e5f62000-03-10 01:56:58 +0000150
151
Greg Ward169f91b2000-03-10 01:57:51 +0000152\subsection{Directly specifying installation directories}
Greg Ward7c1e5f62000-03-10 01:56:58 +0000153\label{sec:install-dirs}
154
Greg Ward169f91b2000-03-10 01:57:51 +0000155The most common type of custom module installation is where you maintain
156a personal stash of Python modules under your home directory, say in
157\homefile{lib/python}. If you only care about a single platform
158there, then you only need to specify the \option{install-lib} option and
159can forget about \option{install-platlib}:
160\begin{verbatim}
161python setup.py install --install-lib=~/lib/python
162\end{verbatim}
163You can, of course, supply whatever directory you like in place of
164\homefile{lib/python}. More importantly, you can specify this
165directory permanently in your personal configuration file (XXX
166filename?):
167\begin{verbatim}
168[install]
169install-lib=~/lib/python
170\end{verbatim}
171Note that use of shell-style tilde and environment variable expansion is
172supported both on the command line and in configuration files. (See
173section~\ref{sec:config-files} for more information on configuration
174files.)
Greg Ward7c1e5f62000-03-10 01:56:58 +0000175
Greg Ward169f91b2000-03-10 01:57:51 +0000176Of course, in order for this personal Python library scheme to work, you
177have to ensure that \homefile{lib/python} is in \code{sys.path} when you
178run Python. The easiest way to do this under \UNIX{} is to add it to
179your \code{PYTHONPATH} environment variable when you login. For
180example, if you use a Bourne shell derivative such as bash, zsh, or ksh,
181add the following to your \homefile{.profile} (or \homefile{.bashrc}, or
182\homefile{.zshenv}, depending on your shell and personal preferences):
183\begin{verbatim}
184export PYTHONPATH=$HOME/lib/python
185\end{verbatim}
186If you use a csh-derivative such as tcsh, add the following to your
187\homefile{.cshrc}:
188\begin{verbatim}
189setenv PYTHONPATH $HOME/lib/python
190\end{verbatim}
191
192If you use multiple platforms (architectures and/or operating systems)
193from the same home directory, then you probably want to maintain a
194multi-platform personal Python library. One possible scheme is to put
195platform-neutral (pure Python) distributions in \homefile{lib/python}
196and platform-specific distributions (any that containe extension
197modules) in \homefile{lib/python.\filevar{plat}}:
198\begin{verbatim}
199python setup.py install --install-lib=~/lib/python \
200 --install-lib-plat=~/lib/python.plat \
201\end{verbatim}
202On the command line, of course, you can just type in the current
203platform in place of \filevar{plat}: \file{linux-x86},
204\file{solaris-sparc}, \file{linux-alpha}, whatever. That's not an
205option in a configuration file, though---the same file has to cover all
206platforms for which you maintain a personal Python library. So the
207Distutils provide a \code{PLAT} environment variable which will expand
208to the current platform name:
209\begin{verbatim}
210[install]
211install-lib=~/lib/python
212install-platlib=~/lib/python.$PLAT
213\end{verbatim}
214(If \code{PLAT} is already defined in your environment, the Distutils
215won't override it: that way you can maintain consistency with other
216applications that look for a \code{PLAT} variable; this is especially
217useful when you refer to \code{PLAT} in your login scripts, as explained
Greg Ward7c1e5f62000-03-10 01:56:58 +0000218below.)
219
Greg Ward169f91b2000-03-10 01:57:51 +0000220(XXX danger danger! this environment-variable-in-config-file thing is
221frighteningly make-like: is there any way to avoid it?)
Greg Ward7c1e5f62000-03-10 01:56:58 +0000222
Greg Ward169f91b2000-03-10 01:57:51 +0000223Again, you have to make sure that your personal Python library appears
224in \code{sys.path}, and again the easiest way to do this is to set
225\code{PYTHONPATH} in your login scripts. This time, though, you have to
226be sure to set \emph{both} directories (platform-neutral and the current
227platform-specific directory). For Bourne-shell derivatives:
Greg Ward7c1e5f62000-03-10 01:56:58 +0000228\begin{verbatim}
Greg Ward169f91b2000-03-10 01:57:51 +0000229export PYTHONPATH=$HOME/lib/python:$HOME/lib/python.$PLAT
Greg Ward7c1e5f62000-03-10 01:56:58 +0000230\end{verbatim}
Greg Ward169f91b2000-03-10 01:57:51 +0000231and for csh-derivatives:
Greg Ward7c1e5f62000-03-10 01:56:58 +0000232\begin{verbatim}
Greg Ward169f91b2000-03-10 01:57:51 +0000233setenv PYTHONPATH $HOME/lib/python:$HOME/lib/python.$PLAT
Greg Ward7c1e5f62000-03-10 01:56:58 +0000234\end{verbatim}
Greg Ward169f91b2000-03-10 01:57:51 +0000235Note that it is your responsibility to set the \code{PATH} environment
236variable (unless your system administrator has kindly taken care of it
237in the system-wide login scripts, which is a wise thing to do on
238multi-platform networks). One way to do this is with the \code{uname}
239command:
Greg Ward7c1e5f62000-03-10 01:56:58 +0000240\begin{verbatim}
Greg Ward169f91b2000-03-10 01:57:51 +0000241export PLAT=`uname -sm | tr 'A-Z ' 'a-z-'`
242\end{verbatim}
243(XXX check that this works well on other Unices: on Linux, \code{-m}
244becomes eg. \code{i586}, which is not the \emph{machine} but the
245\emph{processor}. Arggh!)
246
247Of course, there are more reasons to do custom installation than
248maintaining a personal Python library. Even if you have write access to
249the system-wide directories for third-party modules
250(\file{\filevar{prefix}/lib/python1.\filevar{x}/site-packages} and
251\file{\filevar{exec-prefix}/lib/python1.\filevar{x}/site-packages}), you
252might want to install new module distributions---especially upgrades of
253modules that are crucial to your local infrastructure---to a temporary
254location, in order to test them before installing them ``for real.''
255This is fundamentally no different from installing to your home
256directory, except that you probably won't bother to set
257\code{PYTHONPATH} permanently. For example, to install a module
258distribution to \file{/tmp/pylib}:
259\begin{verbatim}
260python setup.py install --install-lib=/tmp/pylib
261\end{verbatim}
262Then, of course, you'll want to run some script that depends on these
263modules to make sure that they still work with your installed base of
264code:
265\begin{verbatim}
266env PYTHONPATH=/tmp/pylib python /usr/local/bin/crucial_script ...
Greg Ward7c1e5f62000-03-10 01:56:58 +0000267\end{verbatim}
268
Greg Ward169f91b2000-03-10 01:57:51 +0000269Of course, you can do this temporary installation with separate
270\option{install-lib} and \option{install-platlib} options. If you're
271doing this to a network-wide directory, not \file{/tmp}, this might be
272essential. As you might have guessed, it's not too hard:
Greg Ward7c1e5f62000-03-10 01:56:58 +0000273\begin{verbatim}
Greg Ward169f91b2000-03-10 01:57:51 +0000274python setup.py install --install-lib=/scratch/pylib \
275 --install-platlib=/scratch/pylib.plat
Greg Ward7c1e5f62000-03-10 01:56:58 +0000276\end{verbatim}
Greg Ward169f91b2000-03-10 01:57:51 +0000277and then, testing your crucial scripts on multiple platforms:
Greg Ward7c1e5f62000-03-10 01:56:58 +0000278\begin{verbatim}
Greg Ward169f91b2000-03-10 01:57:51 +0000279env PYTHONPATH=/scratch/pylib:/scratch/pylib.plat \
280 python /usr/local/bin/crucial_script ...
Greg Ward7c1e5f62000-03-10 01:56:58 +0000281\end{verbatim}
282
Greg Ward169f91b2000-03-10 01:57:51 +0000283However you do the testing, once you're satisfied that the new version
284doesn't break anything, you can install it to the system-wide
285third-party module directory as usual:
Greg Ward7c1e5f62000-03-10 01:56:58 +0000286\begin{verbatim}
Greg Ward169f91b2000-03-10 01:57:51 +0000287python setup.py install
Greg Ward7c1e5f62000-03-10 01:56:58 +0000288\end{verbatim}
Greg Ward7c1e5f62000-03-10 01:56:58 +0000289
Greg Ward169f91b2000-03-10 01:57:51 +0000290
291\subsection{Indirect specification: prefix directories}
292\label{sec:prefix-dirs}
293
294Occasionally, you may want to install a module distribution
Greg Ward7c1e5f62000-03-10 01:56:58 +0000295
296
297\section{Custom Installation (Windows)}
298\label{sec:custom-install-windows}
299
300\comingsoon
301
302
303\section{Configuration Files}
304\label{sec:config-files}
305
306\comingsoon
307
308
309
310\end{document}