blob: 3f4e8304c00317417295ba7d0d9b920ec906273d [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
Martin v. Löwisbb0a4b72001-01-11 13:02:43 +000026\code{#} are skipped. Lines starting with \code{import} are executed.
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
Martin v. Löwisbb0a4b72001-01-11 13:02:43 +0000108 if dir.startswith("import"):
109 exec dir
110 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000111 if dir[-1] == '\n':
112 dir = dir[:-1]
Fred Drake38cb9f12000-09-28 16:52:36 +0000113 dir = makepath(sitedir, dir)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000114 if dir not in sys.path and os.path.exists(dir):
115 sys.path.append(dir)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000116
117prefixes = [sys.prefix]
118if sys.exec_prefix != sys.prefix:
119 prefixes.append(sys.exec_prefix)
120for prefix in prefixes:
Guido van Rossume57c96e1996-08-17 19:56:26 +0000121 if prefix:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000122 if os.sep == '/':
Fred Drake38cb9f12000-09-28 16:52:36 +0000123 sitedirs = [makepath(prefix,
124 "lib",
125 "python" + sys.version[:3],
126 "site-packages"),
127 makepath(prefix, "lib", "site-python")]
Jack Jansend49056c2000-12-12 22:39:04 +0000128 elif os.sep == ':':
129 sitedirs = [makepath(prefix, "lib", "site-packages")]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000130 else:
131 sitedirs = [prefix]
132 for sitedir in sitedirs:
133 if os.path.isdir(sitedir):
134 addsitedir(sitedir)
Guido van Rossume57c96e1996-08-17 19:56:26 +0000135
Guido van Rossumd89fa0c1998-08-07 18:01:14 +0000136# Define new built-ins 'quit' and 'exit'.
137# These are simply strings that display a hint on how to exit.
138if os.sep == ':':
139 exit = 'Use Cmd-Q to quit.'
140elif os.sep == '\\':
141 exit = 'Use Ctrl-Z plus Return to exit.'
142else:
143 exit = 'Use Ctrl-D (i.e. EOF) to exit.'
144import __builtin__
145__builtin__.quit = __builtin__.exit = exit
146del exit
147
Guido van Rossumd1252392000-09-05 04:39:55 +0000148# interactive prompt objects for printing the license text, a list of
149# contributors and the copyright notice.
150class _Printer:
151 MAXLINES = 23
152
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000153 def __init__(self, name, data, files=(), dirs=()):
154 self.__name = name
155 self.__data = data
156 self.__files = files
157 self.__dirs = dirs
158 self.__lines = None
159
160 def __setup(self):
161 if self.__lines:
162 return
163 data = None
164 for dir in self.__dirs:
165 for file in self.__files:
166 file = os.path.join(dir, file)
167 try:
168 fp = open(file)
169 data = fp.read()
170 fp.close()
171 break
172 except IOError:
173 pass
174 if data:
175 break
176 if not data:
177 data = self.__data
178 self.__lines = data.split('\n')
Guido van Rossumd1252392000-09-05 04:39:55 +0000179 self.__linecnt = len(self.__lines)
180
181 def __repr__(self):
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000182 self.__setup()
183 if len(self.__lines) <= self.MAXLINES:
184 return "\n".join(self.__lines)
185 else:
186 return "Type %s() to see the full %s text" % ((self.__name,)*2)
187
188 def __call__(self):
189 self.__setup()
Guido van Rossumd1252392000-09-05 04:39:55 +0000190 prompt = 'Hit Return for more, or q (and Return) to quit: '
191 lineno = 0
192 while 1:
193 try:
194 for i in range(lineno, lineno + self.MAXLINES):
195 print self.__lines[i]
196 except IndexError:
197 break
198 else:
199 lineno += self.MAXLINES
200 key = None
201 while key is None:
202 key = raw_input(prompt)
203 if key not in ('', 'q'):
204 key = None
205 if key == 'q':
206 break
Guido van Rossumd1252392000-09-05 04:39:55 +0000207
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000208__builtin__.copyright = _Printer("copyright", sys.copyright)
Barry Warsaw23f26ce2000-12-06 22:20:07 +0000209if sys.platform[:4] == 'java':
210 __builtin__.credits = _Printer(
211 "credits",
212 "Jython is maintained by the Jython developers (www.jython.org).")
213else:
214 __builtin__.credits = _Printer("credits", """\
215Thanks to CWI, CNRI, BeOpen.com, Digital Creations and a cast of thousands
216for supporting Python development. See www.python.org for more information.""")
Guido van Rossumd1252392000-09-05 04:39:55 +0000217here = os.path.dirname(os.__file__)
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000218__builtin__.license = _Printer(
219 "license", "See http://www.pythonlabs.com/products/python2.0/license.html",
220 ["LICENSE.txt", "LICENSE"],
221 [here, os.path.join(here, os.pardir), os.curdir])
Guido van Rossumd1252392000-09-05 04:39:55 +0000222
223
Fredrik Lundh3fded4b2000-07-15 20:58:44 +0000224# Set the string encoding used by the Unicode implementation. The
225# default is 'ascii', but if you're willing to experiment, you can
226# change this.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000227
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000228encoding = "ascii" # Default value set by _PyUnicode_Init()
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000229
230if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000231 # Enable to support locale aware default string encodings.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000232 import locale
233 loc = locale.getdefaultlocale()
234 if loc[1]:
235 encoding = loc[1]
236
237if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000238 # Enable to switch off string to Unicode coercion and implicit
239 # Unicode to string conversion.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000240 encoding = "undefined"
241
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000242if encoding != "ascii":
243 sys.setdefaultencoding(encoding)
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000244
245#
246# Run custom site specific code, if available.
247#
Guido van Rossume57c96e1996-08-17 19:56:26 +0000248try:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000249 import sitecustomize
Guido van Rossume57c96e1996-08-17 19:56:26 +0000250except ImportError:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000251 pass
252
253#
254# Remove sys.setdefaultencoding() so that users cannot change the
Fred Drake38cb9f12000-09-28 16:52:36 +0000255# encoding after initialization. The test for presence is needed when
Barry Warsaw23f26ce2000-12-06 22:20:07 +0000256# this module is run as a script, because this code is executed twice.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000257#
Fred Drake38cb9f12000-09-28 16:52:36 +0000258if hasattr(sys, "setdefaultencoding"):
259 del sys.setdefaultencoding
Guido van Rossumf30bec71997-08-29 22:30:45 +0000260
261def _test():
262 print "sys.path = ["
263 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000264 print " %s," % `dir`
Guido van Rossumf30bec71997-08-29 22:30:45 +0000265 print "]"
266
267if __name__ == '__main__':
268 _test()