| 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 |  | 
|  | 7 |  | 
|  | 8 | **This module is automatically imported during initialization.** The automatic | 
|  | 9 | import can be suppressed using the interpreter's :option:`-S` option. | 
|  | 10 |  | 
|  | 11 | .. index:: triple: module; search; path | 
|  | 12 |  | 
|  | 13 | Importing 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 |  | 
|  | 19 | It starts by constructing up to four directories from a head and a tail part. | 
|  | 20 | For the head part, it uses ``sys.prefix`` and ``sys.exec_prefix``; empty heads | 
|  | 21 | are 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 | 
|  | 24 | Unix and Macintosh).  For each of the distinct head-tail combinations, it sees | 
|  | 25 | if it refers to an existing directory, and if so, adds it to ``sys.path`` and | 
|  | 26 | also inspects the newly added path for configuration files. | 
|  | 27 |  | 
|  | 28 | A path configuration file is a file whose name has the form :file:`package.pth` | 
|  | 29 | and exists in one of the four directories mentioned above; its contents are | 
|  | 30 | additional items (one per line) to be added to ``sys.path``.  Non-existing items | 
|  | 31 | are never added to ``sys.path``, but no check is made that the item refers to a | 
|  | 32 | directory (rather than a file).  No item is added to ``sys.path`` more than | 
|  | 33 | once.  Blank lines and lines beginning with ``#`` are skipped.  Lines starting | 
|  | 34 | with ``import`` (followed by space or tab) are executed. | 
|  | 35 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | .. index:: | 
|  | 37 | single: package | 
|  | 38 | triple: path; configuration; file | 
|  | 39 |  | 
|  | 40 | For 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 | 
|  | 44 | a subdirectory :file:`/usr/local/lib/python{X.Y}/site-packages` with three | 
|  | 45 | subsubdirectories, :file:`foo`, :file:`bar` and :file:`spam`, and two path | 
|  | 46 | configuration 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 |  | 
|  | 55 | and :file:`bar.pth` contains:: | 
|  | 56 |  | 
|  | 57 | # bar package configuration | 
|  | 58 |  | 
|  | 59 | bar | 
|  | 60 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 61 | Then the following version-specific directories are added to | 
|  | 62 | ``sys.path``, in this order:: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 |  | 
| Benjamin Peterson | e9bbc8b | 2008-09-28 02:06:32 +0000 | [diff] [blame] | 64 | /usr/local/lib/pythonX.Y/site-packages/bar | 
|  | 65 | /usr/local/lib/pythonX.Y/site-packages/foo | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 |  | 
|  | 67 | Note that :file:`bletch` is omitted because it doesn't exist; the :file:`bar` | 
|  | 68 | directory precedes the :file:`foo` directory because :file:`bar.pth` comes | 
|  | 69 | alphabetically before :file:`foo.pth`; and :file:`spam` is omitted because it is | 
|  | 70 | not mentioned in either path configuration file. | 
|  | 71 |  | 
|  | 72 | .. index:: module: sitecustomize | 
|  | 73 |  | 
|  | 74 | After these path manipulations, an attempt is made to import a module named | 
|  | 75 | :mod:`sitecustomize`, which can perform arbitrary site-specific customizations. | 
|  | 76 | If this import fails with an :exc:`ImportError` exception, it is silently | 
|  | 77 | ignored. | 
|  | 78 |  | 
|  | 79 | .. index:: module: sitecustomize | 
|  | 80 |  | 
|  | 81 | Note that for some non-Unix systems, ``sys.prefix`` and ``sys.exec_prefix`` are | 
|  | 82 | empty, and the path manipulations are skipped; however the import of | 
|  | 83 | :mod:`sitecustomize` is still attempted. | 
|  | 84 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 85 |  | 
|  | 86 | .. data:: PREFIXES | 
|  | 87 |  | 
|  | 88 | A list of prefixes for site package directories | 
|  | 89 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 90 |  | 
|  | 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 Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 97 |  | 
|  | 98 | .. data:: USER_SITE | 
|  | 99 |  | 
|  | 100 | Path to the user site directory for the current Python version or None | 
|  | 101 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 102 |  | 
|  | 103 | .. data:: USER_BASE | 
|  | 104 |  | 
|  | 105 | Path to the base directory for user site directories | 
|  | 106 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 107 |  | 
|  | 108 | .. envvar:: PYTHONNOUSERSITE | 
|  | 109 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 110 |  | 
|  | 111 | .. envvar:: PYTHONUSERBASE | 
|  | 112 |  | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 113 |  | 
|  | 114 | .. function:: addsitedir(sitedir, known_paths=None) | 
|  | 115 |  | 
|  | 116 | Adds a directory to sys.path and processes its pth files. | 
|  | 117 |  | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 118 | .. function:: getsitepackages() | 
|  | 119 |  | 
|  | 120 | Returns a list containing all global site-packages directories | 
|  | 121 | (and possibly site-python). | 
|  | 122 |  | 
| Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 123 | .. versionadded:: 3.2 | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 124 |  | 
|  | 125 | .. function:: getuserbase() | 
|  | 126 |  | 
| Georg Brandl | ef871f6 | 2010-03-12 10:06:40 +0000 | [diff] [blame] | 127 | Returns the "user base" directory path. | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 128 |  | 
| Georg Brandl | ef871f6 | 2010-03-12 10:06:40 +0000 | [diff] [blame] | 129 | 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] | 130 | variable ``USER_BASE`` is not initialized yet, this function will also set | 
|  | 131 | it. | 
|  | 132 |  | 
| Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 133 | .. versionadded:: 3.2 | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 134 |  | 
|  | 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 Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 142 | .. versionadded:: 3.2 | 
| Christian Heimes | 8dc226f | 2008-05-06 23:45:46 +0000 | [diff] [blame] | 143 |  | 
| Georg Brandl | 1824415 | 2009-09-02 20:34:52 +0000 | [diff] [blame] | 144 | .. XXX Update documentation | 
|  | 145 | .. XXX document python -m site --user-base --user-site | 
| Tarek Ziadé | 4a608c0 | 2009-08-20 21:28:05 +0000 | [diff] [blame] | 146 |  |