blob: aedcafab626100f9737f237963631321a940ed18 [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
Guido van Rossum62b297b1997-09-08 02:14:09 +000015lib/python<version>/site-packages as well as lib/site-python. The
16resulting directories, if they exist, are appended to sys.path, and
17also inspected for path configuration files.
Guido van Rossume57c96e1996-08-17 19:56:26 +000018
Guido van Rossumf30bec71997-08-29 22:30:45 +000019A path configuration file is a file whose name has the form
20<package>.pth; its contents are additional directories (one per line)
21to be added to sys.path. Non-existing directories (or
22non-directories) are never added to sys.path; no directory is added to
23sys.path more than once. Blank lines and lines beginning with
24\code{#} are skipped.
Guido van Rossume57c96e1996-08-17 19:56:26 +000025
Guido van Rossumf30bec71997-08-29 22:30:45 +000026For example, suppose sys.prefix and sys.exec_prefix are set to
Guido van Rossum62b297b1997-09-08 02:14:09 +000027/usr/local and there is a directory /usr/local/python1.5/site-packages
28with three subdirectories, foo, bar and spam, and two path
29configuration files, foo.pth and bar.pth. Assume foo.pth contains the
30following:
Guido van Rossumf30bec71997-08-29 22:30:45 +000031
32 # foo package configuration
33 foo
34 bar
35 bletch
36
37and bar.pth contains:
38
39 # bar package configuration
40 bar
41
42Then the following directories are added to sys.path, in this order:
43
Guido van Rossum62b297b1997-09-08 02:14:09 +000044 /usr/local/lib/python1.5/site-packages/bar
45 /usr/local/lib/python1.5/site-packages/foo
Guido van Rossumf30bec71997-08-29 22:30:45 +000046
47Note that bletch is omitted because it doesn't exist; bar precedes foo
48because bar.pth comes alphabetically before foo.pth; and spam is
49omitted because it is not mentioned in either path configuration file.
Guido van Rossume57c96e1996-08-17 19:56:26 +000050
51After these path manipulations, an attempt is made to import a module
Guido van Rossumf30bec71997-08-29 22:30:45 +000052named sitecustomize, which can perform arbitrary additional
53site-specific customizations. If this import fails with an
54ImportError exception, it is silently ignored.
Guido van Rossume57c96e1996-08-17 19:56:26 +000055
Guido van Rossumf30bec71997-08-29 22:30:45 +000056Note that for some non-Unix systems, sys.prefix and sys.exec_prefix
57are empty, and then the path manipulations are skipped; however the
58import of sitecustomize is still attempted.
Guido van Rossume57c96e1996-08-17 19:56:26 +000059
Guido van Rossume57c96e1996-08-17 19:56:26 +000060"""
61
62import sys, os
63
Guido van Rossumf30bec71997-08-29 22:30:45 +000064def 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
77def 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
95prefixes = [sys.prefix]
96if sys.exec_prefix != sys.prefix:
97 prefixes.append(sys.exec_prefix)
98for prefix in prefixes:
Guido van Rossume57c96e1996-08-17 19:56:26 +000099 if prefix:
Guido van Rossumdc5d07d1997-09-03 23:12:18 +0000100 if os.sep == '/':
Guido van Rossumad87d3e1997-09-03 21:41:30 +0000101 sitedirs = [os.path.join(prefix,
102 "lib",
103 "python" + sys.version[:3],
Guido van Rossum62b297b1997-09-08 02:14:09 +0000104 "site-packages"),
Guido van Rossumad87d3e1997-09-03 21:41:30 +0000105 os.path.join(prefix, "lib", "site-python")]
Guido van Rossumdc5d07d1997-09-03 23:12:18 +0000106 else:
107 sitedirs = [prefix]
Guido van Rossumad87d3e1997-09-03 21:41:30 +0000108 for sitedir in sitedirs:
109 if os.path.isdir(sitedir):
110 addsitedir(sitedir)
Guido van Rossume57c96e1996-08-17 19:56:26 +0000111
112try:
113 import sitecustomize # Run arbitrary site specific code
114except ImportError:
115 pass # No site customization module
Guido van Rossumf30bec71997-08-29 22:30:45 +0000116
117def _test():
118 print "sys.path = ["
119 for dir in sys.path:
120 print " %s," % `dir`
121 print "]"
122
123if __name__ == '__main__':
124 _test()