Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 1 | """Append module search paths for third-party packages to sys.path. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 3 | **************************************************************** |
| 4 | * This module is automatically imported during initialization. * |
| 5 | **************************************************************** |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 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). |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 0d8fcb2 | 1998-01-13 18:32:40 +0000 | [diff] [blame] | 13 | This will append site-specific paths to to the module search path. On |
| 14 | Unix, it starts with sys.prefix and sys.exec_prefix (if different) and |
| 15 | appends lib/python<version>/site-packages as well as lib/site-python. |
| 16 | On other platforms (mainly Mac and Windows), it uses just sys.prefix |
| 17 | (and sys.exec_prefix, if different, but this is unlikely). The |
Guido van Rossum | 62b297b | 1997-09-08 02:14:09 +0000 | [diff] [blame] | 18 | resulting directories, if they exist, are appended to sys.path, and |
| 19 | also inspected for path configuration files. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 21 | A path configuration file is a file whose name has the form |
| 22 | <package>.pth; its contents are additional directories (one per line) |
| 23 | to be added to sys.path. Non-existing directories (or |
| 24 | non-directories) are never added to sys.path; no directory is added to |
| 25 | sys.path more than once. Blank lines and lines beginning with |
| 26 | \code{#} are skipped. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 28 | For example, suppose sys.prefix and sys.exec_prefix are set to |
Guido van Rossum | e720176 | 1998-11-25 15:57:47 +0000 | [diff] [blame] | 29 | /usr/local and there is a directory /usr/local/lib/python1.5/site-packages |
Guido van Rossum | 62b297b | 1997-09-08 02:14:09 +0000 | [diff] [blame] | 30 | with three subdirectories, foo, bar and spam, and two path |
| 31 | configuration files, foo.pth and bar.pth. Assume foo.pth contains the |
| 32 | following: |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 33 | |
| 34 | # foo package configuration |
| 35 | foo |
| 36 | bar |
| 37 | bletch |
| 38 | |
| 39 | and bar.pth contains: |
| 40 | |
| 41 | # bar package configuration |
| 42 | bar |
| 43 | |
| 44 | Then the following directories are added to sys.path, in this order: |
| 45 | |
Guido van Rossum | 62b297b | 1997-09-08 02:14:09 +0000 | [diff] [blame] | 46 | /usr/local/lib/python1.5/site-packages/bar |
| 47 | /usr/local/lib/python1.5/site-packages/foo |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 48 | |
| 49 | Note that bletch is omitted because it doesn't exist; bar precedes foo |
| 50 | because bar.pth comes alphabetically before foo.pth; and spam is |
| 51 | omitted because it is not mentioned in either path configuration file. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 52 | |
| 53 | After these path manipulations, an attempt is made to import a module |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 54 | named sitecustomize, which can perform arbitrary additional |
| 55 | site-specific customizations. If this import fails with an |
| 56 | ImportError exception, it is silently ignored. |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 58 | """ |
| 59 | |
| 60 | import sys, os |
| 61 | |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 62 | def addsitedir(sitedir): |
| 63 | if sitedir not in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 64 | sys.path.append(sitedir) # Add path component |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 65 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 66 | names = os.listdir(sitedir) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 67 | except os.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 68 | return |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 69 | names = map(os.path.normcase, names) |
| 70 | names.sort() |
| 71 | for name in names: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 72 | if name[-4:] == ".pth": |
| 73 | addpackage(sitedir, name) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 74 | |
| 75 | def addpackage(sitedir, name): |
| 76 | fullname = os.path.join(sitedir, name) |
| 77 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 78 | f = open(fullname) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 79 | except IOError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 80 | return |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 81 | while 1: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 82 | dir = f.readline() |
| 83 | if not dir: |
| 84 | break |
| 85 | if dir[0] == '#': |
| 86 | continue |
| 87 | if dir[-1] == '\n': |
| 88 | dir = dir[:-1] |
| 89 | dir = os.path.join(sitedir, dir) |
| 90 | if dir not in sys.path and os.path.exists(dir): |
| 91 | sys.path.append(dir) |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 92 | |
| 93 | prefixes = [sys.prefix] |
| 94 | if sys.exec_prefix != sys.prefix: |
| 95 | prefixes.append(sys.exec_prefix) |
| 96 | for prefix in prefixes: |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 97 | if prefix: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 98 | if os.sep == '/': |
| 99 | sitedirs = [os.path.join(prefix, |
| 100 | "lib", |
| 101 | "python" + sys.version[:3], |
| 102 | "site-packages"), |
| 103 | os.path.join(prefix, "lib", "site-python")] |
| 104 | else: |
| 105 | sitedirs = [prefix] |
| 106 | for sitedir in sitedirs: |
| 107 | if os.path.isdir(sitedir): |
| 108 | addsitedir(sitedir) |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | d89fa0c | 1998-08-07 18:01:14 +0000 | [diff] [blame] | 110 | # Define new built-ins 'quit' and 'exit'. |
| 111 | # These are simply strings that display a hint on how to exit. |
| 112 | if os.sep == ':': |
| 113 | exit = 'Use Cmd-Q to quit.' |
| 114 | elif os.sep == '\\': |
| 115 | exit = 'Use Ctrl-Z plus Return to exit.' |
| 116 | else: |
| 117 | exit = 'Use Ctrl-D (i.e. EOF) to exit.' |
| 118 | import __builtin__ |
| 119 | __builtin__.quit = __builtin__.exit = exit |
| 120 | del exit |
| 121 | |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 122 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 123 | import sitecustomize # Run arbitrary site specific code |
Guido van Rossum | e57c96e | 1996-08-17 19:56:26 +0000 | [diff] [blame] | 124 | except ImportError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 125 | pass # No site customization module |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 126 | |
| 127 | def _test(): |
| 128 | print "sys.path = [" |
| 129 | for dir in sys.path: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 130 | print " %s," % `dir` |
Guido van Rossum | f30bec7 | 1997-08-29 22:30:45 +0000 | [diff] [blame] | 131 | print "]" |
| 132 | |
| 133 | if __name__ == '__main__': |
| 134 | _test() |