blob: 70f34b8fc4a4f68dca4b4c32f24705d78899235a [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
29\file{\var{package}.pth}; its contents are additional items (one
30per line) to be added to \code{sys.path}. Non-existing items are
31never added to \code{sys.path}, but no check is made that the item
32refers to a directory (rather than a file). No item is added to
Guido van Rossum28cad961997-08-30 20:03:28 +000033\code{sys.path} more than once. Blank lines and lines beginning with
Martin v. Löwisa177c402001-01-11 22:07:25 +000034\code{\#} are skipped. Lines starting with \code{import} are executed.
Guido van Rossum28cad961997-08-30 20:03:28 +000035\index{package}
Guido van Rossumf01dff71997-09-03 22:05:54 +000036\indexiii{path}{configuration}{file}
Guido van Rossum28cad961997-08-30 20:03:28 +000037
38For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are
Fred Drakeb0f77d61998-02-16 20:58:58 +000039set to \file{/usr/local}. The Python \version\ library is then
Fred Draked5d04352000-09-14 20:24:17 +000040installed in \file{/usr/local/lib/python\shortversion} (where only the
41first three characters of \code{sys.version} are used to form the
42installation path name). Suppose this has a subdirectory
43\file{/usr/local/lib/python\shortversion/site-packages} with three
Fred Drakeb0f77d61998-02-16 20:58:58 +000044subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two
45path configuration files, \file{foo.pth} and \file{bar.pth}. Assume
46\file{foo.pth} contains the following:
Guido van Rossum28cad961997-08-30 20:03:28 +000047
Fred Drake19479911998-02-13 06:58:54 +000048\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000049# foo package configuration
50
51foo
52bar
53bletch
Fred Drake19479911998-02-13 06:58:54 +000054\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000055
56and \file{bar.pth} contains:
57
Fred Drake19479911998-02-13 06:58:54 +000058\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000059# bar package configuration
60
61bar
Fred Drake19479911998-02-13 06:58:54 +000062\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000063
Fred Drakecf757541998-01-13 18:34:40 +000064Then the following directories are added to \code{sys.path}, in this
65order:
Guido van Rossum28cad961997-08-30 20:03:28 +000066
Fred Drake19479911998-02-13 06:58:54 +000067\begin{verbatim}
Neal Norwitzba902fd2002-02-19 02:58:54 +000068/usr/local/lib/python2.3/site-packages/bar
69/usr/local/lib/python2.3/site-packages/foo
Fred Drake19479911998-02-13 06:58:54 +000070\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000071
72Note that \file{bletch} is omitted because it doesn't exist; the
73\file{bar} directory precedes the \file{foo} directory because
74\file{bar.pth} comes alphabetically before \file{foo.pth}; and
75\file{spam} is omitted because it is not mentioned in either path
76configuration file.
Guido van Rossum571391b1997-04-03 22:41:49 +000077
78After these path manipulations, an attempt is made to import a module
Fred Drakeb991f8d1998-03-08 07:09:19 +000079named \module{sitecustomize}\refmodindex{sitecustomize}, which can
Fred Drakecf757541998-01-13 18:34:40 +000080perform arbitrary site-specific customizations. If this import fails
Fred Drakeb991f8d1998-03-08 07:09:19 +000081with an \exception{ImportError} exception, it is silently ignored.
Guido van Rossum571391b1997-04-03 22:41:49 +000082
Fred Drakecf757541998-01-13 18:34:40 +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
Fred Drakecf757541998-01-13 18:34:40 +000085skipped; however the import of
Fred Drakeb991f8d1998-03-08 07:09:19 +000086\module{sitecustomize}\refmodindex{sitecustomize} is still attempted.