Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{site} --- |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 2 | Site-specific configuration hook} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{site} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{A standard way to reference site-specific modules.} |
| 6 | |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 8 | \strong{This module is automatically imported during initialization.} |
Fred Drake | ff381e1 | 2003-12-30 22:51:32 +0000 | [diff] [blame] | 9 | The automatic import can be suppressed using the interpreter's |
| 10 | \programopt{-S} option. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 11 | |
Fred Drake | ff381e1 | 2003-12-30 22:51:32 +0000 | [diff] [blame] | 12 | Importing this module will append site-specific paths to the module |
| 13 | search path. |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 14 | \indexiii{module}{search}{path} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 15 | |
| 16 | It starts by constructing up to four directories from a head and a |
| 17 | tail part. For the head part, it uses \code{sys.prefix} and |
| 18 | \code{sys.exec_prefix}; empty heads are skipped. For |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 19 | the tail part, it uses the empty string (on Windows) or |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 20 | it uses first \file{lib/python\shortversion/site-packages} and then |
Brett Cannon | 7706c2d | 2005-02-13 22:50:04 +0000 | [diff] [blame] | 21 | \file{lib/site-python} (on \UNIX and Macintosh). For each of the distinct |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 22 | head-tail combinations, it sees if it refers to an existing directory, |
Andrew M. Kuchling | d6d7cfa | 2002-12-30 03:08:27 +0000 | [diff] [blame] | 23 | and if so, adds it to \code{sys.path} and also inspects the newly added |
| 24 | path for configuration files. |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 25 | \indexii{site-python}{directory} |
Guido van Rossum | 9cf4e2b | 1997-09-08 02:02:37 +0000 | [diff] [blame] | 26 | \indexii{site-packages}{directory} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 27 | |
| 28 | A path configuration file is a file whose name has the form |
Brett Cannon | 508c57d | 2004-03-20 21:41:28 +0000 | [diff] [blame] | 29 | \file{\var{package}.pth} and exists in one of the four directories |
| 30 | mentioned above; its contents are additional items (one |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 31 | per line) to be added to \code{sys.path}. Non-existing items are |
| 32 | never added to \code{sys.path}, but no check is made that the item |
| 33 | refers to a directory (rather than a file). No item is added to |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 34 | \code{sys.path} more than once. Blank lines and lines beginning with |
Martin v. Löwis | a177c40 | 2001-01-11 22:07:25 +0000 | [diff] [blame] | 35 | \code{\#} are skipped. Lines starting with \code{import} are executed. |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 36 | \index{package} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 37 | \indexiii{path}{configuration}{file} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 38 | |
| 39 | For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are |
Fred Drake | b0f77d6 | 1998-02-16 20:58:58 +0000 | [diff] [blame] | 40 | set to \file{/usr/local}. The Python \version\ library is then |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 41 | installed in \file{/usr/local/lib/python\shortversion} (where only the |
| 42 | first three characters of \code{sys.version} are used to form the |
| 43 | installation path name). Suppose this has a subdirectory |
| 44 | \file{/usr/local/lib/python\shortversion/site-packages} with three |
Fred Drake | b0f77d6 | 1998-02-16 20:58:58 +0000 | [diff] [blame] | 45 | subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two |
| 46 | path configuration files, \file{foo.pth} and \file{bar.pth}. Assume |
| 47 | \file{foo.pth} contains the following: |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 48 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 49 | \begin{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 50 | # foo package configuration |
| 51 | |
| 52 | foo |
| 53 | bar |
| 54 | bletch |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 55 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 56 | |
| 57 | and \file{bar.pth} contains: |
| 58 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 59 | \begin{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 60 | # bar package configuration |
| 61 | |
| 62 | bar |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 63 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 64 | |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 65 | Then the following directories are added to \code{sys.path}, in this |
| 66 | order: |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 67 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 68 | \begin{verbatim} |
Neal Norwitz | ba902fd | 2002-02-19 02:58:54 +0000 | [diff] [blame] | 69 | /usr/local/lib/python2.3/site-packages/bar |
| 70 | /usr/local/lib/python2.3/site-packages/foo |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 71 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 72 | |
| 73 | Note that \file{bletch} is omitted because it doesn't exist; the |
| 74 | \file{bar} directory precedes the \file{foo} directory because |
| 75 | \file{bar.pth} comes alphabetically before \file{foo.pth}; and |
| 76 | \file{spam} is omitted because it is not mentioned in either path |
| 77 | configuration file. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 78 | |
| 79 | After these path manipulations, an attempt is made to import a module |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 80 | named \module{sitecustomize}\refmodindex{sitecustomize}, which can |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 81 | perform arbitrary site-specific customizations. If this import fails |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 82 | with an \exception{ImportError} exception, it is silently ignored. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 83 | |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 84 | Note that for some non-\UNIX{} systems, \code{sys.prefix} and |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 85 | \code{sys.exec_prefix} are empty, and the path manipulations are |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 86 | skipped; however the import of |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 87 | \module{sitecustomize}\refmodindex{sitecustomize} is still attempted. |