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 | |
| 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 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | .. index:: |
| 40 | single: package |
| 41 | triple: path; configuration; file |
| 42 | |
| 43 | For example, suppose ``sys.prefix`` and ``sys.exec_prefix`` are set to |
| 44 | :file:`/usr/local`. The Python X.Y library is then installed in |
| 45 | :file:`/usr/local/lib/python{X.Y}` (where only the first three characters of |
| 46 | ``sys.version`` are used to form the installation path name). Suppose this has |
| 47 | a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three |
| 48 | subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path |
| 49 | configuration files, :file:`foo.pth` and :file:`bar.pth`. Assume |
| 50 | :file:`foo.pth` contains the following:: |
| 51 | |
| 52 | # foo package configuration |
| 53 | |
| 54 | foo |
| 55 | bar |
| 56 | bletch |
| 57 | |
| 58 | and :file:`bar.pth` contains:: |
| 59 | |
| 60 | # bar package configuration |
| 61 | |
| 62 | bar |
| 63 | |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 64 | Then the following version-specific directories are added to |
| 65 | ``sys.path``, in this order:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 67 | /usr/local/lib/pythonX.Y/site-packages/bar |
| 68 | /usr/local/lib/pythonX.Y/site-packages/foo |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
| 70 | Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar` |
| 71 | directory precedes the :file:`foo` directory because :file:`bar.pth` comes |
| 72 | alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is |
| 73 | not mentioned in either path configuration file. |
| 74 | |
| 75 | .. index:: module: sitecustomize |
| 76 | |
| 77 | After these path manipulations, an attempt is made to import a module named |
| 78 | :mod:`sitecustomize`, which can perform arbitrary site-specific customizations. |
| 79 | If this import fails with an :exc:`ImportError` exception, it is silently |
| 80 | ignored. |
| 81 | |
| 82 | .. index:: module: sitecustomize |
| 83 | |
| 84 | Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are |
| 85 | empty, and the path manipulations are skipped; however the import of |
| 86 | :mod:`sitecustomize` is still attempted. |
| 87 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 88 | |
| 89 | .. data:: PREFIXES |
| 90 | |
| 91 | A list of prefixes for site package directories |
| 92 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 93 | |
| 94 | .. data:: ENABLE_USER_SITE |
| 95 | |
| 96 | Flag showing the status of the user site directory. True means the |
| 97 | user site directory is enabled and added to sys.path. When the flag |
| 98 | is None the user site directory is disabled for security reasons. |
| 99 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 100 | |
| 101 | .. data:: USER_SITE |
| 102 | |
| 103 | Path to the user site directory for the current Python version or None |
| 104 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 105 | |
| 106 | .. data:: USER_BASE |
| 107 | |
| 108 | Path to the base directory for user site directories |
| 109 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 110 | |
| 111 | .. envvar:: PYTHONNOUSERSITE |
| 112 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 113 | |
| 114 | .. envvar:: PYTHONUSERBASE |
| 115 | |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 116 | |
| 117 | .. function:: addsitedir(sitedir, known_paths=None) |
| 118 | |
| 119 | Adds a directory to sys.path and processes its pth files. |
| 120 | |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 121 | .. function:: getsitepackages() |
| 122 | |
| 123 | Returns a list containing all global site-packages directories |
| 124 | (and possibly site-python). |
| 125 | |
Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 126 | .. versionadded:: 3.2 |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 127 | |
| 128 | .. function:: getuserbase() |
| 129 | |
Georg Brandl | ef871f6 | 2010-03-12 10:06:40 +0000 | [diff] [blame] | 130 | Returns the "user base" directory path. |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 131 | |
Georg Brandl | ef871f6 | 2010-03-12 10:06:40 +0000 | [diff] [blame] | 132 | 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] | 133 | variable ``USER_BASE`` is not initialized yet, this function will also set |
| 134 | it. |
| 135 | |
Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 136 | .. versionadded:: 3.2 |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 137 | |
| 138 | .. function:: getusersitepackages() |
| 139 | |
| 140 | Returns the user-specific site-packages directory path. |
| 141 | |
| 142 | If the global variable ``USER_SITE`` is not initialized yet, this |
| 143 | function will also set it. |
| 144 | |
Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 145 | .. versionadded:: 3.2 |
Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 146 | |
Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 147 | .. XXX Update documentation |
| 148 | .. XXX document python -m site --user-base --user-site |
Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 149 | |