blob: 43fad3c181461f76b92860d9936a7037d3d482ad [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.
13
14It starts by constructing up to four directories from a head and a
15tail part. For the head part, it uses \code{sys.prefix} and
16\code{sys.exec_prefix}; empty heads are skipped. For
17the tail part, it uses the empty string (on Mac or Windows) or it uses
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000018first \file{lib/python\var{version}/site-packages} and then
Guido van Rossumf01dff71997-09-03 22:05:54 +000019\file{lib/site-python} (on Unix). For each of the distinct head-tail
20combinations, it sees if it refers to an existing directory, and if
21so, adds to \code{sys.path}, and also inspected for path configuration
22files.
23\indexii{site-python}{directory}
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000024\indexii{site-packages}{directory}
Guido van Rossumf01dff71997-09-03 22:05:54 +000025
26A path configuration file is a file whose name has the form
27\file{\var{package}.pth}; its contents are additional items (one
28per line) to be added to \code{sys.path}. Non-existing items are
29never added to \code{sys.path}, but no check is made that the item
30refers to a directory (rather than a file). No item is added to
Guido van Rossum28cad961997-08-30 20:03:28 +000031\code{sys.path} more than once. Blank lines and lines beginning with
32\code{\#} are skipped.
33\index{package}
Guido van Rossumf01dff71997-09-03 22:05:54 +000034\indexiii{path}{configuration}{file}
Guido van Rossum28cad961997-08-30 20:03:28 +000035\kwindex{sys.prefix}
36\kwindex{sys.exec_prefix}
37\kwindex{prefix}
38\kwindex{exec_prefix}
39
40For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are
41set to \file{/usr/local}. The Python 1.5 library is then installed in
42\file{/usr/local/lib/python1.5}. Suppose this has a subdirectory
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000043\file{/usr/local/python1.5/site-packages} with three subsubdirectories,
Guido van Rossum28cad961997-08-30 20:03:28 +000044\file{foo}, \file{bar} and \file{spam}, and two path configuration
45files, \file{foo.pth} and \file{bar.pth}. Assume \file{foo.pth}
46contains the following:
47
48\bcode\begin{verbatim}
49# foo package configuration
50
51foo
52bar
53bletch
54\end{verbatim}\ecode
55
56and \file{bar.pth} contains:
57
58\bcode\begin{verbatim}
59# bar package configuration
60
61bar
62\end{verbatim}\ecode
63
64Then the following directories are added to sys.path, in this order:
65
66\bcode\begin{verbatim}
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000067/usr/local/python1.5/site-packages/bar
68/usr/local/python1.5/site-packages/foo
Guido van Rossum28cad961997-08-30 20:03:28 +000069\end{verbatim}\ecode
70
71Note that \file{bletch} is omitted because it doesn't exist; the
72\file{bar} directory precedes the \file{foo} directory because
73\file{bar.pth} comes alphabetically before \file{foo.pth}; and
74\file{spam} is omitted because it is not mentioned in either path
75configuration file.
Guido van Rossum571391b1997-04-03 22:41:49 +000076
77After these path manipulations, an attempt is made to import a module
78named \code{sitecustomize}, which can perform arbitrary site-specific
79customizations. If this import fails with an \code{ImportError}
Guido van Rossum28cad961997-08-30 20:03:28 +000080exception, it is silently ignored.
81\stmodindex{sitecustomize}
Guido van Rossum571391b1997-04-03 22:41:49 +000082
Guido van Rossum28cad961997-08-30 20:03:28 +000083Note that for some non-Unix systems, \code{sys.prefix} and
Guido van Rossum571391b1997-04-03 22:41:49 +000084\code{sys.exec_prefix} are empty, and the path manipulations are
85skipped; however the import of \code{sitecustomize} is still attempted.