blob: 13a4909938da866d2fe9577607d7ecdddc4f7190 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{site} ---
Fred Drakebbac4321999-02-20 00:14:17 +00002 Site-specific configuration hook}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakebbac4321999-02-20 00:14:17 +00004\declaremodule{standard}{site}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{A standard way to reference site-specific modules.}
6
Guido van Rossum571391b1997-04-03 22:41:49 +00007
Guido van Rossum28cad961997-08-30 20:03:28 +00008\strong{This module is automatically imported during initialization.}
Fred Drakeff381e12003-12-30 22:51:32 +00009The automatic import can be suppressed using the interpreter's
10\programopt{-S} option.
Guido van Rossum571391b1997-04-03 22:41:49 +000011
Fred Drakeff381e12003-12-30 22:51:32 +000012Importing this module will append site-specific paths to the module
13search path.
Fred Drakecf757541998-01-13 18:34:40 +000014\indexiii{module}{search}{path}
Guido van Rossumf01dff71997-09-03 22:05:54 +000015
16It starts by constructing up to four directories from a head and a
17tail part. For the head part, it uses \code{sys.prefix} and
18\code{sys.exec_prefix}; empty heads are skipped. For
Fred Drakeb991f8d1998-03-08 07:09:19 +000019the tail part, it uses the empty string (on Macintosh or Windows) or
Fred Draked5d04352000-09-14 20:24:17 +000020it uses first \file{lib/python\shortversion/site-packages} and then
Fred Drakec37b65e2001-11-28 07:26:15 +000021\file{lib/site-python} (on \UNIX). For each of the distinct
Fred Drakecf757541998-01-13 18:34:40 +000022head-tail combinations, it sees if it refers to an existing directory,
Andrew M. Kuchlingd6d7cfa2002-12-30 03:08:27 +000023and if so, adds it to \code{sys.path} and also inspects the newly added
24path for configuration files.
Guido van Rossumf01dff71997-09-03 22:05:54 +000025\indexii{site-python}{directory}
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000026\indexii{site-packages}{directory}
Guido van Rossumf01dff71997-09-03 22:05:54 +000027
28A path configuration file is a file whose name has the form
Brett Cannon508c57d2004-03-20 21:41:28 +000029\file{\var{package}.pth} and exists in one of the four directories
30mentioned above; its contents are additional items (one
Guido van Rossumf01dff71997-09-03 22:05:54 +000031per 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
Martin v. Löwisa177c402001-01-11 22:07:25 +000035\code{\#} are skipped. Lines starting with \code{import} are executed.
Guido van Rossum28cad961997-08-30 20:03:28 +000036\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 Draked5d04352000-09-14 20:24:17 +000041installed in \file{/usr/local/lib/python\shortversion} (where only the
42first three characters of \code{sys.version} are used to form the
43installation path name). Suppose this has a subdirectory
44\file{/usr/local/lib/python\shortversion/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}
Neal Norwitzba902fd2002-02-19 02:58:54 +000069/usr/local/lib/python2.3/site-packages/bar
70/usr/local/lib/python2.3/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.