blob: fee13f8459fa3550f7a42ac51eecdc12ec2d5377 [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 Warsaw6e1c5762001-12-17 15:40:24 +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
Guido van Rossumfacf24b2001-12-17 16:07:06 +000026'#' are skipped. Lines starting with '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 +000062
Fred Drake38cb9f12000-09-28 16:52:36 +000063def makepath(*paths):
Fred Drake1fb5ce02001-07-02 16:55:42 +000064 dir = os.path.abspath(os.path.join(*paths))
65 return dir, os.path.normcase(dir)
Fred Drake38cb9f12000-09-28 16:52:36 +000066
Fred Drake1fb5ce02001-07-02 16:55:42 +000067for m in sys.modules.values():
Barry Warsaw62d24882001-03-23 17:53:49 +000068 if hasattr(m, "__file__") and m.__file__:
Fred Drake1fb5ce02001-07-02 16:55:42 +000069 m.__file__ = os.path.abspath(m.__file__)
70del m
Fred Drake38cb9f12000-09-28 16:52:36 +000071
72# This ensures that the initial path provided by the interpreter contains
73# only absolute pathnames, even if we're running from the build directory.
74L = []
Fred Drake7f5296e2001-07-20 20:06:17 +000075_dirs_in_sys_path = {}
Fred Drake38cb9f12000-09-28 16:52:36 +000076for dir in sys.path:
Fred Drakefd4ff522001-07-12 21:08:33 +000077 # Filter out paths that don't exist, but leave in the empty string
Just van Rossumba634b22001-08-15 21:20:42 +000078 # since it's a special case. We also need to special-case the Mac,
79 # as file names are allowed on sys.path there.
80 if sys.platform != 'mac':
81 if dir and not os.path.isdir(dir):
82 continue
83 else:
84 if dir and not os.path.exists(dir):
85 continue
Fred Drake1fb5ce02001-07-02 16:55:42 +000086 dir, dircase = makepath(dir)
Fred Drake7f5296e2001-07-20 20:06:17 +000087 if not _dirs_in_sys_path.has_key(dircase):
Fred Drake38cb9f12000-09-28 16:52:36 +000088 L.append(dir)
Fred Drake7f5296e2001-07-20 20:06:17 +000089 _dirs_in_sys_path[dircase] = 1
Fred Drake38cb9f12000-09-28 16:52:36 +000090sys.path[:] = L
Neal Norwitz34172d52002-02-11 18:34:41 +000091del dir, dircase, L
Fred Drake38cb9f12000-09-28 16:52:36 +000092
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
Fred Drake7f5296e2001-07-20 20:06:17 +0000102def _init_pathinfo():
103 global _dirs_in_sys_path
104 _dirs_in_sys_path = d = {}
105 for dir in sys.path:
106 if dir and not os.path.isdir(dir):
107 continue
108 dir, dircase = makepath(dir)
109 d[dircase] = 1
110
Guido van Rossumf30bec71997-08-29 22:30:45 +0000111def addsitedir(sitedir):
Fred Drake7f5296e2001-07-20 20:06:17 +0000112 global _dirs_in_sys_path
113 if _dirs_in_sys_path is None:
114 _init_pathinfo()
115 reset = 1
116 else:
117 reset = 0
Fred Drake1fb5ce02001-07-02 16:55:42 +0000118 sitedir, sitedircase = makepath(sitedir)
Fred Drake7f5296e2001-07-20 20:06:17 +0000119 if not _dirs_in_sys_path.has_key(sitedircase):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 sys.path.append(sitedir) # Add path component
Guido van Rossumf30bec71997-08-29 22:30:45 +0000121 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000122 names = os.listdir(sitedir)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000123 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000124 return
Guido van Rossumf30bec71997-08-29 22:30:45 +0000125 names.sort()
126 for name in names:
Guido van Rossume2ae77b2001-10-24 20:42:55 +0000127 if name[-4:] == os.extsep + "pth":
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000128 addpackage(sitedir, name)
Fred Drake7f5296e2001-07-20 20:06:17 +0000129 if reset:
130 _dirs_in_sys_path = None
Guido van Rossumf30bec71997-08-29 22:30:45 +0000131
132def addpackage(sitedir, name):
Fred Drake7f5296e2001-07-20 20:06:17 +0000133 global _dirs_in_sys_path
134 if _dirs_in_sys_path is None:
135 _init_pathinfo()
136 reset = 1
137 else:
138 reset = 0
Guido van Rossumf30bec71997-08-29 22:30:45 +0000139 fullname = os.path.join(sitedir, name)
140 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000141 f = open(fullname)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000142 except IOError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000143 return
Guido van Rossumf30bec71997-08-29 22:30:45 +0000144 while 1:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000145 dir = f.readline()
146 if not dir:
147 break
148 if dir[0] == '#':
149 continue
Martin v. Löwisbb0a4b72001-01-11 13:02:43 +0000150 if dir.startswith("import"):
151 exec dir
152 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000153 if dir[-1] == '\n':
154 dir = dir[:-1]
Fred Drake1fb5ce02001-07-02 16:55:42 +0000155 dir, dircase = makepath(sitedir, dir)
Fred Drake7f5296e2001-07-20 20:06:17 +0000156 if not _dirs_in_sys_path.has_key(dircase) and os.path.exists(dir):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000157 sys.path.append(dir)
Fred Drake7f5296e2001-07-20 20:06:17 +0000158 _dirs_in_sys_path[dircase] = 1
159 if reset:
160 _dirs_in_sys_path = None
Guido van Rossumf30bec71997-08-29 22:30:45 +0000161
162prefixes = [sys.prefix]
163if sys.exec_prefix != sys.prefix:
164 prefixes.append(sys.exec_prefix)
165for prefix in prefixes:
Guido van Rossume57c96e1996-08-17 19:56:26 +0000166 if prefix:
Andrew MacIntyre5cef5712002-02-24 05:32:32 +0000167 if sys.platform == 'os2emx':
168 sitedirs = [os.path.join(prefix, "Lib", "site-packages")]
169 elif os.sep == '/':
Fred Drake1fb5ce02001-07-02 16:55:42 +0000170 sitedirs = [os.path.join(prefix,
171 "lib",
172 "python" + sys.version[:3],
173 "site-packages"),
174 os.path.join(prefix, "lib", "site-python")]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000175 else:
Tim Peters6a479f52001-07-12 05:20:13 +0000176 sitedirs = [prefix, os.path.join(prefix, "lib", "site-packages")]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000177 for sitedir in sitedirs:
178 if os.path.isdir(sitedir):
179 addsitedir(sitedir)
Neal Norwitz34172d52002-02-11 18:34:41 +0000180del prefix, sitedir
Guido van Rossume57c96e1996-08-17 19:56:26 +0000181
Fred Drake7f5296e2001-07-20 20:06:17 +0000182_dirs_in_sys_path = None
183
Fred Drake1fb5ce02001-07-02 16:55:42 +0000184
Guido van Rossumd89fa0c1998-08-07 18:01:14 +0000185# Define new built-ins 'quit' and 'exit'.
186# These are simply strings that display a hint on how to exit.
187if os.sep == ':':
188 exit = 'Use Cmd-Q to quit.'
189elif os.sep == '\\':
190 exit = 'Use Ctrl-Z plus Return to exit.'
191else:
192 exit = 'Use Ctrl-D (i.e. EOF) to exit.'
193import __builtin__
194__builtin__.quit = __builtin__.exit = exit
195del exit
196
Guido van Rossumd1252392000-09-05 04:39:55 +0000197# interactive prompt objects for printing the license text, a list of
198# contributors and the copyright notice.
199class _Printer:
200 MAXLINES = 23
201
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000202 def __init__(self, name, data, files=(), dirs=()):
203 self.__name = name
204 self.__data = data
205 self.__files = files
206 self.__dirs = dirs
207 self.__lines = None
208
209 def __setup(self):
210 if self.__lines:
211 return
212 data = None
213 for dir in self.__dirs:
214 for file in self.__files:
215 file = os.path.join(dir, file)
216 try:
217 fp = open(file)
218 data = fp.read()
219 fp.close()
220 break
221 except IOError:
222 pass
223 if data:
224 break
225 if not data:
226 data = self.__data
227 self.__lines = data.split('\n')
Guido van Rossumd1252392000-09-05 04:39:55 +0000228 self.__linecnt = len(self.__lines)
229
230 def __repr__(self):
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000231 self.__setup()
232 if len(self.__lines) <= self.MAXLINES:
233 return "\n".join(self.__lines)
234 else:
235 return "Type %s() to see the full %s text" % ((self.__name,)*2)
236
237 def __call__(self):
238 self.__setup()
Guido van Rossumd1252392000-09-05 04:39:55 +0000239 prompt = 'Hit Return for more, or q (and Return) to quit: '
240 lineno = 0
241 while 1:
242 try:
243 for i in range(lineno, lineno + self.MAXLINES):
244 print self.__lines[i]
245 except IndexError:
246 break
247 else:
248 lineno += self.MAXLINES
249 key = None
250 while key is None:
251 key = raw_input(prompt)
252 if key not in ('', 'q'):
253 key = None
254 if key == 'q':
255 break
Guido van Rossumd1252392000-09-05 04:39:55 +0000256
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000257__builtin__.copyright = _Printer("copyright", sys.copyright)
Barry Warsaw23f26ce2000-12-06 22:20:07 +0000258if sys.platform[:4] == 'java':
259 __builtin__.credits = _Printer(
260 "credits",
261 "Jython is maintained by the Jython developers (www.jython.org).")
262else:
263 __builtin__.credits = _Printer("credits", """\
264Thanks to CWI, CNRI, BeOpen.com, Digital Creations and a cast of thousands
265for supporting Python development. See www.python.org for more information.""")
Guido van Rossumd1252392000-09-05 04:39:55 +0000266here = os.path.dirname(os.__file__)
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000267__builtin__.license = _Printer(
Guido van Rossume37e96d2001-10-02 18:27:09 +0000268 "license", "See http://www.python.org/%.3s/license.html" % sys.version,
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000269 ["LICENSE.txt", "LICENSE"],
Barry Warsaw62d24882001-03-23 17:53:49 +0000270 [os.path.join(here, os.pardir), here, os.curdir])
Guido van Rossumd1252392000-09-05 04:39:55 +0000271
272
Guido van Rossum83213cc2001-06-12 16:48:52 +0000273# Define new built-in 'help'.
274# This is a wrapper around pydoc.help (with a twist).
275
276class _Helper:
277 def __repr__(self):
278 return "Type help() for interactive help, " \
279 "or help(object) for help about object."
280 def __call__(self, *args, **kwds):
281 import pydoc
282 return pydoc.help(*args, **kwds)
283
284__builtin__.help = _Helper()
285
286
Fredrik Lundh3fded4b2000-07-15 20:58:44 +0000287# Set the string encoding used by the Unicode implementation. The
288# default is 'ascii', but if you're willing to experiment, you can
289# change this.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000290
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000291encoding = "ascii" # Default value set by _PyUnicode_Init()
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000292
293if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000294 # Enable to support locale aware default string encodings.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000295 import locale
296 loc = locale.getdefaultlocale()
297 if loc[1]:
298 encoding = loc[1]
299
300if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000301 # Enable to switch off string to Unicode coercion and implicit
302 # Unicode to string conversion.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000303 encoding = "undefined"
304
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000305if encoding != "ascii":
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000306 # On Non-Unicode builds this will raise an AttributeError...
307 sys.setdefaultencoding(encoding) # Needs Python Unicode build !
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000308
309#
310# Run custom site specific code, if available.
311#
Guido van Rossume57c96e1996-08-17 19:56:26 +0000312try:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000313 import sitecustomize
Guido van Rossume57c96e1996-08-17 19:56:26 +0000314except ImportError:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000315 pass
316
317#
318# Remove sys.setdefaultencoding() so that users cannot change the
Fred Drake38cb9f12000-09-28 16:52:36 +0000319# encoding after initialization. The test for presence is needed when
Barry Warsaw23f26ce2000-12-06 22:20:07 +0000320# this module is run as a script, because this code is executed twice.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000321#
Fred Drake38cb9f12000-09-28 16:52:36 +0000322if hasattr(sys, "setdefaultencoding"):
323 del sys.setdefaultencoding
Guido van Rossumf30bec71997-08-29 22:30:45 +0000324
325def _test():
326 print "sys.path = ["
327 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000328 print " %s," % `dir`
Guido van Rossumf30bec71997-08-29 22:30:45 +0000329 print "]"
330
331if __name__ == '__main__':
332 _test()