Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`site` --- Site-specific configuration hook |
| 2 | ================================================ |
| 3 | |
| 4 | .. module:: site |
| 5 | :synopsis: A standard way to reference site-specific modules. |
| 6 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 7 | **Source code:** :source:`Lib/site.py` |
| 8 | |
| 9 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
| 11 | **This module is automatically imported during initialization.** The automatic |
| 12 | import can be suppressed using the interpreter's :option:`-S` option. |
| 13 | |
| 14 | .. index:: triple: module; search; path |
| 15 | |
Éric Araujo | c09fca6 | 2011-03-23 02:06:24 +0100 | [diff] [blame] | 16 | Importing this module will append site-specific paths to the module search |
| 17 | path, unless :option:`-S` was used. In that case, this module can be safely |
| 18 | imported with no automatic modifications to the module search path. To |
| 19 | explicitly trigger the usual site-specific additions, call the |
| 20 | :func:`site.main` function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
| 22 | .. index:: |
| 23 | pair: site-python; directory |
| 24 | pair: site-packages; directory |
| 25 | |
| 26 | It starts by constructing up to four directories from a head and a tail part. |
| 27 | For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads |
| 28 | are 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 |
| 31 | Unix and Macintosh). For each of the distinct head-tail combinations, it sees |
| 32 | if it refers to an existing directory, and if so, adds it to ``sys.path`` and |
| 33 | also inspects the newly added path for configuration files. |
| 34 | |
| 35 | A path configuration file is a file whose name has the form :file:`package.pth` |
| 36 | and exists in one of the four directories mentioned above; its contents are |
| 37 | additional items (one per line) to be added to ``sys.path``. Non-existing items |
| 38 | are never added to ``sys.path``, but no check is made that the item refers to a |
| 39 | directory (rather than a file). No item is added to ``sys.path`` more than |
| 40 | once. Blank lines and lines beginning with ``#`` are skipped. Lines starting |
| 41 | with ``import`` (followed by space or tab) are executed. |
| 42 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | .. index:: |
| 44 | single: package |
| 45 | triple: path; configuration; file |
| 46 | |
| 47 | For 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 |
| 51 | a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three |
| 52 | subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path |
| 53 | configuration 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 | |
| 62 | and :file:`bar.pth` contains:: |
| 63 | |
| 64 | # bar package configuration |
| 65 | |
| 66 | bar |
| 67 | |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 68 | Then the following version-specific directories are added to |
| 69 | ``sys.path``, in this order:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 71 | /usr/local/lib/pythonX.Y/site-packages/bar |
| 72 | /usr/local/lib/pythonX.Y/site-packages/foo |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
| 74 | Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar` |
| 75 | directory precedes the :file:`foo` directory because :file:`bar.pth` comes |
| 76 | alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is |
| 77 | not mentioned in either path configuration file. |
| 78 | |
| 79 | .. index:: module: sitecustomize |
| 80 | |
| 81 | After these path manipulations, an attempt is made to import a module named |
| 82 | :mod:`sitecustomize`, which can perform arbitrary site-specific customizations. |
| 83 | If this import fails with an :exc:`ImportError` exception, it is silently |
| 84 | ignored. |
| 85 | |
| 86 | .. index:: module: sitecustomize |
| 87 | |
| 88 | Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are |
| 89 | empty, and the path manipulations are skipped; however the import of |
| 90 | :mod:`sitecustomize` is still attempted. |
| 91 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 92 | |
| 93 | .. data:: PREFIXES |
| 94 | |
| 95 | A list of prefixes for site package directories |
| 96 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 97 | |
| 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 Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 104 | |
| 105 | .. data:: USER_SITE |
| 106 | |
| 107 | Path to the user site directory for the current Python version or None |
| 108 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 109 | |
| 110 | .. data:: USER_BASE |
| 111 | |
| 112 | Path to the base directory for user site directories |
| 113 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 114 | |
| 115 | .. envvar:: PYTHONNOUSERSITE |
| 116 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 117 | |
| 118 | .. envvar:: PYTHONUSERBASE |
| 119 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 120 | |
Éric Araujo | c09fca6 | 2011-03-23 02:06:24 +0100 | [diff] [blame] | 121 | .. 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 Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 128 | .. function:: addsitedir(sitedir, known_paths=None) |
| 129 | |
| 130 | Adds a directory to sys.path and processes its pth files. |
| 131 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 132 | .. function:: getsitepackages() |
| 133 | |
| 134 | Returns a list containing all global site-packages directories |
| 135 | (and possibly site-python). |
| 136 | |
Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 137 | .. versionadded:: 3.2 |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 138 | |
| 139 | .. function:: getuserbase() |
| 140 | |
Georg Brandl | ef871f6 | 2010-03-12 10:06:40 +0000 | [diff] [blame] | 141 | Returns the "user base" directory path. |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 142 | |
Georg Brandl | ef871f6 | 2010-03-12 10:06:40 +0000 | [diff] [blame] | 143 | The "user base" directory can be used to store data. If the global |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 144 | variable ``USER_BASE`` is not initialized yet, this function will also set |
| 145 | it. |
| 146 | |
Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 147 | .. versionadded:: 3.2 |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 148 | |
| 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 Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 156 | .. versionadded:: 3.2 |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 157 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 158 | .. XXX Update documentation |
| 159 | .. XXX document python -m site --user-base --user-site |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 160 | |