blob: a9f5480114ed970759019098d3228cb88bf87da2 [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 Rossum0d8fcb21998-01-13 18:32:40 +000013This will append site-specific paths to to the module search path. On
14Unix, it starts with sys.prefix and sys.exec_prefix (if different) and
15appends lib/python<version>/site-packages as well as lib/site-python.
16On 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 Rossum62b297b1997-09-08 02:14:09 +000018resulting directories, if they exist, are appended to sys.path, and
19also inspected for path configuration files.
Guido van Rossume57c96e1996-08-17 19:56:26 +000020
Guido van Rossumf30bec71997-08-29 22:30:45 +000021A path configuration file is a file whose name has the form
22<package>.pth; its contents are additional directories (one per line)
23to be added to sys.path. Non-existing directories (or
24non-directories) are never added to sys.path; no directory is added to
25sys.path more than once. Blank lines and lines beginning with
26\code{#} are skipped.
Guido van Rossume57c96e1996-08-17 19:56:26 +000027
Guido van Rossumf30bec71997-08-29 22:30:45 +000028For example, suppose sys.prefix and sys.exec_prefix are set to
Guido van Rossume7201761998-11-25 15:57:47 +000029/usr/local and there is a directory /usr/local/lib/python1.5/site-packages
Guido van Rossum62b297b1997-09-08 02:14:09 +000030with three subdirectories, foo, bar and spam, and two path
31configuration files, foo.pth and bar.pth. Assume foo.pth contains the
32following:
Guido van Rossumf30bec71997-08-29 22:30:45 +000033
34 # foo package configuration
35 foo
36 bar
37 bletch
38
39and bar.pth contains:
40
41 # bar package configuration
42 bar
43
44Then the following directories are added to sys.path, in this order:
45
Guido van Rossum62b297b1997-09-08 02:14:09 +000046 /usr/local/lib/python1.5/site-packages/bar
47 /usr/local/lib/python1.5/site-packages/foo
Guido van Rossumf30bec71997-08-29 22:30:45 +000048
49Note that bletch is omitted because it doesn't exist; bar precedes foo
50because bar.pth comes alphabetically before foo.pth; and spam is
51omitted because it is not mentioned in either path configuration file.
Guido van Rossume57c96e1996-08-17 19:56:26 +000052
53After these path manipulations, an attempt is made to import a module
Guido van Rossumf30bec71997-08-29 22:30:45 +000054named sitecustomize, which can perform arbitrary additional
55site-specific customizations. If this import fails with an
56ImportError exception, it is silently ignored.
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:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064 sys.path.append(sitedir) # Add path component
Guido van Rossumf30bec71997-08-29 22:30:45 +000065 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000066 names = os.listdir(sitedir)
Guido van Rossumf30bec71997-08-29 22:30:45 +000067 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000068 return
Guido van Rossumf30bec71997-08-29 22:30:45 +000069 names = map(os.path.normcase, names)
70 names.sort()
71 for name in names:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000072 if name[-4:] == ".pth":
73 addpackage(sitedir, name)
Guido van Rossumf30bec71997-08-29 22:30:45 +000074
75def addpackage(sitedir, name):
76 fullname = os.path.join(sitedir, name)
77 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000078 f = open(fullname)
Guido van Rossumf30bec71997-08-29 22:30:45 +000079 except IOError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000080 return
Guido van Rossumf30bec71997-08-29 22:30:45 +000081 while 1:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000082 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 Rossumf30bec71997-08-29 22:30:45 +000092
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 Rossum45e2fbc1998-03-26 21:13:24 +000098 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 Rossume57c96e1996-08-17 19:56:26 +0000109
Guido van Rossumd89fa0c1998-08-07 18:01:14 +0000110# Define new built-ins 'quit' and 'exit'.
111# These are simply strings that display a hint on how to exit.
112if os.sep == ':':
113 exit = 'Use Cmd-Q to quit.'
114elif os.sep == '\\':
115 exit = 'Use Ctrl-Z plus Return to exit.'
116else:
117 exit = 'Use Ctrl-D (i.e. EOF) to exit.'
118import __builtin__
119__builtin__.quit = __builtin__.exit = exit
120del exit
121
Guido van Rossumd1252392000-09-05 04:39:55 +0000122# interactive prompt objects for printing the license text, a list of
123# contributors and the copyright notice.
124class _Printer:
125 MAXLINES = 23
126
127 def __init__(self, s):
128 self.__lines = s.split('\n')
129 self.__linecnt = len(self.__lines)
130
131 def __repr__(self):
132 prompt = 'Hit Return for more, or q (and Return) to quit: '
133 lineno = 0
134 while 1:
135 try:
136 for i in range(lineno, lineno + self.MAXLINES):
137 print self.__lines[i]
138 except IndexError:
139 break
140 else:
141 lineno += self.MAXLINES
142 key = None
143 while key is None:
144 key = raw_input(prompt)
145 if key not in ('', 'q'):
146 key = None
147 if key == 'q':
148 break
149 return ''
150
151__builtin__.copyright = _Printer(sys.copyright)
152__builtin__.credits = _Printer(
153 '''Python development is led by BeOpen PythonLabs (www.pythonlabs.com).''')
154
155def make_license(filename):
156 try:
157 return _Printer(open(filename).read())
158 except IOError:
159 return None
160
161here = os.path.dirname(os.__file__)
162for dir in here, os.path.join(here, os.pardir), os.curdir:
163 for file in "LICENSE.txt", "LICENSE":
164 lic = make_license(os.path.join(dir, file))
165 if lic:
166 break
167 if lic:
168 __builtin__.license = lic
169 break
170else:
171 __builtin__.license = _Printer('See http://hdl.handle.net/1895.22/1012')
172
173
Fredrik Lundh3fded4b2000-07-15 20:58:44 +0000174# Set the string encoding used by the Unicode implementation. The
175# default is 'ascii', but if you're willing to experiment, you can
176# change this.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000177
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000178encoding = "ascii" # Default value set by _PyUnicode_Init()
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000179
180if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000181 # Enable to support locale aware default string encodings.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000182 import locale
183 loc = locale.getdefaultlocale()
184 if loc[1]:
185 encoding = loc[1]
186
187if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000188 # Enable to switch off string to Unicode coercion and implicit
189 # Unicode to string conversion.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000190 encoding = "undefined"
191
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000192if encoding != "ascii":
193 sys.setdefaultencoding(encoding)
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000194
195#
196# Run custom site specific code, if available.
197#
Guido van Rossume57c96e1996-08-17 19:56:26 +0000198try:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000199 import sitecustomize
Guido van Rossume57c96e1996-08-17 19:56:26 +0000200except ImportError:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000201 pass
202
203#
204# Remove sys.setdefaultencoding() so that users cannot change the
205# encoding after initialization.
206#
207del sys.setdefaultencoding
Guido van Rossumf30bec71997-08-29 22:30:45 +0000208
209def _test():
210 print "sys.path = ["
211 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000212 print " %s," % `dir`
Guido van Rossumf30bec71997-08-29 22:30:45 +0000213 print "]"
214
215if __name__ == '__main__':
216 _test()