Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{site}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-site} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 3 | \stmodindex{site} |
| 4 | |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 5 | \strong{This module is automatically imported during initialization.} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 7 | In earlier versions of Python (up to and including 1.5a3), scripts or |
| 8 | modules that needed to use site-specific modules would place |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 9 | \samp{import site} somewhere near the top of their code. This is no |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 10 | longer necessary. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 12 | This will append site-specific paths to to the module search path. |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 13 | \indexiii{module}{search}{path} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 14 | |
| 15 | It starts by constructing up to four directories from a head and a |
| 16 | tail part. For the head part, it uses \code{sys.prefix} and |
| 17 | \code{sys.exec_prefix}; empty heads are skipped. For |
| 18 | the tail part, it uses the empty string (on Mac or Windows) or it uses |
Guido van Rossum | 9cf4e2b | 1997-09-08 02:02:37 +0000 | [diff] [blame] | 19 | first \file{lib/python\var{version}/site-packages} and then |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 20 | \file{lib/site-python} (on \UNIX{}). For each of the distinct |
| 21 | head-tail combinations, it sees if it refers to an existing directory, |
| 22 | and if so, adds to \code{sys.path}, and also inspected for path |
| 23 | configuration files. |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 24 | \indexii{site-python}{directory} |
Guido van Rossum | 9cf4e2b | 1997-09-08 02:02:37 +0000 | [diff] [blame] | 25 | \indexii{site-packages}{directory} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 26 | |
| 27 | A path configuration file is a file whose name has the form |
| 28 | \file{\var{package}.pth}; its contents are additional items (one |
| 29 | per line) to be added to \code{sys.path}. Non-existing items are |
| 30 | never added to \code{sys.path}, but no check is made that the item |
| 31 | 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] | 32 | \code{sys.path} more than once. Blank lines and lines beginning with |
| 33 | \code{\#} are skipped. |
| 34 | \index{package} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 35 | \indexiii{path}{configuration}{file} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 36 | |
| 37 | For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are |
Fred Drake | b0f77d6 | 1998-02-16 20:58:58 +0000 | [diff] [blame] | 38 | set to \file{/usr/local}. The Python \version\ library is then |
| 39 | installed in \file{/usr/local/lib/python\version}. Suppose this has a |
Guido van Rossum | 505d80b | 1998-02-19 21:02:32 +0000 | [diff] [blame] | 40 | subdirectory \file{/usr/local/lib/python\version/site-packages} with three |
Fred Drake | b0f77d6 | 1998-02-16 20:58:58 +0000 | [diff] [blame] | 41 | subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two |
| 42 | path configuration files, \file{foo.pth} and \file{bar.pth}. Assume |
| 43 | \file{foo.pth} contains the following: |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 44 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 45 | \begin{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 46 | # foo package configuration |
| 47 | |
| 48 | foo |
| 49 | bar |
| 50 | bletch |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 51 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 52 | |
| 53 | and \file{bar.pth} contains: |
| 54 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 55 | \begin{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 56 | # bar package configuration |
| 57 | |
| 58 | bar |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 59 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 60 | |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 61 | Then the following directories are added to \code{sys.path}, in this |
| 62 | order: |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 63 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 64 | \begin{verbatim} |
Guido van Rossum | 505d80b | 1998-02-19 21:02:32 +0000 | [diff] [blame] | 65 | /usr/local/lib/python1.5/site-packages/bar |
| 66 | /usr/local/lib/python1.5/site-packages/foo |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 67 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 68 | |
| 69 | Note that \file{bletch} is omitted because it doesn't exist; the |
| 70 | \file{bar} directory precedes the \file{foo} directory because |
| 71 | \file{bar.pth} comes alphabetically before \file{foo.pth}; and |
| 72 | \file{spam} is omitted because it is not mentioned in either path |
| 73 | configuration file. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 74 | |
| 75 | After these path manipulations, an attempt is made to import a module |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 76 | named \code{sitecustomize}\refmodindex{sitecustomize}, which can |
| 77 | perform arbitrary site-specific customizations. If this import fails |
| 78 | with an \code{ImportError} exception, it is silently ignored. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 79 | |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 80 | Note that for some non-\UNIX{} systems, \code{sys.prefix} and |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 81 | \code{sys.exec_prefix} are empty, and the path manipulations are |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 82 | skipped; however the import of |
| 83 | \code{sitecustomize}\refmodindex{sitecustomize} is still attempted. |