blob: 4b7a2344464ea665ed05a7f30043e342bcb1f25b [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
22.. index::
23 pair: site-python; directory
24 pair: site-packages; directory
25
26It starts by constructing up to four directories from a head and a tail part.
27For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads
28are skipped. For the tail part, it uses the empty string and then
29:file:`lib/site-packages` (on Windows) or
30:file:`lib/python|version|/site-packages` and then :file:`lib/site-python` (on
31Unix and Macintosh). For each of the distinct head-tail combinations, it sees
32if it refers to an existing directory, and if so, adds it to ``sys.path`` and
33also inspects the newly added path for configuration files.
34
35A path configuration file is a file whose name has the form :file:`package.pth`
36and exists in one of the four directories mentioned above; its contents are
37additional items (one per line) to be added to ``sys.path``. Non-existing items
38are never added to ``sys.path``, but no check is made that the item refers to a
39directory (rather than a file). No item is added to ``sys.path`` more than
40once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
41with ``import`` (followed by space or tab) are executed.
42
Georg Brandl116aa622007-08-15 14:28:22 +000043.. index::
44 single: package
45 triple: path; configuration; file
46
47For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
48:file:`/usr/local`. The Python X.Y library is then installed in
49:file:`/usr/local/lib/python{X.Y}` (where only the first three characters of
50``sys.version`` are used to form the installation path name). Suppose this has
51a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
52subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
53configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
54:file:`foo.pth` contains the following::
55
56 # foo package configuration
57
58 foo
59 bar
60 bletch
61
62and :file:`bar.pth` contains::
63
64 # bar package configuration
65
66 bar
67
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000068Then the following version-specific directories are added to
69``sys.path``, in this order::
Georg Brandl116aa622007-08-15 14:28:22 +000070
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000071 /usr/local/lib/pythonX.Y/site-packages/bar
72 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl116aa622007-08-15 14:28:22 +000073
74Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar`
75directory precedes the :file:`foo` directory because :file:`bar.pth` comes
76alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is
77not mentioned in either path configuration file.
78
79.. index:: module: sitecustomize
80
81After these path manipulations, an attempt is made to import a module named
82:mod:`sitecustomize`, which can perform arbitrary site-specific customizations.
83If this import fails with an :exc:`ImportError` exception, it is silently
84ignored.
85
86.. index:: module: sitecustomize
87
88Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are
89empty, and the path manipulations are skipped; however the import of
90:mod:`sitecustomize` is still attempted.
91
Christian Heimes8dc226f2008-05-06 23:45:46 +000092
93.. data:: PREFIXES
94
95 A list of prefixes for site package directories
96
Christian Heimes8dc226f2008-05-06 23:45:46 +000097
98.. data:: ENABLE_USER_SITE
99
100 Flag showing the status of the user site directory. True means the
101 user site directory is enabled and added to sys.path. When the flag
102 is None the user site directory is disabled for security reasons.
103
Christian Heimes8dc226f2008-05-06 23:45:46 +0000104
105.. data:: USER_SITE
106
107 Path to the user site directory for the current Python version or None
108
Christian Heimes8dc226f2008-05-06 23:45:46 +0000109
110.. data:: USER_BASE
111
112 Path to the base directory for user site directories
113
Christian Heimes8dc226f2008-05-06 23:45:46 +0000114
115.. envvar:: PYTHONNOUSERSITE
116
Christian Heimes8dc226f2008-05-06 23:45:46 +0000117
118.. envvar:: PYTHONUSERBASE
119
Christian Heimes8dc226f2008-05-06 23:45:46 +0000120
Éric Araujoc09fca62011-03-23 02:06:24 +0100121.. function:: main()
122
123 Adds all the standard site-specific directories to the module search
124 path. This function is called automatically when this module is imported,
125 unless the :program:`python` interpreter was started with the :option:`-S`
126 flag.
127
Christian Heimes8dc226f2008-05-06 23:45:46 +0000128.. function:: addsitedir(sitedir, known_paths=None)
129
130 Adds a directory to sys.path and processes its pth files.
131
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000132.. function:: getsitepackages()
133
134 Returns a list containing all global site-packages directories
135 (and possibly site-python).
136
Mark Dickinson574b1d62009-10-01 20:20:09 +0000137 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000138
139.. function:: getuserbase()
140
Georg Brandlef871f62010-03-12 10:06:40 +0000141 Returns the "user base" directory path.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000142
Georg Brandlef871f62010-03-12 10:06:40 +0000143 The "user base" directory can be used to store data. If the global
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000144 variable ``USER_BASE`` is not initialized yet, this function will also set
145 it.
146
Mark Dickinson574b1d62009-10-01 20:20:09 +0000147 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000148
149.. function:: getusersitepackages()
150
151 Returns the user-specific site-packages directory path.
152
153 If the global variable ``USER_SITE`` is not initialized yet, this
154 function will also set it.
155
Mark Dickinson574b1d62009-10-01 20:20:09 +0000156 .. versionadded:: 3.2
Christian Heimes8dc226f2008-05-06 23:45:46 +0000157
Georg Brandl18244152009-09-02 20:34:52 +0000158.. XXX Update documentation
159.. XXX document python -m site --user-base --user-site
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000160