blob: b77f3cf02d3476f513d79e3143e39182caad6324 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`site` --- Site-specific configuration hook
2================================================
3
4.. module:: site
5 :synopsis: A standard way to reference site-specific modules.
6
Raymond Hettinger469271d2011-01-27 20:38:46 +00007**Source code:** :source:`Lib/site.py`
8
9--------------
Georg Brandl116aa622007-08-15 14:28:22 +000010
11**This module is automatically imported during initialization.** The automatic
12import can be suppressed using the interpreter's :option:`-S` option.
13
14.. index:: triple: module; search; path
15
16Importing this module will append site-specific paths to the module search path.
17
18.. index::
19 pair: site-python; directory
20 pair: site-packages; directory
21
22It starts by constructing up to four directories from a head and a tail part.
23For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads
24are skipped. For the tail part, it uses the empty string and then
25:file:`lib/site-packages` (on Windows) or
26:file:`lib/python|version|/site-packages` and then :file:`lib/site-python` (on
27Unix and Macintosh). For each of the distinct head-tail combinations, it sees
28if it refers to an existing directory, and if so, adds it to ``sys.path`` and
29also inspects the newly added path for configuration files.
30
31A path configuration file is a file whose name has the form :file:`package.pth`
32and exists in one of the four directories mentioned above; its contents are
33additional items (one per line) to be added to ``sys.path``. Non-existing items
34are never added to ``sys.path``, but no check is made that the item refers to a
35directory (rather than a file). No item is added to ``sys.path`` more than
36once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
37with ``import`` (followed by space or tab) are executed.
38
Georg Brandl116aa622007-08-15 14:28:22 +000039.. index::
40 single: package
41 triple: path; configuration; file
42
43For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
44:file:`/usr/local`. The Python X.Y library is then installed in
45:file:`/usr/local/lib/python{X.Y}` (where only the first three characters of
46``sys.version`` are used to form the installation path name). Suppose this has
47a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
48subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
49configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
50:file:`foo.pth` contains the following::
51
52 # foo package configuration
53
54 foo
55 bar
56 bletch
57
58and :file:`bar.pth` contains::
59
60 # bar package configuration
61
62 bar
63
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000064Then the following version-specific directories are added to
65``sys.path``, in this order::
Georg Brandl116aa622007-08-15 14:28:22 +000066
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000067 /usr/local/lib/pythonX.Y/site-packages/bar
68 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl116aa622007-08-15 14:28:22 +000069
70Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
71directory precedes the :file:`foo` directory because :file:`bar.pth` comes
72alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
73not mentioned in either path configuration file.
74
75.. index:: module: sitecustomize
76
77After these path manipulations, an attempt is made to import a module named
78:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
79If this import fails with an :exc:`ImportError` exception, it is silently
80ignored.
81
82.. index:: module: sitecustomize
83
84Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
85empty, and the path manipulations are skipped; however the import of
86:mod:`sitecustomize` is still attempted.
87
Christian Heimes8dc226f2008-05-06 23:45:46 +000088
89.. data:: PREFIXES
90
91 A list of prefixes for site package directories
92
Christian Heimes8dc226f2008-05-06 23:45:46 +000093
94.. data:: ENABLE_USER_SITE
95
96 Flag showing the status of the user site directory. True means the
97 user site directory is enabled and added to sys.path. When the flag
98 is None the user site directory is disabled for security reasons.
99
Christian Heimes8dc226f2008-05-06 23:45:46 +0000100
101.. data:: USER_SITE
102
103 Path to the user site directory for the current Python version or None
104
Christian Heimes8dc226f2008-05-06 23:45:46 +0000105
106.. data:: USER_BASE
107
108 Path to the base directory for user site directories
109
Christian Heimes8dc226f2008-05-06 23:45:46 +0000110
111.. envvar:: PYTHONNOUSERSITE
112
Christian Heimes8dc226f2008-05-06 23:45:46 +0000113
114.. envvar:: PYTHONUSERBASE
115
Christian Heimes8dc226f2008-05-06 23:45:46 +0000116
117.. function:: addsitedir(sitedir, known_paths=None)
118
119 Adds a directory to sys.path and processes its pth files.
120
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000121.. function:: getsitepackages()
122
123 Returns a list containing all global site-packages directories
124 (and possibly site-python).
125
Mark Dickinson574b1d62009-10-01 20:20:09 +0000126 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000127
128.. function:: getuserbase()
129
Georg Brandlef871f62010-03-12 10:06:40 +0000130 Returns the "user base" directory path.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000131
Georg Brandlef871f62010-03-12 10:06:40 +0000132 The "user base" directory can be used to store data. If the global
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000133 variable ``USER_BASE`` is not initialized yet, this function will also set
134 it.
135
Mark Dickinson574b1d62009-10-01 20:20:09 +0000136 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000137
138.. function:: getusersitepackages()
139
140 Returns the user-specific site-packages directory path.
141
142 If the global variable ``USER_SITE`` is not initialized yet, this
143 function will also set it.
144
Mark Dickinson574b1d62009-10-01 20:20:09 +0000145 .. versionadded:: 3.2
Christian Heimes8dc226f2008-05-06 23:45:46 +0000146
Georg Brandl18244152009-09-02 20:34:52 +0000147.. XXX Update documentation
148.. XXX document python -m site --user-base --user-site
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000149