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, it starts with sys.prefix and sys.exec_prefix (if different) and
15 appends lib/python<version>/site-packages as well as lib/site-python.
16 On other platforms (mainly Mac and Windows), it uses just sys.prefix
17 (and sys.exec_prefix, if different, but this is unlikely). The
18 resulting directories, if they exist, are appended to sys.path, and
19 also inspected for path configuration files.
21 A path configuration file is a file whose name has the form
22 <package>.pth; its contents are additional directories (one per line)
23 to be added to sys.path. Non-existing directories (or
24 non-directories) are never added to sys.path; no directory is added to
25 sys.path more than once. Blank lines and lines beginning with
26 '#' are skipped. Lines starting with 'import' are executed.
28 For example, suppose sys.prefix and sys.exec_prefix are set to
29 /usr/local and there is a directory /usr/local/lib/python1.5/site-packages
30 with three subdirectories, foo, bar and spam, and two path
31 configuration files, foo.pth and bar.pth. Assume foo.pth contains the
34 # foo package configuration
41 # bar package configuration
44 Then the following directories are added to sys.path, in this order:
46 /usr/local/lib/python1.5/site-packages/bar
47 /usr/local/lib/python1.5/site-packages/foo
49 Note that bletch is omitted because it doesn't exist; bar precedes foo
50 because bar.pth comes alphabetically before foo.pth; and spam is
51 omitted because it is not mentioned in either path configuration file.
53 After these path manipulations, an attempt is made to import a module
54 named sitecustomize, which can perform arbitrary additional
55 site-specific customizations. If this import fails with an
56 ImportError exception, it is silently ignored.
66 dir = os
.path
.abspath(os
.path
.join(*paths
))
67 return dir, os
.path
.normcase(dir)
70 """Set all module' __file__ attribute to an absolute path"""
71 for m
in sys
.modules
.values():
73 m
.__file
__ = os
.path
.abspath(m
.__file
__)
74 except AttributeError:
78 """ Remove duplicate entries from sys.path along with making them
80 # This ensures that the initial path provided by the interpreter contains
81 # only absolute pathnames, even if we're running from the build directory.
85 # Filter out duplicate paths (on case-insensitive file systems also
86 # if they only differ in case); turn relative paths into absolute
88 dir, dircase
= makepath(dir)
89 if not dircase
in known_paths
:
91 known_paths
.add(dircase
)
95 # XXX This should not be part of site.py, since it is needed even when
96 # using the -S option for Python. See http://www.python.org/sf/586680
98 """Append ./build/lib.<platform> in case we're running in the build dir
99 (especially for Guido :-)"""
100 from distutils
.util
import get_platform
101 s
= "build/lib.%s-%.3s" % (get_platform(), sys
.version
)
102 s
= os
.path
.join(os
.path
.dirname(sys
.path
[-1]), s
)
105 def _init_pathinfo():
106 """Return a set containing all existing directory entries from sys.path"""
110 if os
.path
.isdir(dir):
111 dir, dircase
= makepath(dir)
117 def addpackage(sitedir
, name
, known_paths
):
118 """Add a new path to known_paths by combining sitedir and 'name' or execute
119 sitedir if it starts with 'import'"""
120 if known_paths
is None:
125 fullname
= os
.path
.join(sitedir
, name
)
127 f
= open(fullname
, "rU")
132 if line
.startswith("#"):
134 if line
.startswith("import"):
138 dir, dircase
= makepath(sitedir
, line
)
139 if not dircase
in known_paths
and os
.path
.exists(dir):
141 known_paths
.add(dircase
)
148 def addsitedir(sitedir
, known_paths
=None):
149 """Add 'sitedir' argument to sys.path if missing and handle .pth files in
151 if known_paths
is None:
152 known_paths
= _init_pathinfo()
156 sitedir
, sitedircase
= makepath(sitedir
)
157 if not sitedircase
in known_paths
:
158 sys
.path
.append(sitedir
) # Add path component
160 names
= os
.listdir(sitedir
)
165 if name
.endswith(os
.extsep
+ "pth"):
166 addpackage(sitedir
, name
, known_paths
)
171 def addsitepackages(known_paths
):
172 """Add site-packages (and possibly site-python) to sys.path"""
173 prefixes
= [sys
.prefix
]
174 if sys
.exec_prefix
!= sys
.prefix
:
175 prefixes
.append(sys
.exec_prefix
)
176 for prefix
in prefixes
:
178 if sys
.platform
in ('os2emx', 'riscos'):
179 sitedirs
= [os
.path
.join(prefix
, "Lib", "site-packages")]
181 sitedirs
= [os
.path
.join(prefix
,
183 "python" + sys
.version
[:3],
185 os
.path
.join(prefix
, "lib", "site-python")]
187 sitedirs
= [prefix
, os
.path
.join(prefix
, "lib", "site-packages")]
188 if sys
.platform
== 'darwin':
189 # for framework builds *only* we add the standard Apple
190 # locations. Currently only per-user, but /Library and
191 # /Network/Library could be added too
192 if 'Python.framework' in prefix
:
193 home
= os
.environ
.get('HOME')
201 for sitedir
in sitedirs
:
202 if os
.path
.isdir(sitedir
):
203 addsitedir(sitedir
, known_paths
)
207 def setBEGINLIBPATH():
208 """The OS/2 EMX port has optional extension modules that do double duty
209 as DLLs (and must use the .DLL file extension) for other extensions.
210 The library search path needs to be amended so these will be found
211 during module import. Use BEGINLIBPATH so that these are at the start
212 of the library search path.
215 dllpath
= os
.path
.join(sys
.prefix
, "Lib", "lib-dynload")
216 libpath
= os
.environ
['BEGINLIBPATH'].split(';')
218 libpath
.append(dllpath
)
220 libpath
[-1] = dllpath
221 os
.environ
['BEGINLIBPATH'] = ';'.join(libpath
)
225 """Define new built-ins 'quit' and 'exit'.
226 These are simply strings that display a hint on how to exit.
232 eof
= 'Ctrl-Z plus Return'
234 eof
= 'Ctrl-D (i.e. EOF)'
236 class Quitter(object):
237 def __init__(self
, name
):
240 return 'Use %s() or %s to exit' % (self
.name
, eof
)
241 def __call__(self
, code
=None):
242 raise SystemExit(code
)
243 __builtin__
.quit
= Quitter('quit')
244 __builtin__
.exit
= Quitter('exit')
247 class _Printer(object):
248 """interactive prompt objects for printing the license text, a list of
249 contributors and the copyright notice."""
253 def __init__(self
, name
, data
, files
=(), dirs
=()):
264 for dir in self
.__dirs
:
265 for filename
in self
.__files
:
266 filename
= os
.path
.join(dir, filename
)
268 fp
= file(filename
, "rU")
278 self
.__lines
= data
.split('\n')
279 self
.__linecnt
= len(self
.__lines
)
283 if len(self
.__lines
) <= self
.MAXLINES
:
284 return "\n".join(self
.__lines
)
286 return "Type %s() to see the full %s text" % ((self
.__name
,)*2)
290 prompt
= 'Hit Return for more, or q (and Return) to quit: '
294 for i
in range(lineno
, lineno
+ self
.MAXLINES
):
295 print self
.__lines
[i
]
299 lineno
+= self
.MAXLINES
302 key
= raw_input(prompt
)
303 if key
not in ('', 'q'):
309 """Set 'copyright' and 'credits' in __builtin__"""
310 __builtin__
.copyright
= _Printer("copyright", sys
.copyright
)
311 if sys
.platform
[:4] == 'java':
312 __builtin__
.credits
= _Printer(
314 "Jython is maintained by the Jython developers (www.jython.org).")
316 __builtin__
.credits
= _Printer("credits", """\
317 Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
318 for supporting Python development. See www.python.org for more information.""")
319 here
= os
.path
.dirname(os
.__file
__)
320 __builtin__
.license
= _Printer(
321 "license", "See http://www.python.org/%.3s/license.html" % sys
.version
,
322 ["LICENSE.txt", "LICENSE"],
323 [os
.path
.join(here
, os
.pardir
), here
, os
.curdir
])
326 class _Helper(object):
327 """Define the built-in 'help'.
328 This is a wrapper around pydoc.help (with a twist).
333 return "Type help() for interactive help, " \
334 "or help(object) for help about object."
335 def __call__(self
, *args
, **kwds
):
337 return pydoc
.help(*args
, **kwds
)
340 __builtin__
.help = _Helper()
343 """On Windows, some default encodings are not provided by Python,
344 while they are always available as "mbcs" in each locale. Make
345 them usable by aliasing to "mbcs" in such a case."""
346 if sys
.platform
== 'win32':
347 import locale
, codecs
348 enc
= locale
.getdefaultlocale()[1]
349 if enc
.startswith('cp'): # "cp***" ?
354 encodings
._cache
[enc
] = encodings
._unknown
355 encodings
.aliases
.aliases
[enc
] = 'mbcs'
358 """Set the string encoding used by the Unicode implementation. The
359 default is 'ascii', but if you're willing to experiment, you can
361 encoding
= "ascii" # Default value set by _PyUnicode_Init()
363 # Enable to support locale aware default string encodings.
365 loc
= locale
.getdefaultlocale()
369 # Enable to switch off string to Unicode coercion and implicit
370 # Unicode to string conversion.
371 encoding
= "undefined"
372 if encoding
!= "ascii":
373 # On Non-Unicode builds this will raise an AttributeError...
374 sys
.setdefaultencoding(encoding
) # Needs Python Unicode build !
377 def execsitecustomize():
378 """Run custom site specific code, if available."""
387 paths_in_sys
= removeduppaths()
388 if (os
.name
== "posix" and sys
.path
and
389 os
.path
.basename(sys
.path
[-1]) == "Modules"):
391 paths_in_sys
= addsitepackages(paths_in_sys
)
392 if sys
.platform
== 'os2emx':
400 # Remove sys.setdefaultencoding() so that users cannot change the
401 # encoding after initialization. The test for presence is needed when
402 # this module is run as a script, because this code is executed twice.
403 if hasattr(sys
, "setdefaultencoding"):
404 del sys
.setdefaultencoding
411 print " %r," % (dir,)
414 if __name__
== '__main__':