1998-04-04 03:23:21 -04:00
|
|
|
\section{Standard Module \module{site}}
|
1997-07-17 13:34:52 -03:00
|
|
|
\label{module-site}
|
1997-04-03 18:41:49 -04:00
|
|
|
\stmodindex{site}
|
|
|
|
|
1997-08-30 17:03:28 -03:00
|
|
|
\strong{This module is automatically imported during initialization.}
|
|
|
|
|
|
|
|
In earlier versions of Python (up to and including 1.5a3), scripts or
|
|
|
|
modules that needed to use site-specific modules would place
|
1998-02-13 02:58:54 -04:00
|
|
|
\samp{import site} somewhere near the top of their code. This is no
|
1997-08-30 17:03:28 -03:00
|
|
|
longer necessary.
|
|
|
|
|
1997-09-03 19:05:54 -03:00
|
|
|
This will append site-specific paths to to the module search path.
|
1998-01-13 14:34:40 -04:00
|
|
|
\indexiii{module}{search}{path}
|
1997-09-03 19:05:54 -03:00
|
|
|
|
|
|
|
It starts by constructing up to four directories from a head and a
|
|
|
|
tail part. For the head part, it uses \code{sys.prefix} and
|
|
|
|
\code{sys.exec_prefix}; empty heads are skipped. For
|
1998-03-08 03:09:19 -04:00
|
|
|
the tail part, it uses the empty string (on Macintosh or Windows) or
|
|
|
|
it uses first \file{lib/python\var{version}/site-packages} and then
|
1998-01-13 14:34:40 -04:00
|
|
|
\file{lib/site-python} (on \UNIX{}). For each of the distinct
|
|
|
|
head-tail combinations, it sees if it refers to an existing directory,
|
|
|
|
and if so, adds to \code{sys.path}, and also inspected for path
|
|
|
|
configuration files.
|
1997-09-03 19:05:54 -03:00
|
|
|
\indexii{site-python}{directory}
|
1997-09-07 23:02:37 -03:00
|
|
|
\indexii{site-packages}{directory}
|
1997-09-03 19:05:54 -03:00
|
|
|
|
|
|
|
A path configuration file is a file whose name has the form
|
|
|
|
\file{\var{package}.pth}; its contents are additional items (one
|
|
|
|
per line) to be added to \code{sys.path}. Non-existing items are
|
|
|
|
never added to \code{sys.path}, but no check is made that the item
|
|
|
|
refers to a directory (rather than a file). No item is added to
|
1997-08-30 17:03:28 -03:00
|
|
|
\code{sys.path} more than once. Blank lines and lines beginning with
|
|
|
|
\code{\#} are skipped.
|
|
|
|
\index{package}
|
1997-09-03 19:05:54 -03:00
|
|
|
\indexiii{path}{configuration}{file}
|
1997-08-30 17:03:28 -03:00
|
|
|
|
|
|
|
For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are
|
1998-02-16 16:58:58 -04:00
|
|
|
set to \file{/usr/local}. The Python \version\ library is then
|
|
|
|
installed in \file{/usr/local/lib/python\version}. Suppose this has a
|
1998-02-19 17:02:32 -04:00
|
|
|
subdirectory \file{/usr/local/lib/python\version/site-packages} with three
|
1998-02-16 16:58:58 -04:00
|
|
|
subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two
|
|
|
|
path configuration files, \file{foo.pth} and \file{bar.pth}. Assume
|
|
|
|
\file{foo.pth} contains the following:
|
1997-08-30 17:03:28 -03:00
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1997-08-30 17:03:28 -03:00
|
|
|
# foo package configuration
|
|
|
|
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
bletch
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1997-08-30 17:03:28 -03:00
|
|
|
|
|
|
|
and \file{bar.pth} contains:
|
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1997-08-30 17:03:28 -03:00
|
|
|
# bar package configuration
|
|
|
|
|
|
|
|
bar
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1997-08-30 17:03:28 -03:00
|
|
|
|
1998-01-13 14:34:40 -04:00
|
|
|
Then the following directories are added to \code{sys.path}, in this
|
|
|
|
order:
|
1997-08-30 17:03:28 -03:00
|
|
|
|
1998-02-13 02:58:54 -04:00
|
|
|
\begin{verbatim}
|
1998-02-19 17:02:32 -04:00
|
|
|
/usr/local/lib/python1.5/site-packages/bar
|
|
|
|
/usr/local/lib/python1.5/site-packages/foo
|
1998-02-13 02:58:54 -04:00
|
|
|
\end{verbatim}
|
1997-08-30 17:03:28 -03:00
|
|
|
|
|
|
|
Note that \file{bletch} is omitted because it doesn't exist; the
|
|
|
|
\file{bar} directory precedes the \file{foo} directory because
|
|
|
|
\file{bar.pth} comes alphabetically before \file{foo.pth}; and
|
|
|
|
\file{spam} is omitted because it is not mentioned in either path
|
|
|
|
configuration file.
|
1997-04-03 18:41:49 -04:00
|
|
|
|
|
|
|
After these path manipulations, an attempt is made to import a module
|
1998-03-08 03:09:19 -04:00
|
|
|
named \module{sitecustomize}\refmodindex{sitecustomize}, which can
|
1998-01-13 14:34:40 -04:00
|
|
|
perform arbitrary site-specific customizations. If this import fails
|
1998-03-08 03:09:19 -04:00
|
|
|
with an \exception{ImportError} exception, it is silently ignored.
|
1997-04-03 18:41:49 -04:00
|
|
|
|
1998-01-13 14:34:40 -04:00
|
|
|
Note that for some non-\UNIX{} systems, \code{sys.prefix} and
|
1997-04-03 18:41:49 -04:00
|
|
|
\code{sys.exec_prefix} are empty, and the path manipulations are
|
1998-01-13 14:34:40 -04:00
|
|
|
skipped; however the import of
|
1998-03-08 03:09:19 -04:00
|
|
|
\module{sitecustomize}\refmodindex{sitecustomize} is still attempted.
|