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 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.
64 dir = os.path.abspath(os.path.join(*paths))
65 return dir, os.path.normcase(dir)
67 for m
in sys.modules.values():
68 if hasattr(m,
"__file__")
and m.__file__:
69 m.__file__ = os.path.abspath(m.__file__)
75 _dirs_in_sys_path = {}
80 if sys.platform !=
'mac':
81 if dir
and not os.path.isdir(dir):
84 if dir
and not os.path.exists(dir):
87 if not _dirs_in_sys_path.has_key(dircase):
89 _dirs_in_sys_path[dircase] = 1
95 if os.name ==
"posix" and os.path.basename(sys.path[-1]) ==
"Modules":
96 from distutils.util
import get_platform
97 s =
"build/lib.%s-%.3s" % (get_platform(), sys.version)
98 s = os.path.join(os.path.dirname(sys.path[-1]), s)
102 def _init_pathinfo():
103 global _dirs_in_sys_path
104 _dirs_in_sys_path = d = {}
106 if dir
and not os.path.isdir(dir):
112 global _dirs_in_sys_path
113 if _dirs_in_sys_path
is None:
118 sitedir, sitedircase =
makepath(sitedir)
119 if not _dirs_in_sys_path.has_key(sitedircase):
120 sys.path.append(sitedir)
122 names = os.listdir(sitedir)
127 if name[-4:] == os.extsep +
"pth":
130 _dirs_in_sys_path =
None
133 global _dirs_in_sys_path
134 if _dirs_in_sys_path
is None:
139 fullname = os.path.join(sitedir, name)
150 if dir.startswith(
"import"):
155 dir, dircase =
makepath(sitedir, dir)
156 if not _dirs_in_sys_path.has_key(dircase)
and os.path.exists(dir):
158 _dirs_in_sys_path[dircase] = 1
160 _dirs_in_sys_path =
None
162 prefixes = [sys.prefix]
163 if sys.exec_prefix != sys.prefix:
164 prefixes.append(sys.exec_prefix)
165 for prefix
in prefixes:
168 sitedirs = [os.path.join(prefix,
170 "python" + sys.version[:3],
172 os.path.join(prefix,
"lib",
"site-python")]
174 sitedirs = [prefix, os.path.join(prefix,
"lib",
"site-packages")]
175 for sitedir
in sitedirs:
176 if os.path.isdir(sitedir):
179 _dirs_in_sys_path =
None
185 exit =
'Use Cmd-Q to quit.'
187 exit =
'Use Ctrl-Z plus Return to exit.'
189 exit =
'Use Ctrl-D (i.e. EOF) to exit.'
191 __builtin__.quit = __builtin__.exit = exit
212 file = os.path.join(dir, file)
224 self.
__lines = data.split(
'\n')
232 return "Type %s() to see the full %s text" % ((self.
__name,)*2)
236 prompt =
'Hit Return for more, or q (and Return) to quit: '
240 for i
in range(lineno, lineno + self.
MAXLINES):
248 key = raw_input(prompt)
249 if key
not in (
'',
'q'):
254 __builtin__.copyright =
_Printer(
"copyright", sys.copyright)
255 if sys.platform[:4] ==
'java':
258 "Jython is maintained by the Jython developers (www.jython.org).")
260 __builtin__.credits =
_Printer(
"credits",
"""\
261 Thanks to CWI, CNRI, BeOpen.com, Digital Creations and a cast of thousands
262 for supporting Python development. See www.python.org for more information.""")
263 here = os.path.dirname(os.__file__)
265 "license",
"See http://www.python.org/%.3s/license.html" % sys.version,
266 [
"LICENSE.txt",
"LICENSE"],
267 [os.path.join(here, os.pardir), here, os.curdir])
275 return "Type help() for interactive help, " \
276 "or help(object) for help about object."
300 encoding =
"undefined"
302 if encoding !=
"ascii":
304 sys.setdefaultencoding(encoding)
319 if hasattr(sys,
"setdefaultencoding"):
320 del sys.setdefaultencoding
328 if __name__ ==
'__main__':