blob: e35eeee5f3909f56ffab56857d3ad136a40992f6 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`site` --- Site-specific configuration hook
2================================================
3
4.. module:: site
5 :synopsis: A standard way to reference site-specific modules.
6
Éric Araujo29a0b572011-08-19 02:14:03 +02007**Source code:** :source:`Lib/site.py`
8
9--------------
Georg Brandl8ec7f652007-08-15 14:28:01 +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
39.. versionchanged:: 2.6
40 A space or tab is now required after the import keyword.
41
42.. index::
43 single: package
44 triple: path; configuration; file
45
46For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
47:file:`/usr/local`. The Python X.Y library is then installed in
48:file:`/usr/local/lib/python{X.Y}` (where only the first three characters of
49``sys.version`` are used to form the installation path name). Suppose this has
50a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
51subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
52configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
53:file:`foo.pth` contains the following::
54
55 # foo package configuration
56
57 foo
58 bar
59 bletch
60
61and :file:`bar.pth` contains::
62
63 # bar package configuration
64
65 bar
66
Andrew M. Kuchlinge6896052008-09-27 22:54:08 +000067Then the following version-specific directories are added to
68``sys.path``, in this order::
Georg Brandl8ec7f652007-08-15 14:28:01 +000069
Andrew M. Kuchlinge6896052008-09-27 22:54:08 +000070 /usr/local/lib/pythonX.Y/site-packages/bar
71 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl8ec7f652007-08-15 14:28:01 +000072
73Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
74directory precedes the :file:`foo` directory because :file:`bar.pth` comes
75alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
76not mentioned in either path configuration file.
77
78.. index:: module: sitecustomize
79
80After these path manipulations, an attempt is made to import a module named
81:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
82If this import fails with an :exc:`ImportError` exception, it is silently
83ignored.
84
85.. index:: module: sitecustomize
86
87Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
88empty, and the path manipulations are skipped; however the import of
89:mod:`sitecustomize` is still attempted.
90
Christian Heimesaf748c32008-05-06 22:41:46 +000091
92.. data:: PREFIXES
93
94 A list of prefixes for site package directories
95
96 .. versionadded:: 2.6
97
98
99.. data:: ENABLE_USER_SITE
100
101 Flag showing the status of the user site directory. True means the
102 user site directory is enabled and added to sys.path. When the flag
103 is None the user site directory is disabled for security reasons.
104
105 .. versionadded:: 2.6
106
107
108.. data:: USER_SITE
109
110 Path to the user site directory for the current Python version or None
111
112 .. versionadded:: 2.6
113
114
115.. data:: USER_BASE
116
117 Path to the base directory for user site directories
118
119 .. versionadded:: 2.6
120
121
122.. envvar:: PYTHONNOUSERSITE
123
124 .. versionadded:: 2.6
125
126
127.. envvar:: PYTHONUSERBASE
128
129 .. versionadded:: 2.6
130
131
132.. function:: addsitedir(sitedir, known_paths=None)
133
134 Adds a directory to sys.path and processes its pth files.
135
Tarek Ziadé764fc232009-08-20 21:23:13 +0000136.. function:: getsitepackages()
137
138 Returns a list containing all global site-packages directories
139 (and possibly site-python).
140
141 .. versionadded:: 2.7
142
143.. function:: getuserbase()
144
Georg Brandlf6d367452010-03-12 10:02:03 +0000145 Returns the "user base" directory path.
Tarek Ziadé764fc232009-08-20 21:23:13 +0000146
Georg Brandlf6d367452010-03-12 10:02:03 +0000147 The "user base" directory can be used to store data. If the global
Tarek Ziadé764fc232009-08-20 21:23:13 +0000148 variable ``USER_BASE`` is not initialized yet, this function will also set
149 it.
150
151 .. versionadded:: 2.7
152
153.. function:: getusersitepackages()
154
155 Returns the user-specific site-packages directory path.
156
157 If the global variable ``USER_SITE`` is not initialized yet, this
158 function will also set it.
159
160 .. versionadded:: 2.7
Christian Heimesaf748c32008-05-06 22:41:46 +0000161
Benjamin Petersonafdbe3d2009-09-26 02:57:59 +0000162.. XXX Update documentation
163.. XXX document python -m site --user-base --user-site
Tarek Ziadé764fc232009-08-20 21:23:13 +0000164