blob: cf2892f5d6e973564f67d9961bd74ef6dfd69ac2 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`site` --- Site-specific configuration hook
3================================================
4
5.. module:: site
6 :synopsis: A standard way to reference site-specific modules.
7
8
9**This module is automatically imported during initialization.** The automatic
10import can be suppressed using the interpreter's :option:`-S` option.
11
12.. index:: triple: module; search; path
13
14Importing this module will append site-specific paths to the module search path.
15
16.. index::
17 pair: site-python; directory
18 pair: site-packages; directory
19
20It starts by constructing up to four directories from a head and a tail part.
21For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads
22are skipped. For the tail part, it uses the empty string and then
23:file:`lib/site-packages` (on Windows) or
24:file:`lib/python|version|/site-packages` and then :file:`lib/site-python` (on
25Unix and Macintosh). For each of the distinct head-tail combinations, it sees
26if it refers to an existing directory, and if so, adds it to ``sys.path`` and
27also inspects the newly added path for configuration files.
28
29A path configuration file is a file whose name has the form :file:`package.pth`
30and exists in one of the four directories mentioned above; its contents are
31additional items (one per line) to be added to ``sys.path``. Non-existing items
32are never added to ``sys.path``, but no check is made that the item refers to a
33directory (rather than a file). No item is added to ``sys.path`` more than
34once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
35with ``import`` (followed by space or tab) are executed.
36
Georg Brandl116aa622007-08-15 14:28:22 +000037.. index::
38 single: package
39 triple: path; configuration; file
40
41For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
42:file:`/usr/local`. The Python X.Y library is then installed in
43:file:`/usr/local/lib/python{X.Y}` (where only the first three characters of
44``sys.version`` are used to form the installation path name). Suppose this has
45a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
46subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
47configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
48:file:`foo.pth` contains the following::
49
50 # foo package configuration
51
52 foo
53 bar
54 bletch
55
56and :file:`bar.pth` contains::
57
58 # bar package configuration
59
60 bar
61
62Then the following directories are added to ``sys.path``, in this order::
63
64 /usr/local/lib/python2.3/site-packages/bar
65 /usr/local/lib/python2.3/site-packages/foo
66
67Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
68directory precedes the :file:`foo` directory because :file:`bar.pth` comes
69alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
70not mentioned in either path configuration file.
71
72.. index:: module: sitecustomize
73
74After these path manipulations, an attempt is made to import a module named
75:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
76If this import fails with an :exc:`ImportError` exception, it is silently
77ignored.
78
79.. index:: module: sitecustomize
80
81Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
82empty, and the path manipulations are skipped; however the import of
83:mod:`sitecustomize` is still attempted.
84