blob: 020b266ffd829147532b50d4f32caa2839819876 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{site}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{site}
3
4\modulesynopsis{A standard way to reference site-specific modules.}
5
Guido van Rossum571391b1997-04-03 22:41:49 +00006
Guido van Rossum28cad961997-08-30 20:03:28 +00007\strong{This module is automatically imported during initialization.}
Guido van Rossum571391b1997-04-03 22:41:49 +00008
Guido van Rossum28cad961997-08-30 20:03:28 +00009In earlier versions of Python (up to and including 1.5a3), scripts or
10modules that needed to use site-specific modules would place
Fred Drake19479911998-02-13 06:58:54 +000011\samp{import site} somewhere near the top of their code. This is no
Guido van Rossum28cad961997-08-30 20:03:28 +000012longer necessary.
Guido van Rossum571391b1997-04-03 22:41:49 +000013
Guido van Rossumf01dff71997-09-03 22:05:54 +000014This will append site-specific paths to to the module search path.
Fred Drakecf757541998-01-13 18:34:40 +000015\indexiii{module}{search}{path}
Guido van Rossumf01dff71997-09-03 22:05:54 +000016
17It starts by constructing up to four directories from a head and a
18tail part. For the head part, it uses \code{sys.prefix} and
19\code{sys.exec_prefix}; empty heads are skipped. For
Fred Drakeb991f8d1998-03-08 07:09:19 +000020the tail part, it uses the empty string (on Macintosh or Windows) or
21it uses first \file{lib/python\var{version}/site-packages} and then
Fred Drakecf757541998-01-13 18:34:40 +000022\file{lib/site-python} (on \UNIX{}). For each of the distinct
23head-tail combinations, it sees if it refers to an existing directory,
24and if so, adds to \code{sys.path}, and also inspected for path
25configuration files.
Guido van Rossumf01dff71997-09-03 22:05:54 +000026\indexii{site-python}{directory}
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000027\indexii{site-packages}{directory}
Guido van Rossumf01dff71997-09-03 22:05:54 +000028
29A path configuration file is a file whose name has the form
30\file{\var{package}.pth}; its contents are additional items (one
31per line) to be added to \code{sys.path}. Non-existing items are
32never added to \code{sys.path}, but no check is made that the item
33refers to a directory (rather than a file). No item is added to
Guido van Rossum28cad961997-08-30 20:03:28 +000034\code{sys.path} more than once. Blank lines and lines beginning with
35\code{\#} are skipped.
36\index{package}
Guido van Rossumf01dff71997-09-03 22:05:54 +000037\indexiii{path}{configuration}{file}
Guido van Rossum28cad961997-08-30 20:03:28 +000038
39For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are
Fred Drakeb0f77d61998-02-16 20:58:58 +000040set to \file{/usr/local}. The Python \version\ library is then
Fred Drake2de75ec1998-04-09 14:12:11 +000041installed in \file{/usr/local/lib/python1.5} (note that only the first
42three characters of \code{sys.version} are used to form the path
43name). Suppose this has a subdirectory
44\file{/usr/local/lib/python1.5/site-packages} with three
Fred Drakeb0f77d61998-02-16 20:58:58 +000045subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two
46path configuration files, \file{foo.pth} and \file{bar.pth}. Assume
47\file{foo.pth} contains the following:
Guido van Rossum28cad961997-08-30 20:03:28 +000048
Fred Drake19479911998-02-13 06:58:54 +000049\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000050# foo package configuration
51
52foo
53bar
54bletch
Fred Drake19479911998-02-13 06:58:54 +000055\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000056
57and \file{bar.pth} contains:
58
Fred Drake19479911998-02-13 06:58:54 +000059\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000060# bar package configuration
61
62bar
Fred Drake19479911998-02-13 06:58:54 +000063\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000064
Fred Drakecf757541998-01-13 18:34:40 +000065Then the following directories are added to \code{sys.path}, in this
66order:
Guido van Rossum28cad961997-08-30 20:03:28 +000067
Fred Drake19479911998-02-13 06:58:54 +000068\begin{verbatim}
Guido van Rossum505d80b1998-02-19 21:02:32 +000069/usr/local/lib/python1.5/site-packages/bar
70/usr/local/lib/python1.5/site-packages/foo
Fred Drake19479911998-02-13 06:58:54 +000071\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000072
73Note that \file{bletch} is omitted because it doesn't exist; the
74\file{bar} directory precedes the \file{foo} directory because
75\file{bar.pth} comes alphabetically before \file{foo.pth}; and
76\file{spam} is omitted because it is not mentioned in either path
77configuration file.
Guido van Rossum571391b1997-04-03 22:41:49 +000078
79After these path manipulations, an attempt is made to import a module
Fred Drakeb991f8d1998-03-08 07:09:19 +000080named \module{sitecustomize}\refmodindex{sitecustomize}, which can
Fred Drakecf757541998-01-13 18:34:40 +000081perform arbitrary site-specific customizations. If this import fails
Fred Drakeb991f8d1998-03-08 07:09:19 +000082with an \exception{ImportError} exception, it is silently ignored.
Guido van Rossum571391b1997-04-03 22:41:49 +000083
Fred Drakecf757541998-01-13 18:34:40 +000084Note that for some non-\UNIX{} systems, \code{sys.prefix} and
Guido van Rossum571391b1997-04-03 22:41:49 +000085\code{sys.exec_prefix} are empty, and the path manipulations are
Fred Drakecf757541998-01-13 18:34:40 +000086skipped; however the import of
Fred Drakeb991f8d1998-03-08 07:09:19 +000087\module{sitecustomize}\refmodindex{sitecustomize} is still attempted.