blob: 7c387f1446dabd4cd0f386ed86327f96752ad9b9 [file] [log] [blame]
Guido van Rossumf30bec71997-08-29 22:30:45 +00001"""Append module search paths for third-party packages to sys.path.
Guido van Rossume57c96e1996-08-17 19:56:26 +00002
Guido van Rossumf30bec71997-08-29 22:30:45 +00003****************************************************************
4* This module is automatically imported during initialization. *
5****************************************************************
Guido van Rossume57c96e1996-08-17 19:56:26 +00006
Guido van Rossumf30bec71997-08-29 22:30:45 +00007In earlier versions of Python (up to 1.5a3), scripts or modules that
8needed to use site-specific modules would place ``import site''
9somewhere near the top of their code. Because of the automatic
10import, this is no longer necessary (but code that does it still
11works).
Guido van Rossume57c96e1996-08-17 19:56:26 +000012
Guido van Rossumf30bec71997-08-29 22:30:45 +000013This will append site-specific paths to to the module search path. It
14starts with sys.prefix and sys.exec_prefix (if different) and appends
15lib/python<version>/packages. The resulting directory, if it exists,
16is added to sys.path, and also inspected for path configuration files.
Guido van Rossume57c96e1996-08-17 19:56:26 +000017
Guido van Rossumf30bec71997-08-29 22:30:45 +000018A path configuration file is a file whose name has the form
19<package>.pth; its contents are additional directories (one per line)
20to be added to sys.path. Non-existing directories (or
21non-directories) are never added to sys.path; no directory is added to
22sys.path more than once. Blank lines and lines beginning with
23\code{#} are skipped.
Guido van Rossume57c96e1996-08-17 19:56:26 +000024
Guido van Rossumf30bec71997-08-29 22:30:45 +000025For 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
27three subdirectories, foo, bar and spam, and two path configuration
28files, foo.pth and bar.pth. Assume foo.pth contains the following:
29
30 # foo package configuration
31 foo
32 bar
33 bletch
34
35and bar.pth contains:
36
37 # bar package configuration
38 bar
39
40Then 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
45Note that bletch is omitted because it doesn't exist; bar precedes foo
46because bar.pth comes alphabetically before foo.pth; and spam is
47omitted because it is not mentioned in either path configuration file.
Guido van Rossume57c96e1996-08-17 19:56:26 +000048
49After these path manipulations, an attempt is made to import a module
Guido van Rossumf30bec71997-08-29 22:30:45 +000050named sitecustomize, which can perform arbitrary additional
51site-specific customizations. If this import fails with an
52ImportError exception, it is silently ignored.
Guido van Rossume57c96e1996-08-17 19:56:26 +000053
Guido van Rossumf30bec71997-08-29 22:30:45 +000054Note that for some non-Unix systems, sys.prefix and sys.exec_prefix
55are empty, and then the path manipulations are skipped; however the
56import of sitecustomize is still attempted.
Guido van Rossume57c96e1996-08-17 19:56:26 +000057
Guido van Rossume57c96e1996-08-17 19:56:26 +000058"""
59
60import sys, os
61
Guido van Rossumf30bec71997-08-29 22:30:45 +000062def addsitedir(sitedir):
63 if sitedir not in sys.path:
64 sys.path.append(sitedir) # Add path component
65 try:
66 names = os.listdir(sitedir)
67 except os.error:
68 return
69 names = map(os.path.normcase, names)
70 names.sort()
71 for name in names:
72 if name[-4:] == ".pth":
73 addpackage(sitedir, name)
74
75def addpackage(sitedir, name):
76 fullname = os.path.join(sitedir, name)
77 try:
78 f = open(fullname)
79 except IOError:
80 return
81 while 1:
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)
92
93prefixes = [sys.prefix]
94if sys.exec_prefix != sys.prefix:
95 prefixes.append(sys.exec_prefix)
96for prefix in prefixes:
Guido van Rossume57c96e1996-08-17 19:56:26 +000097 if prefix:
Guido van Rossumf30bec71997-08-29 22:30:45 +000098 if sys.platform[:3] in ('win', 'mac'):
99 sitedir = prefix
100 else:
101 sitedir = os.path.join(prefix,
102 "lib",
103 "python" + sys.version[:3],
104 "packages")
105 if os.path.isdir(sitedir):
106 addsitedir(sitedir)
Guido van Rossume57c96e1996-08-17 19:56:26 +0000107
108try:
109 import sitecustomize # Run arbitrary site specific code
110except ImportError:
111 pass # No site customization module
Guido van Rossumf30bec71997-08-29 22:30:45 +0000112
113def _test():
114 print "sys.path = ["
115 for dir in sys.path:
116 print " %s," % `dir`
117 print "]"
118
119if __name__ == '__main__':
120 _test()