| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`site` --- Site-specific configuration hook | 
 | 2 | ================================================ | 
 | 3 |  | 
 | 4 | .. module:: site | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 5 |    :synopsis: Module responsible for site-specific configuration. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 11 | .. highlightlang:: none | 
 | 12 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | **This module is automatically imported during initialization.** The automatic | 
 | 14 | import can be suppressed using the interpreter's :option:`-S` option. | 
 | 15 |  | 
 | 16 | .. index:: triple: module; search; path | 
 | 17 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 18 | Importing this module will append site-specific paths to the module search path | 
 | 19 | and add a few builtins. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 |  | 
 | 21 | .. index:: | 
 | 22 |    pair: site-python; directory | 
 | 23 |    pair: site-packages; directory | 
 | 24 |  | 
 | 25 | It starts by constructing up to four directories from a head and a tail part. | 
 | 26 | For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads | 
 | 27 | are skipped.  For the tail part, it uses the empty string and then | 
 | 28 | :file:`lib/site-packages` (on Windows) or | 
| Ezio Melotti | 26e5878 | 2012-09-17 08:59:36 +0200 | [diff] [blame] | 29 | :file:`lib/python{X.Y}/site-packages` and then :file:`lib/site-python` (on | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | Unix and Macintosh).  For each of the distinct head-tail combinations, it sees | 
 | 31 | if it refers to an existing directory, and if so, adds it to ``sys.path`` and | 
 | 32 | also inspects the newly added path for configuration files. | 
 | 33 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 34 | A path configuration file is a file whose name has the form :file:`{name}.pth` | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | and exists in one of the four directories mentioned above; its contents are | 
 | 36 | additional items (one per line) to be added to ``sys.path``.  Non-existing items | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 37 | are never added to ``sys.path``, and no check is made that the item refers to a | 
 | 38 | directory rather than a file.  No item is added to ``sys.path`` more than | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | once.  Blank lines and lines beginning with ``#`` are skipped.  Lines starting | 
 | 40 | with ``import`` (followed by space or tab) are executed. | 
 | 41 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 48 | :file:`/usr/local/lib/python{X.Y}`.  Suppose this has | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three | 
 | 50 | subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path | 
 | 51 | configuration files, :file:`foo.pth` and :file:`bar.pth`.  Assume | 
 | 52 | :file:`foo.pth` contains the following:: | 
 | 53 |  | 
 | 54 |    # foo package configuration | 
 | 55 |  | 
 | 56 |    foo | 
 | 57 |    bar | 
 | 58 |    bletch | 
 | 59 |  | 
 | 60 | and :file:`bar.pth` contains:: | 
 | 61 |  | 
 | 62 |    # bar package configuration | 
 | 63 |  | 
 | 64 |    bar | 
 | 65 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 66 | Then the following version-specific directories are added to | 
 | 67 | ``sys.path``, in this order:: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 69 |    /usr/local/lib/pythonX.Y/site-packages/bar | 
 | 70 |    /usr/local/lib/pythonX.Y/site-packages/foo | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 |  | 
 | 72 | Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar` | 
 | 73 | directory precedes the :file:`foo` directory because :file:`bar.pth` comes | 
 | 74 | alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is | 
 | 75 | not mentioned in either path configuration file. | 
 | 76 |  | 
 | 77 | .. index:: module: sitecustomize | 
 | 78 |  | 
 | 79 | After these path manipulations, an attempt is made to import a module named | 
 | 80 | :mod:`sitecustomize`, which can perform arbitrary site-specific customizations. | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 81 | It is typically created by a system administrator in the site-packages | 
 | 82 | directory.  If this import fails with an :exc:`ImportError` exception, it is | 
 | 83 | silently ignored. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 85 | .. index:: module: usercustomize | 
 | 86 |  | 
 | 87 | After this, an attempt is made to import a module named :mod:`usercustomize`, | 
 | 88 | which can perform arbitrary user-specific customizations, if | 
 | 89 | :data:`ENABLE_USER_SITE` is true.  This file is intended to be created in the | 
 | 90 | user site-packages directory (see below), which is part of ``sys.path`` unless | 
 | 91 | disabled by :option:`-s`.  An :exc:`ImportError` will be silently ignored. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 |  | 
 | 93 | Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are | 
 | 94 | empty, and the path manipulations are skipped; however the import of | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 95 | :mod:`sitecustomize` and :mod:`usercustomize` is still attempted. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 97 |  | 
 | 98 | .. data:: PREFIXES | 
 | 99 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 100 |    A list of prefixes for site-packages directories. | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 101 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 102 |  | 
 | 103 | .. data:: ENABLE_USER_SITE | 
 | 104 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 105 |    Flag showing the status of the user site-packages directory.  ``True`` means | 
 | 106 |    that it is enabled and was added to ``sys.path``.  ``False`` means that it | 
 | 107 |    was disabled by user request (with :option:`-s` or | 
 | 108 |    :envvar:`PYTHONNOUSERSITE`).  ``None`` means it was disabled for security | 
 | 109 |    reasons (mismatch between user or group id and effective id) or by an | 
 | 110 |    administrator. | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 111 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 112 |  | 
 | 113 | .. data:: USER_SITE | 
 | 114 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 115 |    Path to the user site-packages for the running Python.  Can be ``None`` if | 
 | 116 |    :func:`getusersitepackages` hasn't been called yet.  Default value is | 
 | 117 |    :file:`~/.local/lib/python{X.Y}/site-packages` for UNIX and non-framework Mac | 
 | 118 |    OS X builds, :file:`~/Library/Python/{X.Y}/lib/python/site-packages` for Mac | 
 | 119 |    framework builds, and :file:`{%APPDATA%}\\Python\\Python{XY}\\site-packages` | 
 | 120 |    on Windows.  This directory is a site directory, which means that | 
 | 121 |    :file:`.pth` files in it will be processed. | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 122 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 123 |  | 
 | 124 | .. data:: USER_BASE | 
 | 125 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 126 |    Path to the base directory for the user site-packages.  Can be ``None`` if | 
 | 127 |    :func:`getuserbase` hasn't been called yet.  Default value is | 
 | 128 |    :file:`~/.local` for UNIX and Mac OS X non-framework builds, | 
 | 129 |    :file:`~/Library/Python/{X.Y}` for Mac framework builds, and | 
 | 130 |    :file:`{%APPDATA%}\\Python` for Windows.  This value is used by Distutils to | 
 | 131 |    compute the installation directories for scripts, data files, Python modules, | 
| Éric Araujo | 6ef038e | 2011-08-06 16:30:42 +0200 | [diff] [blame] | 132 |    etc. for the :ref:`user installation scheme <inst-alt-install-user>`.  See | 
 | 133 |    also :envvar:`PYTHONUSERBASE`. | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 134 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 135 |  | 
 | 136 | .. function:: addsitedir(sitedir, known_paths=None) | 
 | 137 |  | 
| Éric Araujo | 6ef038e | 2011-08-06 16:30:42 +0200 | [diff] [blame] | 138 |    Add a directory to sys.path and process its :file:`.pth` files.  Typically | 
 | 139 |    used in :mod:`sitecustomize` or :mod:`usercustomize` (see above). | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 140 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 141 |  | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 142 | .. function:: getsitepackages() | 
 | 143 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 144 |    Return a list containing all global site-packages directories (and possibly | 
 | 145 |    site-python). | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 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 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 149 |  | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 150 | .. function:: getuserbase() | 
 | 151 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 152 |    Return the path of the user base directory, :data:`USER_BASE`.  If it is not | 
 | 153 |    initialized yet, this function will also set it, respecting | 
 | 154 |    :envvar:`PYTHONUSERBASE`. | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 155 |  | 
| Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 156 |    .. versionadded:: 3.2 | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 157 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 158 |  | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 159 | .. function:: getusersitepackages() | 
 | 160 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 161 |    Return the path of the user-specific site-packages directory, | 
 | 162 |    :data:`USER_SITE`.  If it is not initialized yet, this function will also set | 
 | 163 |    it, respecting :envvar:`PYTHONNOUSERSITE` and :data:`USER_BASE`. | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 164 |  | 
| Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 165 |    .. versionadded:: 3.2 | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 166 |  | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 167 |  | 
| Éric Araujo | de4f05b | 2011-08-06 01:51:07 +0200 | [diff] [blame] | 168 | The :mod:`site` module also provides a way to get the user directories from the | 
 | 169 | command line: | 
 | 170 |  | 
 | 171 | .. code-block:: sh | 
 | 172 |  | 
 | 173 |    $ python3 -m site --user-site | 
 | 174 |    /home/user/.local/lib/python3.3/site-packages | 
 | 175 |  | 
 | 176 | .. program:: site | 
 | 177 |  | 
 | 178 | If it is called without arguments, it will print the contents of | 
 | 179 | :data:`sys.path` on the standard output, followed by the value of | 
 | 180 | :data:`USER_BASE` and whether the directory exists, then the same thing for | 
 | 181 | :data:`USER_SITE`, and finally the value of :data:`ENABLE_USER_SITE`. | 
 | 182 |  | 
 | 183 | .. cmdoption:: --user-base | 
 | 184 |  | 
 | 185 |    Print the path to the user base directory. | 
 | 186 |  | 
 | 187 | .. cmdoption:: --user-site | 
 | 188 |  | 
 | 189 |    Print the path to the user site-packages directory. | 
 | 190 |  | 
 | 191 | If both options are given, user base and user site will be printed (always in | 
 | 192 | this order), separated by :data:`os.pathsep`. | 
 | 193 |  | 
 | 194 | If any option is given, the script will exit with one of these values: ``O`` if | 
 | 195 | the user site-packages directory is enabled, ``1`` if it was disabled by the | 
 | 196 | user, ``2`` if it is disabled for security reasons or by an administrator, and a | 
 | 197 | value greater than 2 if there is an error. | 
 | 198 |  | 
 | 199 | .. seealso:: | 
 | 200 |  | 
 | 201 |    :pep:`370` -- Per user site-packages directory |