blob: 760101a855271a4d1b0fce08f3ed8a08745a5699 [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
Fred Drake19479911998-02-13 06:58:54 +00009\samp{import site} somewhere near the top of their code. This is no
Guido van Rossum28cad961997-08-30 20:03:28 +000010longer 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
Fred Drakeb0f77d61998-02-16 20:58:58 +000038set to \file{/usr/local}. The Python \version\ library is then
39installed in \file{/usr/local/lib/python\version}. Suppose this has a
Guido van Rossum505d80b1998-02-19 21:02:32 +000040subdirectory \file{/usr/local/lib/python\version/site-packages} with three
Fred Drakeb0f77d61998-02-16 20:58:58 +000041subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two
42path configuration files, \file{foo.pth} and \file{bar.pth}. Assume
43\file{foo.pth} contains the following:
Guido van Rossum28cad961997-08-30 20:03:28 +000044
Fred Drake19479911998-02-13 06:58:54 +000045\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000046# foo package configuration
47
48foo
49bar
50bletch
Fred Drake19479911998-02-13 06:58:54 +000051\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000052
53and \file{bar.pth} contains:
54
Fred Drake19479911998-02-13 06:58:54 +000055\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000056# bar package configuration
57
58bar
Fred Drake19479911998-02-13 06:58:54 +000059\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000060
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
Fred Drake19479911998-02-13 06:58:54 +000064\begin{verbatim}
Guido van Rossum505d80b1998-02-19 21:02:32 +000065/usr/local/lib/python1.5/site-packages/bar
66/usr/local/lib/python1.5/site-packages/foo
Fred Drake19479911998-02-13 06:58:54 +000067\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000068
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.