blob: 11858d1cf70d6c43462f65dc79909ee0e02aba76 [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000019the tail part, it uses the empty string and then
20\file{lib/site-packages} (on Windows) or
21\file{lib/python\shortversion/site-packages} and then
22\file{lib/site-python} (on \UNIX{} and Macintosh). For each of the
23distinct head-tail combinations, it sees if it refers to an existing
24directory, and if so, adds it to \code{sys.path} and also inspects
25the newly added path for configuration files.
Guido van Rossumf01dff71997-09-03 22:05:54 +000026\indexii{site-python}{directory}
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000027\indexii{site-packages}{directory}
Guido van Rossumf01dff71997-09-03 22:05:54 +000028
29A path configuration file is a file whose name has the form
Brett Cannon508c57d2004-03-20 21:41:28 +000030\file{\var{package}.pth} and exists in one of the four directories
Guido van Rossumd8faa362007-04-27 19:54:29 +000031mentioned above; its contents are additional items (one per line) to
32be added to \code{sys.path}. Non-existing items are never added to
33\code{sys.path}, but no check is made that the item refers to a
34directory (rather than a file). No item is added to \code{sys.path}
35more than once. Blank lines and lines beginning with \code{\#} are
36skipped. Lines starting with \code{import} (followed by space or tab)
37are executed.
38
39\versionchanged[A space or tab is now required after the import
40keyword]{2.6}
41
Guido van Rossum28cad961997-08-30 20:03:28 +000042\index{package}
Guido van Rossumf01dff71997-09-03 22:05:54 +000043\indexiii{path}{configuration}{file}
Guido van Rossum28cad961997-08-30 20:03:28 +000044
45For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are
Fred Drakeb0f77d61998-02-16 20:58:58 +000046set to \file{/usr/local}. The Python \version\ library is then
Fred Draked5d04352000-09-14 20:24:17 +000047installed in \file{/usr/local/lib/python\shortversion} (where only the
48first three characters of \code{sys.version} are used to form the
49installation path name). Suppose this has a subdirectory
50\file{/usr/local/lib/python\shortversion/site-packages} with three
Fred Drakeb0f77d61998-02-16 20:58:58 +000051subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two
52path configuration files, \file{foo.pth} and \file{bar.pth}. Assume
53\file{foo.pth} contains the following:
Guido van Rossum28cad961997-08-30 20:03:28 +000054
Fred Drake19479911998-02-13 06:58:54 +000055\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000056# foo package configuration
57
58foo
59bar
60bletch
Fred Drake19479911998-02-13 06:58:54 +000061\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000062
63and \file{bar.pth} contains:
64
Fred Drake19479911998-02-13 06:58:54 +000065\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000066# bar package configuration
67
68bar
Fred Drake19479911998-02-13 06:58:54 +000069\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000070
Fred Drakecf757541998-01-13 18:34:40 +000071Then the following directories are added to \code{sys.path}, in this
72order:
Guido van Rossum28cad961997-08-30 20:03:28 +000073
Fred Drake19479911998-02-13 06:58:54 +000074\begin{verbatim}
Neal Norwitzba902fd2002-02-19 02:58:54 +000075/usr/local/lib/python2.3/site-packages/bar
76/usr/local/lib/python2.3/site-packages/foo
Fred Drake19479911998-02-13 06:58:54 +000077\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000078
79Note that \file{bletch} is omitted because it doesn't exist; the
80\file{bar} directory precedes the \file{foo} directory because
81\file{bar.pth} comes alphabetically before \file{foo.pth}; and
82\file{spam} is omitted because it is not mentioned in either path
83configuration file.
Guido van Rossum571391b1997-04-03 22:41:49 +000084
85After these path manipulations, an attempt is made to import a module
Fred Drakeb991f8d1998-03-08 07:09:19 +000086named \module{sitecustomize}\refmodindex{sitecustomize}, which can
Fred Drakecf757541998-01-13 18:34:40 +000087perform arbitrary site-specific customizations. If this import fails
Fred Drakeb991f8d1998-03-08 07:09:19 +000088with an \exception{ImportError} exception, it is silently ignored.
Guido van Rossum571391b1997-04-03 22:41:49 +000089
Fred Drakecf757541998-01-13 18:34:40 +000090Note that for some non-\UNIX{} systems, \code{sys.prefix} and
Guido van Rossum571391b1997-04-03 22:41:49 +000091\code{sys.exec_prefix} are empty, and the path manipulations are
Fred Drakecf757541998-01-13 18:34:40 +000092skipped; however the import of
Fred Drakeb991f8d1998-03-08 07:09:19 +000093\module{sitecustomize}\refmodindex{sitecustomize} is still attempted.