blob: f81841ef16d2f536893c56a48a1da0226c3486aa [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.}
Guido van Rossum571391b1997-04-03 22:41:49 +00009
Guido van Rossum28cad961997-08-30 20:03:28 +000010In earlier versions of Python (up to and including 1.5a3), scripts or
11modules that needed to use site-specific modules would place
Fred Drake19479911998-02-13 06:58:54 +000012\samp{import site} somewhere near the top of their code. This is no
Guido van Rossum28cad961997-08-30 20:03:28 +000013longer necessary.
Guido van Rossum571391b1997-04-03 22:41:49 +000014
Fred Drake24aca831999-04-23 20:33:59 +000015This will append site-specific paths to the module search path.
Fred Drakecf757541998-01-13 18:34:40 +000016\indexiii{module}{search}{path}
Guido van Rossumf01dff71997-09-03 22:05:54 +000017
18It starts by constructing up to four directories from a head and a
19tail part. For the head part, it uses \code{sys.prefix} and
20\code{sys.exec_prefix}; empty heads are skipped. For
Fred Drakeb991f8d1998-03-08 07:09:19 +000021the tail part, it uses the empty string (on Macintosh or Windows) or
Fred Draked5d04352000-09-14 20:24:17 +000022it uses first \file{lib/python\shortversion/site-packages} and then
Fred Drakecf757541998-01-13 18:34:40 +000023\file{lib/site-python} (on \UNIX{}). For each of the distinct
24head-tail combinations, it sees if it refers to an existing directory,
Fred Drake2c4f5542000-10-10 22:00:03 +000025and if so, adds to \code{sys.path}, and also inspects the path for
Fred Drakecf757541998-01-13 18:34:40 +000026configuration files.
Guido van Rossumf01dff71997-09-03 22:05:54 +000027\indexii{site-python}{directory}
Guido van Rossum9cf4e2b1997-09-08 02:02:37 +000028\indexii{site-packages}{directory}
Guido van Rossumf01dff71997-09-03 22:05:54 +000029
30A path configuration file is a file whose name has the form
31\file{\var{package}.pth}; its contents are additional items (one
32per line) to be added to \code{sys.path}. Non-existing items are
33never added to \code{sys.path}, but no check is made that the item
34refers to a directory (rather than a file). No item is added to
Guido van Rossum28cad961997-08-30 20:03:28 +000035\code{sys.path} more than once. Blank lines and lines beginning with
Martin v. Löwisa177c402001-01-11 22:07:25 +000036\code{\#} are skipped. Lines starting with \code{import} are executed.
Guido van Rossum28cad961997-08-30 20:03:28 +000037\index{package}
Guido van Rossumf01dff71997-09-03 22:05:54 +000038\indexiii{path}{configuration}{file}
Guido van Rossum28cad961997-08-30 20:03:28 +000039
40For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are
Fred Drakeb0f77d61998-02-16 20:58:58 +000041set to \file{/usr/local}. The Python \version\ library is then
Fred Draked5d04352000-09-14 20:24:17 +000042installed in \file{/usr/local/lib/python\shortversion} (where only the
43first three characters of \code{sys.version} are used to form the
44installation path name). Suppose this has a subdirectory
45\file{/usr/local/lib/python\shortversion/site-packages} with three
Fred Drakeb0f77d61998-02-16 20:58:58 +000046subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two
47path configuration files, \file{foo.pth} and \file{bar.pth}. Assume
48\file{foo.pth} contains the following:
Guido van Rossum28cad961997-08-30 20:03:28 +000049
Fred Drake19479911998-02-13 06:58:54 +000050\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000051# foo package configuration
52
53foo
54bar
55bletch
Fred Drake19479911998-02-13 06:58:54 +000056\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000057
58and \file{bar.pth} contains:
59
Fred Drake19479911998-02-13 06:58:54 +000060\begin{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000061# bar package configuration
62
63bar
Fred Drake19479911998-02-13 06:58:54 +000064\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000065
Fred Drakecf757541998-01-13 18:34:40 +000066Then the following directories are added to \code{sys.path}, in this
67order:
Guido van Rossum28cad961997-08-30 20:03:28 +000068
Fred Drake19479911998-02-13 06:58:54 +000069\begin{verbatim}
Guido van Rossum505d80b1998-02-19 21:02:32 +000070/usr/local/lib/python1.5/site-packages/bar
71/usr/local/lib/python1.5/site-packages/foo
Fred Drake19479911998-02-13 06:58:54 +000072\end{verbatim}
Guido van Rossum28cad961997-08-30 20:03:28 +000073
74Note that \file{bletch} is omitted because it doesn't exist; the
75\file{bar} directory precedes the \file{foo} directory because
76\file{bar.pth} comes alphabetically before \file{foo.pth}; and
77\file{spam} is omitted because it is not mentioned in either path
78configuration file.
Guido van Rossum571391b1997-04-03 22:41:49 +000079
80After these path manipulations, an attempt is made to import a module
Fred Drakeb991f8d1998-03-08 07:09:19 +000081named \module{sitecustomize}\refmodindex{sitecustomize}, which can
Fred Drakecf757541998-01-13 18:34:40 +000082perform arbitrary site-specific customizations. If this import fails
Fred Drakeb991f8d1998-03-08 07:09:19 +000083with an \exception{ImportError} exception, it is silently ignored.
Guido van Rossum571391b1997-04-03 22:41:49 +000084
Fred Drakecf757541998-01-13 18:34:40 +000085Note that for some non-\UNIX{} systems, \code{sys.prefix} and
Guido van Rossum571391b1997-04-03 22:41:49 +000086\code{sys.exec_prefix} are empty, and the path manipulations are
Fred Drakecf757541998-01-13 18:34:40 +000087skipped; however the import of
Fred Drakeb991f8d1998-03-08 07:09:19 +000088\module{sitecustomize}\refmodindex{sitecustomize} is still attempted.