blob: b347ee8a740282d3e369abb0c049740e1d48e1f7 [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
7
8**This module is automatically imported during initialization.** The automatic
9import can be suppressed using the interpreter's :option:`-S` option.
10
11.. index:: triple: module; search; path
12
13Importing this module will append site-specific paths to the module search path.
14
15.. index::
16 pair: site-python; directory
17 pair: site-packages; directory
18
19It starts by constructing up to four directories from a head and a tail part.
20For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads
21are skipped. For the tail part, it uses the empty string and then
22:file:`lib/site-packages` (on Windows) or
23:file:`lib/python|version|/site-packages` and then :file:`lib/site-python` (on
24Unix and Macintosh). For each of the distinct head-tail combinations, it sees
25if it refers to an existing directory, and if so, adds it to ``sys.path`` and
26also inspects the newly added path for configuration files.
27
28A path configuration file is a file whose name has the form :file:`package.pth`
29and exists in one of the four directories mentioned above; its contents are
30additional items (one per line) to be added to ``sys.path``. Non-existing items
31are never added to ``sys.path``, but no check is made that the item refers to a
32directory (rather than a file). No item is added to ``sys.path`` more than
33once. Blank lines and lines beginning with ``#`` are skipped. Lines starting
34with ``import`` (followed by space or tab) are executed.
35
Georg Brandl116aa622007-08-15 14:28:22 +000036.. index::
37 single: package
38 triple: path; configuration; file
39
40For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to
41:file:`/usr/local`. The Python X.Y library is then installed in
42:file:`/usr/local/lib/python{X.Y}` (where only the first three characters of
43``sys.version`` are used to form the installation path name). Suppose this has
44a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three
45subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path
46configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume
47:file:`foo.pth` contains the following::
48
49 # foo package configuration
50
51 foo
52 bar
53 bletch
54
55and :file:`bar.pth` contains::
56
57 # bar package configuration
58
59 bar
60
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000061Then the following version-specific directories are added to
62``sys.path``, in this order::
Georg Brandl116aa622007-08-15 14:28:22 +000063
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +000064 /usr/local/lib/pythonX.Y/site-packages/bar
65 /usr/local/lib/pythonX.Y/site-packages/foo
Georg Brandl116aa622007-08-15 14:28:22 +000066
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
Christian Heimes8dc226f2008-05-06 23:45:46 +000085
86.. data:: PREFIXES
87
88 A list of prefixes for site package directories
89
Christian Heimes8dc226f2008-05-06 23:45:46 +000090
91.. data:: ENABLE_USER_SITE
92
93 Flag showing the status of the user site directory. True means the
94 user site directory is enabled and added to sys.path. When the flag
95 is None the user site directory is disabled for security reasons.
96
Christian Heimes8dc226f2008-05-06 23:45:46 +000097
98.. data:: USER_SITE
99
100 Path to the user site directory for the current Python version or None
101
Christian Heimes8dc226f2008-05-06 23:45:46 +0000102
103.. data:: USER_BASE
104
105 Path to the base directory for user site directories
106
Christian Heimes8dc226f2008-05-06 23:45:46 +0000107
108.. envvar:: PYTHONNOUSERSITE
109
Christian Heimes8dc226f2008-05-06 23:45:46 +0000110
111.. envvar:: PYTHONUSERBASE
112
Christian Heimes8dc226f2008-05-06 23:45:46 +0000113
114.. function:: addsitedir(sitedir, known_paths=None)
115
116 Adds a directory to sys.path and processes its pth files.
117
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000118.. function:: getsitepackages()
119
120 Returns a list containing all global site-packages directories
121 (and possibly site-python).
122
Mark Dickinson574b1d62009-10-01 20:20:09 +0000123 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000124
125.. function:: getuserbase()
126
Georg Brandlef871f62010-03-12 10:06:40 +0000127 Returns the "user base" directory path.
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000128
Georg Brandlef871f62010-03-12 10:06:40 +0000129 The "user base" directory can be used to store data. If the global
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000130 variable ``USER_BASE`` is not initialized yet, this function will also set
131 it.
132
Mark Dickinson574b1d62009-10-01 20:20:09 +0000133 .. versionadded:: 3.2
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000134
135.. function:: getusersitepackages()
136
137 Returns the user-specific site-packages directory path.
138
139 If the global variable ``USER_SITE`` is not initialized yet, this
140 function will also set it.
141
Mark Dickinson574b1d62009-10-01 20:20:09 +0000142 .. versionadded:: 3.2
Christian Heimes8dc226f2008-05-06 23:45:46 +0000143
Georg Brandl18244152009-09-02 20:34:52 +0000144.. XXX Update documentation
145.. XXX document python -m site --user-base --user-site
Tarek Ziadé4a608c02009-08-20 21:28:05 +0000146