blob: e2ad3b92c5e987fcebf793341f1327f5985770cb [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
Walter Dörwaldf0dfc7a2003-10-20 14:01:56 +000013This will append site-specific paths to the module search path. On
Nick Coghlanf2b16f32006-06-12 08:23:02 +000014Unix (including Mac OSX), it starts with sys.prefix and
15sys.exec_prefix (if different) and appends
16lib/python<version>/site-packages as well as lib/site-python.
17On other platforms (such as Windows), it tries each of the
Nick Coghlan3fb55ca2006-06-12 08:19:37 +000018prefixes directly, as well as with lib/site-packages appended. The
Guido van Rossum62b297b1997-09-08 02:14:09 +000019resulting directories, if they exist, are appended to sys.path, and
20also inspected for path configuration files.
Guido van Rossume57c96e1996-08-17 19:56:26 +000021
Guido van Rossumf30bec71997-08-29 22:30:45 +000022A path configuration file is a file whose name has the form
23<package>.pth; its contents are additional directories (one per line)
24to be added to sys.path. Non-existing directories (or
25non-directories) are never added to sys.path; no directory is added to
26sys.path more than once. Blank lines and lines beginning with
Guido van Rossumfacf24b2001-12-17 16:07:06 +000027'#' are skipped. Lines starting with 'import' are executed.
Guido van Rossume57c96e1996-08-17 19:56:26 +000028
Guido van Rossumf30bec71997-08-29 22:30:45 +000029For example, suppose sys.prefix and sys.exec_prefix are set to
Neal Norwitz6e482562006-08-15 04:59:30 +000030/usr/local and there is a directory /usr/local/lib/python2.5/site-packages
Guido van Rossum62b297b1997-09-08 02:14:09 +000031with three subdirectories, foo, bar and spam, and two path
32configuration files, foo.pth and bar.pth. Assume foo.pth contains the
33following:
Guido van Rossumf30bec71997-08-29 22:30:45 +000034
35 # foo package configuration
36 foo
37 bar
38 bletch
39
40and bar.pth contains:
41
42 # bar package configuration
43 bar
44
45Then the following directories are added to sys.path, in this order:
46
Neal Norwitz6e482562006-08-15 04:59:30 +000047 /usr/local/lib/python2.5/site-packages/bar
48 /usr/local/lib/python2.5/site-packages/foo
Guido van Rossumf30bec71997-08-29 22:30:45 +000049
50Note that bletch is omitted because it doesn't exist; bar precedes foo
51because bar.pth comes alphabetically before foo.pth; and spam is
52omitted because it is not mentioned in either path configuration file.
Guido van Rossume57c96e1996-08-17 19:56:26 +000053
54After these path manipulations, an attempt is made to import a module
Guido van Rossumf30bec71997-08-29 22:30:45 +000055named sitecustomize, which can perform arbitrary additional
56site-specific customizations. If this import fails with an
57ImportError exception, it is silently ignored.
Guido van Rossume57c96e1996-08-17 19:56:26 +000058
Guido van Rossume57c96e1996-08-17 19:56:26 +000059"""
60
Brett Cannon0096e262004-06-05 01:12:51 +000061import sys
62import os
63import __builtin__
Guido van Rossume57c96e1996-08-17 19:56:26 +000064
Guido van Rossumd74fb6b2001-03-02 06:43:49 +000065
Fred Drake38cb9f12000-09-28 16:52:36 +000066def makepath(*paths):
Fred Drake1fb5ce02001-07-02 16:55:42 +000067 dir = os.path.abspath(os.path.join(*paths))
68 return dir, os.path.normcase(dir)
Fred Drake38cb9f12000-09-28 16:52:36 +000069
Brett Cannon0096e262004-06-05 01:12:51 +000070def abs__file__():
71 """Set all module' __file__ attribute to an absolute path"""
72 for m in sys.modules.values():
Neal Norwitz0c469852006-04-11 07:21:20 +000073 if hasattr(m, '__loader__'):
Phillip J. Eby47032112006-04-11 01:07:43 +000074 continue # don't mess with a PEP 302-supplied __file__
Brett Cannon0096e262004-06-05 01:12:51 +000075 try:
76 m.__file__ = os.path.abspath(m.__file__)
77 except AttributeError:
78 continue
Fred Drake38cb9f12000-09-28 16:52:36 +000079
Brett Cannon0096e262004-06-05 01:12:51 +000080def removeduppaths():
81 """ Remove duplicate entries from sys.path along with making them
82 absolute"""
83 # This ensures that the initial path provided by the interpreter contains
84 # only absolute pathnames, even if we're running from the build directory.
85 L = []
86 known_paths = set()
87 for dir in sys.path:
88 # Filter out duplicate paths (on case-insensitive file systems also
89 # if they only differ in case); turn relative paths into absolute
90 # paths.
91 dir, dircase = makepath(dir)
92 if not dircase in known_paths:
93 L.append(dir)
94 known_paths.add(dircase)
95 sys.path[:] = L
96 return known_paths
Fred Drake38cb9f12000-09-28 16:52:36 +000097
Fred Drakee80c0d32002-07-25 20:13:03 +000098# XXX This should not be part of site.py, since it is needed even when
99# using the -S option for Python. See http://www.python.org/sf/586680
Brett Cannon0096e262004-06-05 01:12:51 +0000100def addbuilddir():
101 """Append ./build/lib.<platform> in case we're running in the build dir
102 (especially for Guido :-)"""
Jeremy Hylton6d58bf62003-07-18 17:45:33 +0000103 from distutils.util import get_platform
104 s = "build/lib.%s-%.3s" % (get_platform(), sys.version)
Guido van Rossum48eb9cd2001-01-19 21:54:59 +0000105 s = os.path.join(os.path.dirname(sys.path[-1]), s)
106 sys.path.append(s)
Guido van Rossum48eb9cd2001-01-19 21:54:59 +0000107
Fred Drake7f5296e2001-07-20 20:06:17 +0000108def _init_pathinfo():
Brett Cannon0096e262004-06-05 01:12:51 +0000109 """Return a set containing all existing directory entries from sys.path"""
110 d = set()
Fred Drake7f5296e2001-07-20 20:06:17 +0000111 for dir in sys.path:
Brett Cannon0096e262004-06-05 01:12:51 +0000112 try:
113 if os.path.isdir(dir):
114 dir, dircase = makepath(dir)
115 d.add(dircase)
116 except TypeError:
Fred Drake7f5296e2001-07-20 20:06:17 +0000117 continue
Brett Cannon0096e262004-06-05 01:12:51 +0000118 return d
Fred Drake7f5296e2001-07-20 20:06:17 +0000119
Brett Cannon0096e262004-06-05 01:12:51 +0000120def addpackage(sitedir, name, known_paths):
121 """Add a new path to known_paths by combining sitedir and 'name' or execute
122 sitedir if it starts with 'import'"""
123 if known_paths is None:
Fred Drake7f5296e2001-07-20 20:06:17 +0000124 _init_pathinfo()
125 reset = 1
126 else:
127 reset = 0
Brett Cannon0096e262004-06-05 01:12:51 +0000128 fullname = os.path.join(sitedir, name)
129 try:
Brett Cannon4d0bddf2004-07-20 02:28:28 +0000130 f = open(fullname, "rU")
Brett Cannon0096e262004-06-05 01:12:51 +0000131 except IOError:
132 return
133 try:
134 for line in f:
135 if line.startswith("#"):
136 continue
137 if line.startswith("import"):
138 exec line
139 continue
140 line = line.rstrip()
141 dir, dircase = makepath(sitedir, line)
142 if not dircase in known_paths and os.path.exists(dir):
143 sys.path.append(dir)
144 known_paths.add(dircase)
145 finally:
146 f.close()
147 if reset:
148 known_paths = None
149 return known_paths
150
Brett Cannon12f8c4d2004-07-09 23:38:18 +0000151def addsitedir(sitedir, known_paths=None):
Brett Cannon0096e262004-06-05 01:12:51 +0000152 """Add 'sitedir' argument to sys.path if missing and handle .pth files in
153 'sitedir'"""
154 if known_paths is None:
Brett Cannon4d0bddf2004-07-20 02:28:28 +0000155 known_paths = _init_pathinfo()
Brett Cannon0096e262004-06-05 01:12:51 +0000156 reset = 1
157 else:
158 reset = 0
Fred Drake1fb5ce02001-07-02 16:55:42 +0000159 sitedir, sitedircase = makepath(sitedir)
Brett Cannon0096e262004-06-05 01:12:51 +0000160 if not sitedircase in known_paths:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000161 sys.path.append(sitedir) # Add path component
Guido van Rossumf30bec71997-08-29 22:30:45 +0000162 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000163 names = os.listdir(sitedir)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000164 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000165 return
Guido van Rossumf30bec71997-08-29 22:30:45 +0000166 names.sort()
167 for name in names:
Brett Cannon4d0bddf2004-07-20 02:28:28 +0000168 if name.endswith(os.extsep + "pth"):
Brett Cannon0096e262004-06-05 01:12:51 +0000169 addpackage(sitedir, name, known_paths)
Fred Drake7f5296e2001-07-20 20:06:17 +0000170 if reset:
Brett Cannon0096e262004-06-05 01:12:51 +0000171 known_paths = None
172 return known_paths
Guido van Rossumf30bec71997-08-29 22:30:45 +0000173
Brett Cannon0096e262004-06-05 01:12:51 +0000174def addsitepackages(known_paths):
175 """Add site-packages (and possibly site-python) to sys.path"""
176 prefixes = [sys.prefix]
177 if sys.exec_prefix != sys.prefix:
178 prefixes.append(sys.exec_prefix)
179 for prefix in prefixes:
180 if prefix:
181 if sys.platform in ('os2emx', 'riscos'):
182 sitedirs = [os.path.join(prefix, "Lib", "site-packages")]
183 elif os.sep == '/':
184 sitedirs = [os.path.join(prefix,
185 "lib",
186 "python" + sys.version[:3],
187 "site-packages"),
188 os.path.join(prefix, "lib", "site-python")]
189 else:
190 sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")]
191 if sys.platform == 'darwin':
192 # for framework builds *only* we add the standard Apple
193 # locations. Currently only per-user, but /Library and
194 # /Network/Library could be added too
195 if 'Python.framework' in prefix:
196 home = os.environ.get('HOME')
197 if home:
198 sitedirs.append(
199 os.path.join(home,
200 'Library',
201 'Python',
202 sys.version[:3],
203 'site-packages'))
204 for sitedir in sitedirs:
205 if os.path.isdir(sitedir):
206 addsitedir(sitedir, known_paths)
207 return None
Fred Drake7f5296e2001-07-20 20:06:17 +0000208
Fred Drake1fb5ce02001-07-02 16:55:42 +0000209
Brett Cannon0096e262004-06-05 01:12:51 +0000210def setBEGINLIBPATH():
211 """The OS/2 EMX port has optional extension modules that do double duty
212 as DLLs (and must use the .DLL file extension) for other extensions.
213 The library search path needs to be amended so these will be found
214 during module import. Use BEGINLIBPATH so that these are at the start
215 of the library search path.
Tim Peters4e0e1b62004-07-07 20:54:48 +0000216
Brett Cannon0096e262004-06-05 01:12:51 +0000217 """
Andrew MacIntyre2e8a6e02003-12-02 12:27:25 +0000218 dllpath = os.path.join(sys.prefix, "Lib", "lib-dynload")
219 libpath = os.environ['BEGINLIBPATH'].split(';')
220 if libpath[-1]:
221 libpath.append(dllpath)
222 else:
223 libpath[-1] = dllpath
224 os.environ['BEGINLIBPATH'] = ';'.join(libpath)
225
226
Brett Cannon0096e262004-06-05 01:12:51 +0000227def setquit():
228 """Define new built-ins 'quit' and 'exit'.
229 These are simply strings that display a hint on how to exit.
Guido van Rossumd89fa0c1998-08-07 18:01:14 +0000230
Brett Cannon0096e262004-06-05 01:12:51 +0000231 """
232 if os.sep == ':':
Georg Brandl24cb0532006-03-09 23:22:06 +0000233 eof = 'Cmd-Q'
Brett Cannon0096e262004-06-05 01:12:51 +0000234 elif os.sep == '\\':
Georg Brandl24cb0532006-03-09 23:22:06 +0000235 eof = 'Ctrl-Z plus Return'
Brett Cannon0096e262004-06-05 01:12:51 +0000236 else:
Georg Brandl24cb0532006-03-09 23:22:06 +0000237 eof = 'Ctrl-D (i.e. EOF)'
Tim Peters88ca4672006-03-10 23:39:56 +0000238
Georg Brandl24cb0532006-03-09 23:22:06 +0000239 class Quitter(object):
240 def __init__(self, name):
241 self.name = name
242 def __repr__(self):
243 return 'Use %s() or %s to exit' % (self.name, eof)
244 def __call__(self, code=None):
245 raise SystemExit(code)
246 __builtin__.quit = Quitter('quit')
247 __builtin__.exit = Quitter('exit')
Brett Cannon0096e262004-06-05 01:12:51 +0000248
249
250class _Printer(object):
251 """interactive prompt objects for printing the license text, a list of
252 contributors and the copyright notice."""
253
Guido van Rossumd1252392000-09-05 04:39:55 +0000254 MAXLINES = 23
255
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000256 def __init__(self, name, data, files=(), dirs=()):
257 self.__name = name
258 self.__data = data
259 self.__files = files
260 self.__dirs = dirs
261 self.__lines = None
262
263 def __setup(self):
264 if self.__lines:
265 return
266 data = None
267 for dir in self.__dirs:
Brett Cannon0096e262004-06-05 01:12:51 +0000268 for filename in self.__files:
269 filename = os.path.join(dir, filename)
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000270 try:
Brett Cannon0096e262004-06-05 01:12:51 +0000271 fp = file(filename, "rU")
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000272 data = fp.read()
273 fp.close()
274 break
275 except IOError:
276 pass
277 if data:
278 break
279 if not data:
280 data = self.__data
281 self.__lines = data.split('\n')
Guido van Rossumd1252392000-09-05 04:39:55 +0000282 self.__linecnt = len(self.__lines)
283
284 def __repr__(self):
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000285 self.__setup()
286 if len(self.__lines) <= self.MAXLINES:
287 return "\n".join(self.__lines)
288 else:
289 return "Type %s() to see the full %s text" % ((self.__name,)*2)
290
291 def __call__(self):
292 self.__setup()
Guido van Rossumd1252392000-09-05 04:39:55 +0000293 prompt = 'Hit Return for more, or q (and Return) to quit: '
294 lineno = 0
295 while 1:
296 try:
297 for i in range(lineno, lineno + self.MAXLINES):
298 print self.__lines[i]
299 except IndexError:
300 break
301 else:
302 lineno += self.MAXLINES
303 key = None
304 while key is None:
305 key = raw_input(prompt)
306 if key not in ('', 'q'):
307 key = None
308 if key == 'q':
309 break
Guido van Rossumd1252392000-09-05 04:39:55 +0000310
Brett Cannon0096e262004-06-05 01:12:51 +0000311def setcopyright():
312 """Set 'copyright' and 'credits' in __builtin__"""
313 __builtin__.copyright = _Printer("copyright", sys.copyright)
314 if sys.platform[:4] == 'java':
315 __builtin__.credits = _Printer(
316 "credits",
317 "Jython is maintained by the Jython developers (www.jython.org).")
318 else:
319 __builtin__.credits = _Printer("credits", """\
320 Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
321 for supporting Python development. See www.python.org for more information.""")
322 here = os.path.dirname(os.__file__)
323 __builtin__.license = _Printer(
324 "license", "See http://www.python.org/%.3s/license.html" % sys.version,
325 ["LICENSE.txt", "LICENSE"],
326 [os.path.join(here, os.pardir), here, os.curdir])
Guido van Rossumd1252392000-09-05 04:39:55 +0000327
328
Brett Cannon0096e262004-06-05 01:12:51 +0000329class _Helper(object):
330 """Define the built-in 'help'.
331 This is a wrapper around pydoc.help (with a twist).
Guido van Rossum83213cc2001-06-12 16:48:52 +0000332
Brett Cannon0096e262004-06-05 01:12:51 +0000333 """
334
Guido van Rossum83213cc2001-06-12 16:48:52 +0000335 def __repr__(self):
336 return "Type help() for interactive help, " \
337 "or help(object) for help about object."
338 def __call__(self, *args, **kwds):
339 import pydoc
340 return pydoc.help(*args, **kwds)
341
Brett Cannon0096e262004-06-05 01:12:51 +0000342def sethelper():
343 __builtin__.help = _Helper()
344
345def aliasmbcs():
346 """On Windows, some default encodings are not provided by Python,
347 while they are always available as "mbcs" in each locale. Make
348 them usable by aliasing to "mbcs" in such a case."""
349 if sys.platform == 'win32':
350 import locale, codecs
351 enc = locale.getdefaultlocale()[1]
352 if enc.startswith('cp'): # "cp***" ?
353 try:
354 codecs.lookup(enc)
355 except LookupError:
356 import encodings
357 encodings._cache[enc] = encodings._unknown
358 encodings.aliases.aliases[enc] = 'mbcs'
359
360def setencoding():
361 """Set the string encoding used by the Unicode implementation. The
362 default is 'ascii', but if you're willing to experiment, you can
363 change this."""
364 encoding = "ascii" # Default value set by _PyUnicode_Init()
365 if 0:
366 # Enable to support locale aware default string encodings.
367 import locale
368 loc = locale.getdefaultlocale()
369 if loc[1]:
370 encoding = loc[1]
371 if 0:
372 # Enable to switch off string to Unicode coercion and implicit
373 # Unicode to string conversion.
374 encoding = "undefined"
375 if encoding != "ascii":
376 # On Non-Unicode builds this will raise an AttributeError...
377 sys.setdefaultencoding(encoding) # Needs Python Unicode build !
Guido van Rossum83213cc2001-06-12 16:48:52 +0000378
379
Brett Cannon0096e262004-06-05 01:12:51 +0000380def execsitecustomize():
381 """Run custom site specific code, if available."""
382 try:
383 import sitecustomize
384 except ImportError:
385 pass
Martin v. Löwis4eab4862003-03-03 09:34:01 +0000386
Martin v. Löwis4eab4862003-03-03 09:34:01 +0000387
Brett Cannon0096e262004-06-05 01:12:51 +0000388def main():
389 abs__file__()
390 paths_in_sys = removeduppaths()
391 if (os.name == "posix" and sys.path and
392 os.path.basename(sys.path[-1]) == "Modules"):
393 addbuilddir()
394 paths_in_sys = addsitepackages(paths_in_sys)
395 if sys.platform == 'os2emx':
396 setBEGINLIBPATH()
397 setquit()
398 setcopyright()
399 sethelper()
400 aliasmbcs()
401 setencoding()
402 execsitecustomize()
403 # Remove sys.setdefaultencoding() so that users cannot change the
404 # encoding after initialization. The test for presence is needed when
405 # this module is run as a script, because this code is executed twice.
406 if hasattr(sys, "setdefaultencoding"):
407 del sys.setdefaultencoding
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000408
Brett Cannon0096e262004-06-05 01:12:51 +0000409main()
Guido van Rossumf30bec71997-08-29 22:30:45 +0000410
411def _test():
412 print "sys.path = ["
413 for dir in sys.path:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000414 print " %r," % (dir,)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000415 print "]"
416
417if __name__ == '__main__':
418 _test()