Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 7 | **Source code:** :source:`Lib/site.py` |
| 8 | |
| 9 | -------------- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
| 16 | Importing 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 | |
| 22 | It starts by constructing up to four directories from a head and a tail part. |
| 23 | For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads |
| 24 | are 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 |
| 27 | Unix and Macintosh). For each of the distinct head-tail combinations, it sees |
| 28 | if it refers to an existing directory, and if so, adds it to ``sys.path`` and |
| 29 | also inspects the newly added path for configuration files. |
| 30 | |
| 31 | A path configuration file is a file whose name has the form :file:`package.pth` |
| 32 | and exists in one of the four directories mentioned above; its contents are |
| 33 | additional items (one per line) to be added to ``sys.path``. Non-existing items |
| 34 | are never added to ``sys.path``, but no check is made that the item refers to a |
| 35 | directory (rather than a file). No item is added to ``sys.path`` more than |
| 36 | once. Blank lines and lines beginning with ``#`` are skipped. Lines starting |
| 37 | with ``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 | |
| 46 | For 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 |
| 50 | a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three |
| 51 | subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path |
| 52 | configuration 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 | |
| 61 | and :file:`bar.pth` contains:: |
| 62 | |
| 63 | # bar package configuration |
| 64 | |
| 65 | bar |
| 66 | |
Andrew M. Kuchling | e689605 | 2008-09-27 22:54:08 +0000 | [diff] [blame] | 67 | Then the following version-specific directories are added to |
| 68 | ``sys.path``, in this order:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 69 | |
Andrew M. Kuchling | e689605 | 2008-09-27 22:54:08 +0000 | [diff] [blame] | 70 | /usr/local/lib/pythonX.Y/site-packages/bar |
| 71 | /usr/local/lib/pythonX.Y/site-packages/foo |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 72 | |
| 73 | Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar` |
| 74 | directory precedes the :file:`foo` directory because :file:`bar.pth` comes |
| 75 | alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is |
| 76 | not mentioned in either path configuration file. |
| 77 | |
| 78 | .. index:: module: sitecustomize |
| 79 | |
| 80 | After these path manipulations, an attempt is made to import a module named |
| 81 | :mod:`sitecustomize`, which can perform arbitrary site-specific customizations. |
| 82 | If this import fails with an :exc:`ImportError` exception, it is silently |
| 83 | ignored. |
| 84 | |
| 85 | .. index:: module: sitecustomize |
| 86 | |
| 87 | Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are |
| 88 | empty, and the path manipulations are skipped; however the import of |
| 89 | :mod:`sitecustomize` is still attempted. |
| 90 | |
Christian Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 91 | |
| 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é | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 136 | .. 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 Brandl | f6d36745 | 2010-03-12 10:02:03 +0000 | [diff] [blame] | 145 | Returns the "user base" directory path. |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 146 | |
Georg Brandl | f6d36745 | 2010-03-12 10:02:03 +0000 | [diff] [blame] | 147 | The "user base" directory can be used to store data. If the global |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 148 | 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 Heimes | af748c3 | 2008-05-06 22:41:46 +0000 | [diff] [blame] | 161 | |
Benjamin Peterson | afdbe3d | 2009-09-26 02:57:59 +0000 | [diff] [blame] | 162 | .. XXX Update documentation |
| 163 | .. XXX document python -m site --user-base --user-site |
Tarek Ziadé | 764fc23 | 2009-08-20 21:23:13 +0000 | [diff] [blame] | 164 | |