summaryrefslogtreecommitdiffstats
path: root/Doc/lib/libsite.tex
blob: ba3e23df71610722efe7b56c37711f6d33a83db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
\section{Standard Module \sectcode{site}}
\label{module-site}
\stmodindex{site}

\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
\samp{import site} somewhere near the top of their code.  This is no
longer necessary.

This will append site-specific paths to to the module search path.
\indexiii{module}{search}{path}

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
the tail part, it uses the empty string (on Mac or Windows) or it uses
first \file{lib/python\var{version}/site-packages} and then
\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.
\indexii{site-python}{directory}
\indexii{site-packages}{directory}

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
\code{sys.path} more than once.  Blank lines and lines beginning with
\code{\#} are skipped.
\index{package}
\indexiii{path}{configuration}{file}

For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are
set to \file{/usr/local}.  The Python 1.5 library is then installed in
\file{/usr/local/lib/python1.5}.  Suppose this has a subdirectory
\file{/usr/local/python1.5/site-packages} with three 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:

\begin{verbatim}
# foo package configuration

foo
bar
bletch
\end{verbatim}

and \file{bar.pth} contains:

\begin{verbatim}
# bar package configuration

bar
\end{verbatim}

Then the following directories are added to \code{sys.path}, in this
order:

\begin{verbatim}
/usr/local/python1.5/site-packages/bar
/usr/local/python1.5/site-packages/foo
\end{verbatim}

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.

After these path manipulations, an attempt is made to import a module
named \code{sitecustomize}\refmodindex{sitecustomize}, which can
perform arbitrary site-specific customizations.  If this import fails
with an \code{ImportError} exception, it is silently ignored.

Note that for some non-\UNIX{} systems, \code{sys.prefix} and
\code{sys.exec_prefix} are empty, and the path manipulations are
skipped; however the import of
\code{sitecustomize}\refmodindex{sitecustomize} is still attempted.