blob: 12fe71d4599de5d8e91ca495caa3661d641235e2 [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
17(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
Fred Drake38cb9f12000-09-28 16:52:36 +000062def makepath(*paths):
63 dir = os.path.join(*paths)
64 return os.path.normcase(os.path.abspath(dir))
65
66L = sys.modules.values()
67for m in L:
68 if hasattr(m, "__file__"):
69 m.__file__ = makepath(m.__file__)
70del m, L
71
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 = []
75for dir in sys.path:
76 dir = makepath(dir)
77 if dir not in L:
78 L.append(dir)
79sys.path[:] = L
80del dir, L
81
Guido van Rossum48eb9cd2001-01-19 21:54:59 +000082# Append ./build/lib.<platform> in case we're running in the build dir
83# (especially for Guido :-)
84if os.name == "posix" and os.path.basename(sys.path[-1]) == "Modules":
85 from distutils.util import get_platform
86 s = "build/lib.%s-%.3s" % (get_platform(), sys.version)
87 s = os.path.join(os.path.dirname(sys.path[-1]), s)
88 sys.path.append(s)
89 del get_platform, s
90
Guido van Rossumf30bec71997-08-29 22:30:45 +000091def addsitedir(sitedir):
Fred Drake38cb9f12000-09-28 16:52:36 +000092 sitedir = makepath(sitedir)
Guido van Rossumf30bec71997-08-29 22:30:45 +000093 if sitedir not in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000094 sys.path.append(sitedir) # Add path component
Guido van Rossumf30bec71997-08-29 22:30:45 +000095 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000096 names = os.listdir(sitedir)
Guido van Rossumf30bec71997-08-29 22:30:45 +000097 except os.error:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000098 return
Guido van Rossumf30bec71997-08-29 22:30:45 +000099 names = map(os.path.normcase, names)
100 names.sort()
101 for name in names:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000102 if name[-4:] == ".pth":
103 addpackage(sitedir, name)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000104
105def addpackage(sitedir, name):
106 fullname = os.path.join(sitedir, name)
107 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000108 f = open(fullname)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000109 except IOError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000110 return
Guido van Rossumf30bec71997-08-29 22:30:45 +0000111 while 1:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000112 dir = f.readline()
113 if not dir:
114 break
115 if dir[0] == '#':
116 continue
Martin v. Löwisbb0a4b72001-01-11 13:02:43 +0000117 if dir.startswith("import"):
118 exec dir
119 continue
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 if dir[-1] == '\n':
121 dir = dir[:-1]
Fred Drake38cb9f12000-09-28 16:52:36 +0000122 dir = makepath(sitedir, dir)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000123 if dir not in sys.path and os.path.exists(dir):
124 sys.path.append(dir)
Guido van Rossumf30bec71997-08-29 22:30:45 +0000125
126prefixes = [sys.prefix]
127if sys.exec_prefix != sys.prefix:
128 prefixes.append(sys.exec_prefix)
129for prefix in prefixes:
Guido van Rossume57c96e1996-08-17 19:56:26 +0000130 if prefix:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000131 if os.sep == '/':
Fred Drake38cb9f12000-09-28 16:52:36 +0000132 sitedirs = [makepath(prefix,
133 "lib",
134 "python" + sys.version[:3],
135 "site-packages"),
136 makepath(prefix, "lib", "site-python")]
Jack Jansend49056c2000-12-12 22:39:04 +0000137 elif os.sep == ':':
138 sitedirs = [makepath(prefix, "lib", "site-packages")]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000139 else:
140 sitedirs = [prefix]
141 for sitedir in sitedirs:
142 if os.path.isdir(sitedir):
143 addsitedir(sitedir)
Guido van Rossume57c96e1996-08-17 19:56:26 +0000144
Guido van Rossumd89fa0c1998-08-07 18:01:14 +0000145# Define new built-ins 'quit' and 'exit'.
146# These are simply strings that display a hint on how to exit.
147if os.sep == ':':
148 exit = 'Use Cmd-Q to quit.'
149elif os.sep == '\\':
150 exit = 'Use Ctrl-Z plus Return to exit.'
151else:
152 exit = 'Use Ctrl-D (i.e. EOF) to exit.'
153import __builtin__
154__builtin__.quit = __builtin__.exit = exit
155del exit
156
Guido van Rossumd1252392000-09-05 04:39:55 +0000157# interactive prompt objects for printing the license text, a list of
158# contributors and the copyright notice.
159class _Printer:
160 MAXLINES = 23
161
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000162 def __init__(self, name, data, files=(), dirs=()):
163 self.__name = name
164 self.__data = data
165 self.__files = files
166 self.__dirs = dirs
167 self.__lines = None
168
169 def __setup(self):
170 if self.__lines:
171 return
172 data = None
173 for dir in self.__dirs:
174 for file in self.__files:
175 file = os.path.join(dir, file)
176 try:
177 fp = open(file)
178 data = fp.read()
179 fp.close()
180 break
181 except IOError:
182 pass
183 if data:
184 break
185 if not data:
186 data = self.__data
187 self.__lines = data.split('\n')
Guido van Rossumd1252392000-09-05 04:39:55 +0000188 self.__linecnt = len(self.__lines)
189
190 def __repr__(self):
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000191 self.__setup()
192 if len(self.__lines) <= self.MAXLINES:
193 return "\n".join(self.__lines)
194 else:
195 return "Type %s() to see the full %s text" % ((self.__name,)*2)
196
197 def __call__(self):
198 self.__setup()
Guido van Rossumd1252392000-09-05 04:39:55 +0000199 prompt = 'Hit Return for more, or q (and Return) to quit: '
200 lineno = 0
201 while 1:
202 try:
203 for i in range(lineno, lineno + self.MAXLINES):
204 print self.__lines[i]
205 except IndexError:
206 break
207 else:
208 lineno += self.MAXLINES
209 key = None
210 while key is None:
211 key = raw_input(prompt)
212 if key not in ('', 'q'):
213 key = None
214 if key == 'q':
215 break
Guido van Rossumd1252392000-09-05 04:39:55 +0000216
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000217__builtin__.copyright = _Printer("copyright", sys.copyright)
Barry Warsaw23f26ce2000-12-06 22:20:07 +0000218if sys.platform[:4] == 'java':
219 __builtin__.credits = _Printer(
220 "credits",
221 "Jython is maintained by the Jython developers (www.jython.org).")
222else:
223 __builtin__.credits = _Printer("credits", """\
224Thanks to CWI, CNRI, BeOpen.com, Digital Creations and a cast of thousands
225for supporting Python development. See www.python.org for more information.""")
Guido van Rossumd1252392000-09-05 04:39:55 +0000226here = os.path.dirname(os.__file__)
Guido van Rossumf19a7ac2000-10-03 17:11:37 +0000227__builtin__.license = _Printer(
228 "license", "See http://www.pythonlabs.com/products/python2.0/license.html",
229 ["LICENSE.txt", "LICENSE"],
230 [here, os.path.join(here, os.pardir), os.curdir])
Guido van Rossumd1252392000-09-05 04:39:55 +0000231
232
Fredrik Lundh3fded4b2000-07-15 20:58:44 +0000233# Set the string encoding used by the Unicode implementation. The
234# default is 'ascii', but if you're willing to experiment, you can
235# change this.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000236
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000237encoding = "ascii" # Default value set by _PyUnicode_Init()
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000238
239if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000240 # Enable to support locale aware default string encodings.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000241 import locale
242 loc = locale.getdefaultlocale()
243 if loc[1]:
244 encoding = loc[1]
245
246if 0:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000247 # Enable to switch off string to Unicode coercion and implicit
248 # Unicode to string conversion.
Fredrik Lundh47ac1262000-07-15 20:45:23 +0000249 encoding = "undefined"
250
Marc-André Lemburg09cad082000-09-18 11:06:00 +0000251if encoding != "ascii":
252 sys.setdefaultencoding(encoding)
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000253
254#
255# Run custom site specific code, if available.
256#
Guido van Rossume57c96e1996-08-17 19:56:26 +0000257try:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000258 import sitecustomize
Guido van Rossume57c96e1996-08-17 19:56:26 +0000259except ImportError:
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000260 pass
261
262#
263# Remove sys.setdefaultencoding() so that users cannot change the
Fred Drake38cb9f12000-09-28 16:52:36 +0000264# encoding after initialization. The test for presence is needed when
Barry Warsaw23f26ce2000-12-06 22:20:07 +0000265# this module is run as a script, because this code is executed twice.
Marc-André Lemburg990bbe92000-06-07 09:12:09 +0000266#
Fred Drake38cb9f12000-09-28 16:52:36 +0000267if hasattr(sys, "setdefaultencoding"):
268 del sys.setdefaultencoding
Guido van Rossumf30bec71997-08-29 22:30:45 +0000269
270def _test():
271 print "sys.path = ["
272 for dir in sys.path:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000273 print " %s," % `dir`
Guido van Rossumf30bec71997-08-29 22:30:45 +0000274 print "]"
275
276if __name__ == '__main__':
277 _test()