blob: c92e98d88949d6c0d7ba8ca69b1ba8730c7102fa [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
Fred Drake38cb9f12000-09-28 16:52:36 +000062def makepath(*paths):
63 dir = os.path.join(*paths)
64 return os.path.normcase(os.path.abspath(dir))
65
66L = sys.modules.values()
67for m in L:
68 if hasattr(m, "__file__"):
69 m.__file__ = makepath(m.__file__)
70del m, L
71
72# This ensures that the initial path provided by the interpreter contains
73# only absolute pathnames, even if we're running from the build directory.
74L = []
75for dir in sys.path:
76 dir = makepath(dir)
77 if dir not in L:
78 L.append(dir)
79sys.path[:] = L
80del dir, L
81
Guido van Rossumf30bec71997-08-29 22:30:45 +000082def addsitedir(sitedir):
Fred Drake38cb9f12000-09-28 16:52:36 +000083 sitedir = makepath(sitedir)
Guido van Rossumf30bec71997-08-29 22:30:45 +000084 if sitedir not in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000085 sys.path.append(sitedir) # Add path component
Guido van Rossumf30bec71997-08-29 22:30:45 +000086 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000087 names = os.listdir(sitedir)
Guido van Rossumf30bec71997-08-29 22:30:45 +000088 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000089 return
Guido van Rossumf30bec71997-08-29 22:30:45 +000090 names = map(os.path.normcase, names)
91 names.sort()
92 for name in names:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000093 if name[-4:] == ".pth":
94 addpackage(sitedir, name)
Guido van Rossumf30bec71997-08-29 22:30:45 +000095
96def addpackage(sitedir, name):
97 fullname = os.path.join(sitedir, name)
98 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000099 f = open(fullname)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000100 except IOError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000101 return
Guido van Rossumf30bec71997-08-29 22:30:45 +0000102 while 1:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000103 dir = f.readline()
104 if not dir:
105 break
106 if dir[0] == '#':
107 continue
108 if dir[-1] == '\n':
109 dir = dir[:-1]
Fred Drake38cb9f12000-09-28 16:52:36 +0000110 dir = makepath(sitedir, dir)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000111 if dir not in sys.path and os.path.exists(dir):
112 sys.path.append(dir)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000113
114prefixes = [sys.prefix]
115if sys.exec_prefix != sys.prefix:
116 prefixes.append(sys.exec_prefix)
117for prefix in prefixes:
Guido van Rossume57c96e1996-08-17 19:56:26 +0000118 if prefix:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000119 if os.sep == '/':
Fred Drake38cb9f12000-09-28 16:52:36 +0000120 sitedirs = [makepath(prefix,
121 "lib",
122 "python" + sys.version[:3],
123 "site-packages"),
124 makepath(prefix, "lib", "site-python")]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000125 else:
126 sitedirs = [prefix]
127 for sitedir in sitedirs:
128 if os.path.isdir(sitedir):
129 addsitedir(sitedir)
Guido van Rossume57c96e1996-08-17 19:56:26 +0000130
Guido van Rossumd89fa0c1998-08-07 18:01:14 +0000131# Define new built-ins 'quit' and 'exit'.
132# These are simply strings that display a hint on how to exit.
133if os.sep == ':':
134 exit = 'Use Cmd-Q to quit.'
135elif os.sep == '\\':
136 exit = 'Use Ctrl-Z plus Return to exit.'
137else:
138 exit = 'Use Ctrl-D (i.e. EOF) to exit.'
139import __builtin__
140__builtin__.quit = __builtin__.exit = exit
141del exit
142
Guido van Rossumd1252392000-09-05 04:39:55 +0000143# interactive prompt objects for printing the license text, a list of
144# contributors and the copyright notice.
145class _Printer:
146 MAXLINES = 23
147
148 def __init__(self, s):
149 self.__lines = s.split('\n')
150 self.__linecnt = len(self.__lines)
151
152 def __repr__(self):
153 prompt = 'Hit Return for more, or q (and Return) to quit: '
154 lineno = 0
155 while 1:
156 try:
157 for i in range(lineno, lineno + self.MAXLINES):
158 print self.__lines[i]
159 except IndexError:
160 break
161 else:
162 lineno += self.MAXLINES
163 key = None
164 while key is None:
165 key = raw_input(prompt)
166 if key not in ('', 'q'):
167 key = None
168 if key == 'q':
169 break
170 return ''
171
172__builtin__.copyright = _Printer(sys.copyright)
173__builtin__.credits = _Printer(
174 '''Python development is led by BeOpen PythonLabs (www.pythonlabs.com).''')
175
176def make_license(filename):
177 try:
178 return _Printer(open(filename).read())
179 except IOError:
180 return None
181
182here = os.path.dirname(os.__file__)
183for dir in here, os.path.join(here, os.pardir), os.curdir:
184 for file in "LICENSE.txt", "LICENSE":
185 lic = make_license(os.path.join(dir, file))
186 if lic:
187 break
188 if lic:
189 __builtin__.license = lic
190 break
191else:
192 __builtin__.license = _Printer('See http://hdl.handle.net/1895.22/1012')
193
194
Fredrik Lundh3fded4b2000-07-15 20:58:44 +0000195# Set the string encoding used by the Unicode implementation. The
196# default is 'ascii', but if you're willing to experiment, you can
197# change this.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000198
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000199encoding = "ascii" # Default value set by _PyUnicode_Init()
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000200
201if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000202 # Enable to support locale aware default string encodings.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000203 import locale
204 loc = locale.getdefaultlocale()
205 if loc[1]:
206 encoding = loc[1]
207
208if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000209 # Enable to switch off string to Unicode coercion and implicit
210 # Unicode to string conversion.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000211 encoding = "undefined"
212
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000213if encoding != "ascii":
214 sys.setdefaultencoding(encoding)
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000215
216#
217# Run custom site specific code, if available.
218#
Guido van Rossume57c96e1996-08-17 19:56:26 +0000219try:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000220 import sitecustomize
Guido van Rossume57c96e1996-08-17 19:56:26 +0000221except ImportError:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000222 pass
223
224#
225# Remove sys.setdefaultencoding() so that users cannot change the
Fred Drake38cb9f12000-09-28 16:52:36 +0000226# encoding after initialization. The test for presence is needed when
227# this module is run as a script, becuase this code is executed twice.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000228#
Fred Drake38cb9f12000-09-28 16:52:36 +0000229if hasattr(sys, "setdefaultencoding"):
230 del sys.setdefaultencoding
Guido van Rossumf30bec71997-08-29 22:30:45 +0000231
232def _test():
233 print "sys.path = ["
234 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000235 print " %s," % `dir`
Guido van Rossumf30bec71997-08-29 22:30:45 +0000236 print "]"
237
238if __name__ == '__main__':
239 _test()