blob: 23ce46504f443cd1a7952810f4fcb5ffd23a988c [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
Éric Araujoc09fca62011-03-23 02:06:24 +010016Importing this module will append site-specific paths to the module search
17path, unless :option:`-S` was used. In that case, this module can be safely
18imported with no automatic modifications to the module search path. To
19explicitly trigger the usual site-specific additions, call the
20:func:`site.main` function.
Georg Brandl116aa622007-08-15 14:28:22 +000021
Éric Araujob1734512011-04-24 03:15:32 +020022.. versionchanged:: 3.3
23 Importing the module used to trigger paths manipulation even when using
24 :option:`-S`.
25
Georg Brandl116aa622007-08-15 14:28:22 +000026.. index::
27 pair: site-python; directory
28 pair: site-packages; directory
29
30It starts by constructing up to four directories from a head and a tail part.
31For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads
32are skipped. For the tail part, it uses the empty string and then
33:file:`lib/site-packages` (on Windows) or
34:file:`lib/python|version|/site-packages` and then :file:`lib/site-python` (on
35Unix and Macintosh). For each of the distinct head-tail combinations, it sees
36if it refers to an existing directory, and if so, adds it to ``sys.path`` and
37also inspects the newly added path for configuration files.
38
39A path configuration file is a file whose name has the form :file:`package.pth`
40and exists in one of the four directories mentioned above; its contents are
41additional items (one per line) to be added to ``sys.path``. Non-existing items
42are never added to ``sys.path``, but no check is made that the item refers to a
43directory (rather than a file). No item is added to ``sys.path`` more than
44once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
45with ``import`` (followed by space or tab) are executed.
46
Georg Brandl116aa622007-08-15 14:28:22 +000047.. index::
48 single: package
49 triple: path; configuration; file
50
51For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
52:file:`/usr/local`. The Python X.Y library is then installed in
53:file:`/usr/local/lib/python{X.Y}` (where only the first three characters of
54``sys.version`` are used to form the installation path name). Suppose this has
55a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
56subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
57configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
58:file:`foo.pth` contains the following::
59
60 # foo package configuration
61
62 foo
63 bar
64 bletch
65
66and :file:`bar.pth` contains::
67
68 # bar package configuration
69
70 bar
71
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000072Then the following version-specific directories are added to
73``sys.path``, in this order::
Georg Brandl116aa622007-08-15 14:28:22 +000074
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000075 /usr/local/lib/pythonX.Y/site-packages/bar
76 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl116aa622007-08-15 14:28:22 +000077
78Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
79directory precedes the :file:`foo` directory because :file:`bar.pth` comes
80alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
81not mentioned in either path configuration file.
82
83.. index:: module: sitecustomize
84
85After these path manipulations, an attempt is made to import a module named
86:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
87If this import fails with an :exc:`ImportError` exception, it is silently
88ignored.
89
90.. index:: module: sitecustomize
91
92Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
93empty, and the path manipulations are skipped; however the import of
94:mod:`sitecustomize` is still attempted.
95
Christian Heimes8dc226f2008-05-06 23:45:46 +000096
97.. data:: PREFIXES
98
99 A list of prefixes for site package directories
100
Christian Heimes8dc226f2008-05-06 23:45:46 +0000101
102.. data:: ENABLE_USER_SITE
103
104 Flag showing the status of the user site directory. True means the
105 user site directory is enabled and added to sys.path. When the flag
106 is None the user site directory is disabled for security reasons.
107
Christian Heimes8dc226f2008-05-06 23:45:46 +0000108
109.. data:: USER_SITE
110
111 Path to the user site directory for the current Python version or None
112
Christian Heimes8dc226f2008-05-06 23:45:46 +0000113
114.. data:: USER_BASE
115
116 Path to the base directory for user site directories
117
Christian Heimes8dc226f2008-05-06 23:45:46 +0000118
119.. envvar:: PYTHONNOUSERSITE
120
Christian Heimes8dc226f2008-05-06 23:45:46 +0000121
122.. envvar:: PYTHONUSERBASE
123
Christian Heimes8dc226f2008-05-06 23:45:46 +0000124
Éric Araujoc09fca62011-03-23 02:06:24 +0100125.. function:: main()
126
127 Adds all the standard site-specific directories to the module search
128 path. This function is called automatically when this module is imported,
129 unless the :program:`python` interpreter was started with the :option:`-S`
130 flag.
131
Christian Heimes8dc226f2008-05-06 23:45:46 +0000132.. function:: addsitedir(sitedir, known_paths=None)
133
134 Adds a directory to sys.path and processes its pth files.
135
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000136.. function:: getsitepackages()
137
138 Returns a list containing all global site-packages directories
139 (and possibly site-python).
140
Mark Dickinson574b1d62009-10-01 20:20:09 +0000141 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000142
143.. function:: getuserbase()
144
Georg Brandlef871f62010-03-12 10:06:40 +0000145 Returns the "user base" directory path.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000146
Georg Brandlef871f62010-03-12 10:06:40 +0000147 The "user base" directory can be used to store data. If the global
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000148 variable ``USER_BASE`` is not initialized yet, this function will also set
149 it.
150
Mark Dickinson574b1d62009-10-01 20:20:09 +0000151 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000152
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
Mark Dickinson574b1d62009-10-01 20:20:09 +0000160 .. versionadded:: 3.2
Christian Heimes8dc226f2008-05-06 23:45:46 +0000161
Georg Brandl18244152009-09-02 20:34:52 +0000162.. XXX Update documentation
163.. XXX document python -m site --user-base --user-site
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000164