1 """Append module search paths for third-party packages to sys.path.
3 ****************************************************************
4 * This module is automatically imported during initialization. *
5 ****************************************************************
7 In earlier versions of Python (up to 1.5a3), scripts or modules that
8 needed to use site-specific modules would place ``import site''
9 somewhere near the top of their code. Because of the automatic
10 import, this is no longer necessary (but code that does it still
13 This will append site-specific paths to the module search path. On
14 Unix (including Mac OSX), it starts with sys.prefix and
15 sys.exec_prefix (if different) and appends
16 lib/python<version>/site-packages as well as lib/site-python.
17 On other platforms (such as Windows), it tries each of the
18 prefixes directly, as well as with lib/site-packages appended. The
19 resulting directories, if they exist, are appended to sys.path, and
20 also inspected for path configuration files.
22 A path configuration file is a file whose name has the form
23 <package>.pth; its contents are additional directories (one per line)
24 to be added to sys.path. Non-existing directories (or
25 non-directories) are never added to sys.path; no directory is added to
26 sys.path more than once. Blank lines and lines beginning with
27 '#' are skipped. Lines starting with 'import' are executed.
29 For example, suppose sys.prefix and sys.exec_prefix are set to
30 /usr/local and there is a directory /usr/local/lib/python2.5/site-packages
31 with three subdirectories, foo, bar and spam, and two path
32 configuration files, foo.pth and bar.pth. Assume foo.pth contains the
35 # foo package configuration
42 # bar package configuration
45 Then the following directories are added to sys.path, in this order:
47 /usr/local/lib/python2.5/site-packages/bar
48 /usr/local/lib/python2.5/site-packages/foo
50 Note that bletch is omitted because it doesn't exist; bar precedes foo
51 because bar.pth comes alphabetically before foo.pth; and spam is
52 omitted because it is not mentioned in either path configuration file.
54 After these path manipulations, an attempt is made to import a module
55 named sitecustomize, which can perform arbitrary additional
56 site-specific customizations. If this import fails with an
57 ImportError exception, it is silently ignored.
67 dir = os
.path
.abspath(os
.path
.join(*paths
))
68 return dir, os
.path
.normcase(dir)
71 """Set all module' __file__ attribute to an absolute path"""
72 for m
in sys
.modules
.values():
73 if hasattr(m
, '__loader__'):
74 continue # don't mess with a PEP 302-supplied __file__
76 m
.__file
__ = os
.path
.abspath(m
.__file
__)
77 except AttributeError:
81 """ Remove duplicate entries from sys.path along with making them
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.
88 # Filter out duplicate paths (on case-insensitive file systems also
89 # if they only differ in case); turn relative paths into absolute
91 dir, dircase
= makepath(dir)
92 if not dircase
in known_paths
:
94 known_paths
.add(dircase
)
98 # 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
101 """Append ./build/lib.<platform> in case we're running in the build dir
102 (especially for Guido :-)"""
103 from distutils
.util
import get_platform
104 s
= "build/lib.%s-%.3s" % (get_platform(), sys
.version
)
105 s
= os
.path
.join(os
.path
.dirname(sys
.path
[-1]), s
)
108 def _init_pathinfo():
109 """Return a set containing all existing directory entries from sys.path"""
113 if os
.path
.isdir(dir):
114 dir, dircase
= makepath(dir)
120 def addpackage(sitedir
, name
, known_paths
):
121 """Process a .pth file within the site-packages directory:
122 For each line in the file, either combine it with sitedir to a path
123 and add that to known_paths, or execute it if it starts with 'import '.
125 if known_paths
is None:
130 fullname
= os
.path
.join(sitedir
, name
)
132 f
= open(fullname
, "rU")
137 if line
.startswith("#"):
139 if line
.startswith("import ") or line
.startswith("import\t"):
143 dir, dircase
= makepath(sitedir
, line
)
144 if not dircase
in known_paths
and os
.path
.exists(dir):
146 known_paths
.add(dircase
)
153 def addsitedir(sitedir
, known_paths
=None):
154 """Add 'sitedir' argument to sys.path if missing and handle .pth files in
156 if known_paths
is None:
157 known_paths
= _init_pathinfo()
161 sitedir
, sitedircase
= makepath(sitedir
)
162 if not sitedircase
in known_paths
:
163 sys
.path
.append(sitedir
) # Add path component
165 names
= os
.listdir(sitedir
)
170 if name
.endswith(os
.extsep
+ "pth"):
171 addpackage(sitedir
, name
, known_paths
)
176 def addsitepackages(known_paths
):
177 """Add site-packages (and possibly site-python) to sys.path"""
178 prefixes
= [sys
.prefix
]
179 if sys
.exec_prefix
!= sys
.prefix
:
180 prefixes
.append(sys
.exec_prefix
)
181 for prefix
in prefixes
:
183 if sys
.platform
in ('os2emx', 'riscos'):
184 sitedirs
= [os
.path
.join(prefix
, "Lib", "site-packages")]
186 sitedirs
= [os
.path
.join(prefix
,
188 "python" + sys
.version
[:3],
190 os
.path
.join(prefix
, "lib", "site-python")]
192 sitedirs
= [prefix
, os
.path
.join(prefix
, "lib", "site-packages")]
193 if sys
.platform
== 'darwin':
194 # for framework builds *only* we add the standard Apple
195 # locations. Currently only per-user, but /Library and
196 # /Network/Library could be added too
197 if 'Python.framework' in prefix
:
198 home
= os
.environ
.get('HOME')
206 for sitedir
in sitedirs
:
207 if os
.path
.isdir(sitedir
):
208 addsitedir(sitedir
, known_paths
)
212 def setBEGINLIBPATH():
213 """The OS/2 EMX port has optional extension modules that do double duty
214 as DLLs (and must use the .DLL file extension) for other extensions.
215 The library search path needs to be amended so these will be found
216 during module import. Use BEGINLIBPATH so that these are at the start
217 of the library search path.
220 dllpath
= os
.path
.join(sys
.prefix
, "Lib", "lib-dynload")
221 libpath
= os
.environ
['BEGINLIBPATH'].split(';')
223 libpath
.append(dllpath
)
225 libpath
[-1] = dllpath
226 os
.environ
['BEGINLIBPATH'] = ';'.join(libpath
)
230 """Define new built-ins 'quit' and 'exit'.
231 These are simply strings that display a hint on how to exit.
237 eof
= 'Ctrl-Z plus Return'
239 eof
= 'Ctrl-D (i.e. EOF)'
241 class Quitter(object):
242 def __init__(self
, name
):
245 return 'Use %s() or %s to exit' % (self
.name
, eof
)
246 def __call__(self
, code
=None):
247 # Shells like IDLE catch the SystemExit, but listen when their
248 # stdin wrapper is closed.
253 raise SystemExit(code
)
254 __builtin__
.quit
= Quitter('quit')
255 __builtin__
.exit
= Quitter('exit')
258 class _Printer(object):
259 """interactive prompt objects for printing the license text, a list of
260 contributors and the copyright notice."""
264 def __init__(self
, name
, data
, files
=(), dirs
=()):
275 for dir in self
.__dirs
:
276 for filename
in self
.__files
:
277 filename
= os
.path
.join(dir, filename
)
279 fp
= file(filename
, "rU")
289 self
.__lines
= data
.split('\n')
290 self
.__linecnt
= len(self
.__lines
)
294 if len(self
.__lines
) <= self
.MAXLINES
:
295 return "\n".join(self
.__lines
)
297 return "Type %s() to see the full %s text" % ((self
.__name
,)*2)
301 prompt
= 'Hit Return for more, or q (and Return) to quit: '
305 for i
in range(lineno
, lineno
+ self
.MAXLINES
):
306 print self
.__lines
[i
]
310 lineno
+= self
.MAXLINES
313 key
= raw_input(prompt
)
314 if key
not in ('', 'q'):
320 """Set 'copyright' and 'credits' in __builtin__"""
321 __builtin__
.copyright
= _Printer("copyright", sys
.copyright
)
322 if sys
.platform
[:4] == 'java':
323 __builtin__
.credits
= _Printer(
325 "Jython is maintained by the Jython developers (www.jython.org).")
327 __builtin__
.credits
= _Printer("credits", """\
328 Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
329 for supporting Python development. See www.python.org for more information.""")
330 here
= os
.path
.dirname(os
.__file
__)
331 __builtin__
.license
= _Printer(
332 "license", "See http://www.python.org/%.3s/license.html" % sys
.version
,
333 ["LICENSE.txt", "LICENSE"],
334 [os
.path
.join(here
, os
.pardir
), here
, os
.curdir
])
337 class _Helper(object):
338 """Define the built-in 'help'.
339 This is a wrapper around pydoc.help (with a twist).
344 return "Type help() for interactive help, " \
345 "or help(object) for help about object."
346 def __call__(self
, *args
, **kwds
):
348 return pydoc
.help(*args
, **kwds
)
351 __builtin__
.help = _Helper()
354 """On Windows, some default encodings are not provided by Python,
355 while they are always available as "mbcs" in each locale. Make
356 them usable by aliasing to "mbcs" in such a case."""
357 if sys
.platform
== 'win32':
358 import locale
, codecs
359 enc
= locale
.getdefaultlocale()[1]
360 if enc
.startswith('cp'): # "cp***" ?
365 encodings
._cache
[enc
] = encodings
._unknown
366 encodings
.aliases
.aliases
[enc
] = 'mbcs'
369 """Set the string encoding used by the Unicode implementation. The
370 default is 'ascii', but if you're willing to experiment, you can
372 encoding
= "ascii" # Default value set by _PyUnicode_Init()
374 # Enable to support locale aware default string encodings.
376 loc
= locale
.getdefaultlocale()
380 # Enable to switch off string to Unicode coercion and implicit
381 # Unicode to string conversion.
382 encoding
= "undefined"
383 if encoding
!= "ascii":
384 # On Non-Unicode builds this will raise an AttributeError...
385 sys
.setdefaultencoding(encoding
) # Needs Python Unicode build !
388 def execsitecustomize():
389 """Run custom site specific code, if available."""
398 paths_in_sys
= removeduppaths()
399 if (os
.name
== "posix" and sys
.path
and
400 os
.path
.basename(sys
.path
[-1]) == "Modules"):
402 paths_in_sys
= addsitepackages(paths_in_sys
)
403 if sys
.platform
== 'os2emx':
411 # Remove sys.setdefaultencoding() so that users cannot change the
412 # encoding after initialization. The test for presence is needed when
413 # this module is run as a script, because this code is executed twice.
414 if hasattr(sys
, "setdefaultencoding"):
415 del sys
.setdefaultencoding
422 print " %r," % (dir,)
425 if __name__
== '__main__':