|
1 | | -"""Hook to allow easy access to site-specific modules. |
| 1 | +"""Append module search paths for third-party packages to sys.path. |
2 | 2 |
|
3 | | -Scripts or modules that need to use site-specific modules should place |
| 3 | +**************************************************************** |
| 4 | +* This module is automatically imported during initialization. * |
| 5 | +**************************************************************** |
4 | 6 |
|
5 | | - import site |
| 7 | +In earlier versions of Python (up to 1.5a3), scripts or modules that |
| 8 | +needed to use site-specific modules would place ``import site'' |
| 9 | +somewhere near the top of their code. Because of the automatic |
| 10 | +import, this is no longer necessary (but code that does it still |
| 11 | +works). |
6 | 12 |
|
7 | | -somewhere near the top of their code. This will append up to two |
8 | | -site-specific paths ($prefix/lib/site-python and |
9 | | -$exec_prefix/lib/site-python) to the module search path. ($prefix |
10 | | -and $exec_prefix are configuration parameters, and both default |
11 | | -to /usr/local; they are accessible in Python as sys.prefix and |
12 | | -sys.exec_prefix). |
| 13 | +This will append site-specific paths to to the module search path. It |
| 14 | +starts with sys.prefix and sys.exec_prefix (if different) and appends |
| 15 | +lib/python<version>/packages. The resulting directory, if it exists, |
| 16 | +is added to sys.path, and also inspected for path configuration files. |
13 | 17 |
|
14 | | -Because of Python's import semantics, it is okay for more than one |
15 | | -module to import site -- only the first one will execute the site |
16 | | -customizations. The directories are only appended to the path if they |
17 | | -exist and are not already on it. |
| 18 | +A path configuration file is a file whose name has the form |
| 19 | +<package>.pth; its contents are additional directories (one per line) |
| 20 | +to be added to sys.path. Non-existing directories (or |
| 21 | +non-directories) are never added to sys.path; no directory is added to |
| 22 | +sys.path more than once. Blank lines and lines beginning with |
| 23 | +\code{#} are skipped. |
18 | 24 |
|
19 | | -Sites that wish to provide site-specific modules should place them in |
20 | | -one of the site specific directories; $prefix/lib/site-python is for |
21 | | -Python source code and $exec_prefix/lib/site-python is for dynamically |
22 | | -loadable extension modules (shared libraries). |
| 25 | +For example, suppose sys.prefix and sys.exec_prefix are set to |
| 26 | +/usr/local and there is a directory /usr/local/python1.5/packages with |
| 27 | +three subdirectories, foo, bar and spam, and two path configuration |
| 28 | +files, foo.pth and bar.pth. Assume foo.pth contains the following: |
| 29 | +
|
| 30 | + # foo package configuration |
| 31 | + foo |
| 32 | + bar |
| 33 | + bletch |
| 34 | +
|
| 35 | +and bar.pth contains: |
| 36 | +
|
| 37 | + # bar package configuration |
| 38 | + bar |
| 39 | +
|
| 40 | +Then the following directories are added to sys.path, in this order: |
| 41 | +
|
| 42 | + /usr/local/lib/python1.5/packages/bar |
| 43 | + /usr/local/lib/python1.5/packages/foo |
| 44 | +
|
| 45 | +Note that bletch is omitted because it doesn't exist; bar precedes foo |
| 46 | +because bar.pth comes alphabetically before foo.pth; and spam is |
| 47 | +omitted because it is not mentioned in either path configuration file. |
23 | 48 |
|
24 | 49 | After these path manipulations, an attempt is made to import a module |
25 | | -named sitecustomize, which can perform arbitrary site-specific |
26 | | -customizations. If this import fails with an ImportError exception, |
27 | | -it is ignored. |
| 50 | +named sitecustomize, which can perform arbitrary additional |
| 51 | +site-specific customizations. If this import fails with an |
| 52 | +ImportError exception, it is silently ignored. |
28 | 53 |
|
29 | | -Note that for non-Unix systems, sys.prefix and sys.exec_prefix are |
30 | | -empty, and the path manipulations are skipped; however the import of |
31 | | -sitecustomize is still attempted. |
| 54 | +Note that for some non-Unix systems, sys.prefix and sys.exec_prefix |
| 55 | +are empty, and then the path manipulations are skipped; however the |
| 56 | +import of sitecustomize is still attempted. |
32 | 57 |
|
33 | | -XXX Any suggestions as to how to handle this for non-Unix systems??? |
34 | 58 | """ |
35 | 59 |
|
| 60 | +print "loading site.py" |
| 61 | + |
36 | 62 | import sys, os |
37 | 63 |
|
38 | | -for prefix in sys.prefix, sys.exec_prefix: |
| 64 | +def addsitedir(sitedir): |
| 65 | + if sitedir not in sys.path: |
| 66 | + sys.path.append(sitedir) # Add path component |
| 67 | + try: |
| 68 | + names = os.listdir(sitedir) |
| 69 | + except os.error: |
| 70 | + return |
| 71 | + names = map(os.path.normcase, names) |
| 72 | + names.sort() |
| 73 | + for name in names: |
| 74 | + if name[-4:] == ".pth": |
| 75 | + addpackage(sitedir, name) |
| 76 | + |
| 77 | +def addpackage(sitedir, name): |
| 78 | + fullname = os.path.join(sitedir, name) |
| 79 | + try: |
| 80 | + f = open(fullname) |
| 81 | + except IOError: |
| 82 | + return |
| 83 | + while 1: |
| 84 | + dir = f.readline() |
| 85 | + if not dir: |
| 86 | + break |
| 87 | + if dir[0] == '#': |
| 88 | + continue |
| 89 | + if dir[-1] == '\n': |
| 90 | + dir = dir[:-1] |
| 91 | + dir = os.path.join(sitedir, dir) |
| 92 | + if dir not in sys.path and os.path.exists(dir): |
| 93 | + sys.path.append(dir) |
| 94 | + |
| 95 | +prefixes = [sys.prefix] |
| 96 | +if sys.exec_prefix != sys.prefix: |
| 97 | + prefixes.append(sys.exec_prefix) |
| 98 | +for prefix in prefixes: |
39 | 99 | if prefix: |
40 | | - sitedir = os.path.join(prefix, os.path.join("lib", "site-python")) |
41 | | - if sitedir not in sys.path and os.path.isdir(sitedir): |
42 | | - sys.path.append(sitedir) # Add path component |
| 100 | + if sys.platform[:3] in ('win', 'mac'): |
| 101 | + sitedir = prefix |
| 102 | + else: |
| 103 | + sitedir = os.path.join(prefix, |
| 104 | + "lib", |
| 105 | + "python" + sys.version[:3], |
| 106 | + "packages") |
| 107 | + if os.path.isdir(sitedir): |
| 108 | + addsitedir(sitedir) |
43 | 109 |
|
44 | 110 | try: |
45 | 111 | import sitecustomize # Run arbitrary site specific code |
46 | 112 | except ImportError: |
47 | 113 | pass # No site customization module |
| 114 | + |
| 115 | +def _test(): |
| 116 | + print "sys.path = [" |
| 117 | + for dir in sys.path: |
| 118 | + print " %s," % `dir` |
| 119 | + print "]" |
| 120 | + |
| 121 | +if __name__ == '__main__': |
| 122 | + _test() |
0 commit comments