blob: 9dfe6ce2c79a4101597f4c3430907480d9753740 [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
Barry Warsaw62d24882001-03-23 17:53:49 +000017\(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
Guido van Rossumd74fb6b2001-03-02 06:43:49 +000062if os.sep==".":
63 endsep = "/"
64else:
65 endsep = "."
66
67
Fred Drake38cb9f12000-09-28 16:52:36 +000068def makepath(*paths):
Fred Drake1fb5ce02001-07-02 16:55:42 +000069 dir = os.path.abspath(os.path.join(*paths))
70 return dir, os.path.normcase(dir)
Fred Drake38cb9f12000-09-28 16:52:36 +000071
Fred Drake1fb5ce02001-07-02 16:55:42 +000072for m in sys.modules.values():
Barry Warsaw62d24882001-03-23 17:53:49 +000073 if hasattr(m, "__file__") and m.__file__:
Fred Drake1fb5ce02001-07-02 16:55:42 +000074 m.__file__ = os.path.abspath(m.__file__)
75del m
Fred Drake38cb9f12000-09-28 16:52:36 +000076
77# This ensures that the initial path provided by the interpreter contains
78# only absolute pathnames, even if we're running from the build directory.
79L = []
Fred Drake1fb5ce02001-07-02 16:55:42 +000080dirs_in_sys_path = {}
Fred Drake38cb9f12000-09-28 16:52:36 +000081for dir in sys.path:
Fred Drakefd4ff522001-07-12 21:08:33 +000082 # Filter out paths that don't exist, but leave in the empty string
83 # since it's a special case.
84 if dir and not os.path.isdir(dir):
85 continue
Fred Drake1fb5ce02001-07-02 16:55:42 +000086 dir, dircase = makepath(dir)
87 if not dirs_in_sys_path.has_key(dircase):
Fred Drake38cb9f12000-09-28 16:52:36 +000088 L.append(dir)
Fred Drake1fb5ce02001-07-02 16:55:42 +000089 dirs_in_sys_path[dircase] = 1
Fred Drake38cb9f12000-09-28 16:52:36 +000090sys.path[:] = L
91del dir, L
92
Guido van Rossum48eb9cd2001-01-19 21:54:59 +000093# Append ./build/lib.<platform> in case we're running in the build dir
94# (especially for Guido :-)
95if 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)
99 sys.path.append(s)
100 del get_platform, s
101
Guido van Rossumf30bec71997-08-29 22:30:45 +0000102def addsitedir(sitedir):
Fred Drake1fb5ce02001-07-02 16:55:42 +0000103 sitedir, sitedircase = makepath(sitedir)
104 if not dirs_in_sys_path.has_key(sitedircase):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000105 sys.path.append(sitedir) # Add path component
Guido van Rossumf30bec71997-08-29 22:30:45 +0000106 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000107 names = os.listdir(sitedir)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000108 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000109 return
Guido van Rossumf30bec71997-08-29 22:30:45 +0000110 names.sort()
111 for name in names:
Guido van Rossumd74fb6b2001-03-02 06:43:49 +0000112 if name[-4:] == endsep + "pth":
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000113 addpackage(sitedir, name)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000114
115def addpackage(sitedir, name):
116 fullname = os.path.join(sitedir, name)
117 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000118 f = open(fullname)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000119 except IOError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 return
Guido van Rossumf30bec71997-08-29 22:30:45 +0000121 while 1:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000122 dir = f.readline()
123 if not dir:
124 break
125 if dir[0] == '#':
126 continue
Martin v. Löwisbb0a4b72001-01-11 13:02:43 +0000127 if dir.startswith("import"):
128 exec dir
129 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000130 if dir[-1] == '\n':
131 dir = dir[:-1]
Fred Drake1fb5ce02001-07-02 16:55:42 +0000132 dir, dircase = makepath(sitedir, dir)
133 if not dirs_in_sys_path.has_key(dircase) and os.path.exists(dir):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000134 sys.path.append(dir)
Fred Drake1fb5ce02001-07-02 16:55:42 +0000135 dirs_in_sys_path[dircase] = 1
Guido van Rossumf30bec71997-08-29 22:30:45 +0000136
137prefixes = [sys.prefix]
138if sys.exec_prefix != sys.prefix:
139 prefixes.append(sys.exec_prefix)
140for prefix in prefixes:
Guido van Rossume57c96e1996-08-17 19:56:26 +0000141 if prefix:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000142 if os.sep == '/':
Fred Drake1fb5ce02001-07-02 16:55:42 +0000143 sitedirs = [os.path.join(prefix,
144 "lib",
145 "python" + sys.version[:3],
146 "site-packages"),
147 os.path.join(prefix, "lib", "site-python")]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000148 else:
Tim Peters6a479f52001-07-12 05:20:13 +0000149 sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000150 for sitedir in sitedirs:
151 if os.path.isdir(sitedir):
152 addsitedir(sitedir)
Guido van Rossume57c96e1996-08-17 19:56:26 +0000153
Fred Drake1fb5ce02001-07-02 16:55:42 +0000154
Guido van Rossumd89fa0c1998-08-07 18:01:14 +0000155# Define new built-ins 'quit' and 'exit'.
156# These are simply strings that display a hint on how to exit.
157if os.sep == ':':
158 exit = 'Use Cmd-Q to quit.'
159elif os.sep == '\\':
160 exit = 'Use Ctrl-Z plus Return to exit.'
161else:
162 exit = 'Use Ctrl-D (i.e. EOF) to exit.'
163import __builtin__
164__builtin__.quit = __builtin__.exit = exit
165del exit
166
Guido van Rossumd1252392000-09-05 04:39:55 +0000167# interactive prompt objects for printing the license text, a list of
168# contributors and the copyright notice.
169class _Printer:
170 MAXLINES = 23
171
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000172 def __init__(self, name, data, files=(), dirs=()):
173 self.__name = name
174 self.__data = data
175 self.__files = files
176 self.__dirs = dirs
177 self.__lines = None
178
179 def __setup(self):
180 if self.__lines:
181 return
182 data = None
183 for dir in self.__dirs:
184 for file in self.__files:
185 file = os.path.join(dir, file)
186 try:
187 fp = open(file)
188 data = fp.read()
189 fp.close()
190 break
191 except IOError:
192 pass
193 if data:
194 break
195 if not data:
196 data = self.__data
197 self.__lines = data.split('\n')
Guido van Rossumd1252392000-09-05 04:39:55 +0000198 self.__linecnt = len(self.__lines)
199
200 def __repr__(self):
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000201 self.__setup()
202 if len(self.__lines) <= self.MAXLINES:
203 return "\n".join(self.__lines)
204 else:
205 return "Type %s() to see the full %s text" % ((self.__name,)*2)
206
207 def __call__(self):
208 self.__setup()
Guido van Rossumd1252392000-09-05 04:39:55 +0000209 prompt = 'Hit Return for more, or q (and Return) to quit: '
210 lineno = 0
211 while 1:
212 try:
213 for i in range(lineno, lineno + self.MAXLINES):
214 print self.__lines[i]
215 except IndexError:
216 break
217 else:
218 lineno += self.MAXLINES
219 key = None
220 while key is None:
221 key = raw_input(prompt)
222 if key not in ('', 'q'):
223 key = None
224 if key == 'q':
225 break
Guido van Rossumd1252392000-09-05 04:39:55 +0000226
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000227__builtin__.copyright = _Printer("copyright", sys.copyright)
Barry Warsaw23f26ce2000-12-06 22:20:07 +0000228if sys.platform[:4] == 'java':
229 __builtin__.credits = _Printer(
230 "credits",
231 "Jython is maintained by the Jython developers (www.jython.org).")
232else:
233 __builtin__.credits = _Printer("credits", """\
234Thanks to CWI, CNRI, BeOpen.com, Digital Creations and a cast of thousands
235for supporting Python development. See www.python.org for more information.""")
Guido van Rossumd1252392000-09-05 04:39:55 +0000236here = os.path.dirname(os.__file__)
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000237__builtin__.license = _Printer(
238 "license", "See http://www.pythonlabs.com/products/python2.0/license.html",
239 ["LICENSE.txt", "LICENSE"],
Barry Warsaw62d24882001-03-23 17:53:49 +0000240 [os.path.join(here, os.pardir), here, os.curdir])
Guido van Rossumd1252392000-09-05 04:39:55 +0000241
242
Guido van Rossum83213cc2001-06-12 16:48:52 +0000243# Define new built-in 'help'.
244# This is a wrapper around pydoc.help (with a twist).
245
246class _Helper:
247 def __repr__(self):
248 return "Type help() for interactive help, " \
249 "or help(object) for help about object."
250 def __call__(self, *args, **kwds):
251 import pydoc
252 return pydoc.help(*args, **kwds)
253
254__builtin__.help = _Helper()
255
256
Fredrik Lundh3fded4b2000-07-15 20:58:44 +0000257# Set the string encoding used by the Unicode implementation. The
258# default is 'ascii', but if you're willing to experiment, you can
259# change this.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000260
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000261encoding = "ascii" # Default value set by _PyUnicode_Init()
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000262
263if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000264 # Enable to support locale aware default string encodings.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000265 import locale
266 loc = locale.getdefaultlocale()
267 if loc[1]:
268 encoding = loc[1]
269
270if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000271 # Enable to switch off string to Unicode coercion and implicit
272 # Unicode to string conversion.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000273 encoding = "undefined"
274
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000275if encoding != "ascii":
276 sys.setdefaultencoding(encoding)
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000277
278#
279# Run custom site specific code, if available.
280#
Guido van Rossume57c96e1996-08-17 19:56:26 +0000281try:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000282 import sitecustomize
Guido van Rossume57c96e1996-08-17 19:56:26 +0000283except ImportError:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000284 pass
285
286#
287# Remove sys.setdefaultencoding() so that users cannot change the
Fred Drake38cb9f12000-09-28 16:52:36 +0000288# encoding after initialization. The test for presence is needed when
Barry Warsaw23f26ce2000-12-06 22:20:07 +0000289# this module is run as a script, because this code is executed twice.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000290#
Fred Drake38cb9f12000-09-28 16:52:36 +0000291if hasattr(sys, "setdefaultencoding"):
292 del sys.setdefaultencoding
Guido van Rossumf30bec71997-08-29 22:30:45 +0000293
294def _test():
295 print "sys.path = ["
296 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000297 print " %s," % `dir`
Guido van Rossumf30bec71997-08-29 22:30:45 +0000298 print "]"
299
300if __name__ == '__main__':
301 _test()