Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 1 | \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 Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 30 | \newcommand{\option}[1]{\textsf{\small{#1}}} |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 31 | \newcommand{\filevar}[1]{{\textsl{\filenq{#1}}}} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 32 | \newcommand{\homefile}[1]{\file{\tilde/#1}} |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 33 | \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 | |
| 105 | A \dfn{custom installation} is where you install modules to a location |
| 106 | that's not in Python's default module search path. There are a couple |
| 107 | of reasons you might want to do this; the most typical is simply that |
| 108 | you don't have permission to write to the standard Python library |
| 109 | directory. Or, even if you do have write permission to the standard |
| 110 | library, you might wish to install a module distribution into a |
| 111 | non-standard place for testing or experimentation. (This is especially |
| 112 | useful when upgrading an existing module distribution: you might want to |
| 113 | make sure that your existing scripts continue to work as before, and |
| 114 | only then install the upgrade ``for real.'') |
| 115 | |
| 116 | (XXX terminology: I keep saying ``standard Python library directory'' |
| 117 | when I really mean ``the site-packages directory under the standard |
| 118 | Python library directory''. Is there a better way?) |
| 119 | |
| 120 | In any event, you can easily install to non-standard locations with a |
| 121 | couple of options to the \command{install} command: |
| 122 | |
| 123 | \begin{tableii}{ll}{option}{Option}{Description} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 124 | \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 Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 137 | \end{tableii} |
| 138 | |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 139 | Of these, the most commonly used will probably be \option{install-lib} |
| 140 | and \option{install-platlib}: you use them to point module installation |
| 141 | right at a particular directory. (You'll only need |
| 142 | \option{install-platlib} if you maintain a multi-platform installation, |
| 143 | which is often done on \UNIX{} networks with different architectures and |
| 144 | operating systems.) The two prefix options are intended for the |
| 145 | somewhat arcane purpose of installing modules into a different Python |
| 146 | installation than the Python binary used to perform the installation. |
| 147 | The last, \option{install-path}, is mainly used for module developers to |
| 148 | ensure that their module will go into a directory of their own, but it |
| 149 | may occasionally be useful to you as a module installer. |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 150 | |
| 151 | |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 152 | \subsection{Directly specifying installation directories} |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 153 | \label{sec:install-dirs} |
| 154 | |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 155 | The most common type of custom module installation is where you maintain |
| 156 | a personal stash of Python modules under your home directory, say in |
| 157 | \homefile{lib/python}. If you only care about a single platform |
| 158 | there, then you only need to specify the \option{install-lib} option and |
| 159 | can forget about \option{install-platlib}: |
| 160 | \begin{verbatim} |
| 161 | python setup.py install --install-lib=~/lib/python |
| 162 | \end{verbatim} |
| 163 | You can, of course, supply whatever directory you like in place of |
| 164 | \homefile{lib/python}. More importantly, you can specify this |
| 165 | directory permanently in your personal configuration file (XXX |
| 166 | filename?): |
| 167 | \begin{verbatim} |
| 168 | [install] |
| 169 | install-lib=~/lib/python |
| 170 | \end{verbatim} |
| 171 | Note that use of shell-style tilde and environment variable expansion is |
| 172 | supported both on the command line and in configuration files. (See |
| 173 | section~\ref{sec:config-files} for more information on configuration |
| 174 | files.) |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 175 | |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 176 | Of course, in order for this personal Python library scheme to work, you |
| 177 | have to ensure that \homefile{lib/python} is in \code{sys.path} when you |
| 178 | run Python. The easiest way to do this under \UNIX{} is to add it to |
| 179 | your \code{PYTHONPATH} environment variable when you login. For |
| 180 | example, if you use a Bourne shell derivative such as bash, zsh, or ksh, |
| 181 | add the following to your \homefile{.profile} (or \homefile{.bashrc}, or |
| 182 | \homefile{.zshenv}, depending on your shell and personal preferences): |
| 183 | \begin{verbatim} |
| 184 | export PYTHONPATH=$HOME/lib/python |
| 185 | \end{verbatim} |
| 186 | If you use a csh-derivative such as tcsh, add the following to your |
| 187 | \homefile{.cshrc}: |
| 188 | \begin{verbatim} |
| 189 | setenv PYTHONPATH $HOME/lib/python |
| 190 | \end{verbatim} |
| 191 | |
| 192 | If you use multiple platforms (architectures and/or operating systems) |
| 193 | from the same home directory, then you probably want to maintain a |
| 194 | multi-platform personal Python library. One possible scheme is to put |
| 195 | platform-neutral (pure Python) distributions in \homefile{lib/python} |
| 196 | and platform-specific distributions (any that containe extension |
| 197 | modules) in \homefile{lib/python.\filevar{plat}}: |
| 198 | \begin{verbatim} |
| 199 | python setup.py install --install-lib=~/lib/python \ |
| 200 | --install-lib-plat=~/lib/python.plat \ |
| 201 | \end{verbatim} |
| 202 | On the command line, of course, you can just type in the current |
| 203 | platform in place of \filevar{plat}: \file{linux-x86}, |
| 204 | \file{solaris-sparc}, \file{linux-alpha}, whatever. That's not an |
| 205 | option in a configuration file, though---the same file has to cover all |
| 206 | platforms for which you maintain a personal Python library. So the |
| 207 | Distutils provide a \code{PLAT} environment variable which will expand |
| 208 | to the current platform name: |
| 209 | \begin{verbatim} |
| 210 | [install] |
| 211 | install-lib=~/lib/python |
| 212 | install-platlib=~/lib/python.$PLAT |
| 213 | \end{verbatim} |
| 214 | (If \code{PLAT} is already defined in your environment, the Distutils |
| 215 | won't override it: that way you can maintain consistency with other |
| 216 | applications that look for a \code{PLAT} variable; this is especially |
| 217 | useful when you refer to \code{PLAT} in your login scripts, as explained |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 218 | below.) |
| 219 | |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 220 | (XXX danger danger! this environment-variable-in-config-file thing is |
| 221 | frighteningly make-like: is there any way to avoid it?) |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 222 | |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 223 | Again, you have to make sure that your personal Python library appears |
| 224 | in \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 |
| 226 | be sure to set \emph{both} directories (platform-neutral and the current |
| 227 | platform-specific directory). For Bourne-shell derivatives: |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 228 | \begin{verbatim} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 229 | export PYTHONPATH=$HOME/lib/python:$HOME/lib/python.$PLAT |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 230 | \end{verbatim} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 231 | and for csh-derivatives: |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 232 | \begin{verbatim} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 233 | setenv PYTHONPATH $HOME/lib/python:$HOME/lib/python.$PLAT |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 234 | \end{verbatim} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 235 | Note that it is your responsibility to set the \code{PATH} environment |
| 236 | variable (unless your system administrator has kindly taken care of it |
| 237 | in the system-wide login scripts, which is a wise thing to do on |
| 238 | multi-platform networks). One way to do this is with the \code{uname} |
| 239 | command: |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 240 | \begin{verbatim} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 241 | export 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} |
| 244 | becomes eg. \code{i586}, which is not the \emph{machine} but the |
| 245 | \emph{processor}. Arggh!) |
| 246 | |
| 247 | Of course, there are more reasons to do custom installation than |
| 248 | maintaining a personal Python library. Even if you have write access to |
| 249 | the 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 |
| 252 | might want to install new module distributions---especially upgrades of |
| 253 | modules that are crucial to your local infrastructure---to a temporary |
| 254 | location, in order to test them before installing them ``for real.'' |
| 255 | This is fundamentally no different from installing to your home |
| 256 | directory, except that you probably won't bother to set |
| 257 | \code{PYTHONPATH} permanently. For example, to install a module |
| 258 | distribution to \file{/tmp/pylib}: |
| 259 | \begin{verbatim} |
| 260 | python setup.py install --install-lib=/tmp/pylib |
| 261 | \end{verbatim} |
| 262 | Then, of course, you'll want to run some script that depends on these |
| 263 | modules to make sure that they still work with your installed base of |
| 264 | code: |
| 265 | \begin{verbatim} |
| 266 | env PYTHONPATH=/tmp/pylib python /usr/local/bin/crucial_script ... |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 267 | \end{verbatim} |
| 268 | |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 269 | Of course, you can do this temporary installation with separate |
| 270 | \option{install-lib} and \option{install-platlib} options. If you're |
| 271 | doing this to a network-wide directory, not \file{/tmp}, this might be |
| 272 | essential. As you might have guessed, it's not too hard: |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 273 | \begin{verbatim} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 274 | python setup.py install --install-lib=/scratch/pylib \ |
| 275 | --install-platlib=/scratch/pylib.plat |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 276 | \end{verbatim} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 277 | and then, testing your crucial scripts on multiple platforms: |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 278 | \begin{verbatim} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 279 | env PYTHONPATH=/scratch/pylib:/scratch/pylib.plat \ |
| 280 | python /usr/local/bin/crucial_script ... |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 281 | \end{verbatim} |
| 282 | |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 283 | However you do the testing, once you're satisfied that the new version |
| 284 | doesn't break anything, you can install it to the system-wide |
| 285 | third-party module directory as usual: |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 286 | \begin{verbatim} |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 287 | python setup.py install |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 288 | \end{verbatim} |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 289 | |
Greg Ward | 169f91b | 2000-03-10 01:57:51 +0000 | [diff] [blame] | 290 | |
| 291 | \subsection{Indirect specification: prefix directories} |
| 292 | \label{sec:prefix-dirs} |
| 293 | |
| 294 | Occasionally, you may want to install a module distribution |
Greg Ward | 7c1e5f6 | 2000-03-10 01:56:58 +0000 | [diff] [blame] | 295 | |
| 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} |