Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{site}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 2 | \declaremodule{standard}{site} |
| 3 | |
| 4 | \modulesynopsis{A standard way to reference site-specific modules.} |
| 5 | |
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 | \strong{This module is automatically imported during initialization.} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 9 | In earlier versions of Python (up to and including 1.5a3), scripts or |
| 10 | modules that needed to use site-specific modules would place |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 11 | \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] | 12 | longer necessary. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 14 | This will append site-specific paths to to the module search path. |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 15 | \indexiii{module}{search}{path} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 16 | |
| 17 | It starts by constructing up to four directories from a head and a |
| 18 | tail part. For the head part, it uses \code{sys.prefix} and |
| 19 | \code{sys.exec_prefix}; empty heads are skipped. For |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 20 | the tail part, it uses the empty string (on Macintosh or Windows) or |
| 21 | it uses first \file{lib/python\var{version}/site-packages} and then |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 22 | \file{lib/site-python} (on \UNIX{}). For each of the distinct |
| 23 | head-tail combinations, it sees if it refers to an existing directory, |
| 24 | and if so, adds to \code{sys.path}, and also inspected for path |
| 25 | configuration files. |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 26 | \indexii{site-python}{directory} |
Guido van Rossum | 9cf4e2b | 1997-09-08 02:02:37 +0000 | [diff] [blame] | 27 | \indexii{site-packages}{directory} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 28 | |
| 29 | A path configuration file is a file whose name has the form |
| 30 | \file{\var{package}.pth}; its contents are additional items (one |
| 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 |
| 35 | \code{\#} are skipped. |
| 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 | 2de75ec | 1998-04-09 14:12:11 +0000 | [diff] [blame] | 41 | installed in \file{/usr/local/lib/python1.5} (note that only the first |
| 42 | three characters of \code{sys.version} are used to form the path |
| 43 | name). Suppose this has a subdirectory |
| 44 | \file{/usr/local/lib/python1.5/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} |
Guido van Rossum | 505d80b | 1998-02-19 21:02:32 +0000 | [diff] [blame] | 69 | /usr/local/lib/python1.5/site-packages/bar |
| 70 | /usr/local/lib/python1.5/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. |