blob: 7e80b08a6a99e2ee6cc07baa608549cd7ab9ecc3 [file] [log] [blame]
Guido van Rossum571391b1997-04-03 22:41:49 +00001\section{Standard Module \sectcode{site}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-site}
Guido van Rossum571391b1997-04-03 22:41:49 +00003\stmodindex{site}
4
Guido van Rossum28cad961997-08-30 20:03:28 +00005\strong{This module is automatically imported during initialization.}
Guido van Rossum571391b1997-04-03 22:41:49 +00006
Guido van Rossum28cad961997-08-30 20:03:28 +00007In earlier versions of Python (up to and including 1.5a3), scripts or
8modules that needed to use site-specific modules would place
9\code{import site} somewhere near the top of their code. This is no
10longer necessary.
Guido van Rossum571391b1997-04-03 22:41:49 +000011
Guido van Rossumf01dff71997-09-03 22:05:54 +000012This will append site-specific paths to to the module search path.
Fred Drakecf757541998-01-13 18:34:40 +000013\indexiii{module}{search}{path}
Guido van Rossumf01dff71997-09-03 22:05:54 +000014
15It starts by constructing up to four directories from a head and a
16tail part. For the head part, it uses \code{sys.prefix} and
17\code{sys.exec_prefix}; empty heads are skipped. For
18the tail part, it uses the empty string (on Mac or Windows) or it uses
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000019first \file{lib/python\var{version}/site-packages} and then
Fred Drakecf757541998-01-13 18:34:40 +000020\file{lib/site-python} (on \UNIX{}). For each of the distinct
21head-tail combinations, it sees if it refers to an existing directory,
22and if so, adds to \code{sys.path}, and also inspected for path
23configuration files.
Guido van Rossumf01dff71997-09-03 22:05:54 +000024\indexii{site-python}{directory}
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000025\indexii{site-packages}{directory}
Guido van Rossumf01dff71997-09-03 22:05:54 +000026
27A path configuration file is a file whose name has the form
28\file{\var{package}.pth}; its contents are additional items (one
29per line) to be added to \code{sys.path}. Non-existing items are
30never added to \code{sys.path}, but no check is made that the item
31refers to a directory (rather than a file). No item is added to
Guido van Rossum28cad961997-08-30 20:03:28 +000032\code{sys.path} more than once. Blank lines and lines beginning with
33\code{\#} are skipped.
34\index{package}
Guido van Rossumf01dff71997-09-03 22:05:54 +000035\indexiii{path}{configuration}{file}
Guido van Rossum28cad961997-08-30 20:03:28 +000036
37For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are
38set to \file{/usr/local}. The Python 1.5 library is then installed in
39\file{/usr/local/lib/python1.5}. Suppose this has a subdirectory
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000040\file{/usr/local/python1.5/site-packages} with three subsubdirectories,
Guido van Rossum28cad961997-08-30 20:03:28 +000041\file{foo}, \file{bar} and \file{spam}, and two path configuration
42files, \file{foo.pth} and \file{bar.pth}. Assume \file{foo.pth}
43contains the following:
44
45\bcode\begin{verbatim}
46# foo package configuration
47
48foo
49bar
50bletch
51\end{verbatim}\ecode
52
53and \file{bar.pth} contains:
54
55\bcode\begin{verbatim}
56# bar package configuration
57
58bar
59\end{verbatim}\ecode
60
Fred Drakecf757541998-01-13 18:34:40 +000061Then the following directories are added to \code{sys.path}, in this
62order:
Guido van Rossum28cad961997-08-30 20:03:28 +000063
64\bcode\begin{verbatim}
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000065/usr/local/python1.5/site-packages/bar
66/usr/local/python1.5/site-packages/foo
Guido van Rossum28cad961997-08-30 20:03:28 +000067\end{verbatim}\ecode
68
69Note that \file{bletch} is omitted because it doesn't exist; the
70\file{bar} directory precedes the \file{foo} directory because
71\file{bar.pth} comes alphabetically before \file{foo.pth}; and
72\file{spam} is omitted because it is not mentioned in either path
73configuration file.
Guido van Rossum571391b1997-04-03 22:41:49 +000074
75After these path manipulations, an attempt is made to import a module
Fred Drakecf757541998-01-13 18:34:40 +000076named \code{sitecustomize}\refmodindex{sitecustomize}, which can
77perform arbitrary site-specific customizations. If this import fails
78with an \code{ImportError} exception, it is silently ignored.
Guido van Rossum571391b1997-04-03 22:41:49 +000079
Fred Drakecf757541998-01-13 18:34:40 +000080Note that for some non-\UNIX{} systems, \code{sys.prefix} and
Guido van Rossum571391b1997-04-03 22:41:49 +000081\code{sys.exec_prefix} are empty, and the path manipulations are
Fred Drakecf757541998-01-13 18:34:40 +000082skipped; however the import of
83\code{sitecustomize}\refmodindex{sitecustomize} is still attempted.