1 """Append module search paths for third-party packages to sys.path.
3 ****************************************************************
4 * This module is automatically imported during initialization. *
5 ****************************************************************
7 This will append site-specific paths to the module search path. On
8 Unix (including Mac OSX), it starts with sys.prefix and
9 sys.exec_prefix (if different) and appends
10 lib/python<version>/site-packages as well as lib/site-python.
11 On other platforms (such as Windows), it tries each of the
12 prefixes directly, as well as with lib/site-packages appended. The
13 resulting directories, if they exist, are appended to sys.path, and
14 also inspected for path configuration files.
16 A path configuration file is a file whose name has the form
17 <package>.pth; its contents are additional directories (one per line)
18 to be added to sys.path. Non-existing directories (or
19 non-directories) are never added to sys.path; no directory is added to
20 sys.path more than once. Blank lines and lines beginning with
21 '#' are skipped. Lines starting with 'import' are executed.
23 For example, suppose sys.prefix and sys.exec_prefix are set to
24 /usr/local and there is a directory /usr/local/lib/python2.5/site-packages
25 with three subdirectories, foo, bar and spam, and two path
26 configuration files, foo.pth and bar.pth. Assume foo.pth contains the
29 # foo package configuration
36 # bar package configuration
39 Then the following directories are added to sys.path, in this order:
41 /usr/local/lib/python2.5/site-packages/bar
42 /usr/local/lib/python2.5/site-packages/foo
44 Note that bletch is omitted because it doesn't exist; bar precedes foo
45 because bar.pth comes alphabetically before foo.pth; and spam is
46 omitted because it is not mentioned in either path configuration file.
48 After these path manipulations, an attempt is made to import a module
49 named sitecustomize, which can perform arbitrary additional
50 site-specific customizations. If this import fails with an
51 ImportError exception, it is silently ignored.
59 # Prefixes for site-packages; add additional prefixes like /usr/local here
60 PREFIXES
= [sys
.prefix
, sys
.exec_prefix
]
61 # Enable per user site-packages directory
62 # set it to False to disable the feature or True to force the feature
63 ENABLE_USER_SITE
= None
64 # for distutils.commands.install
70 dir = os
.path
.abspath(os
.path
.join(*paths
))
71 return dir, os
.path
.normcase(dir)
75 """Set all module' __file__ attribute to an absolute path"""
76 for m
in set(sys
.modules
.values()):
77 if hasattr(m
, '__loader__'):
78 continue # don't mess with a PEP 302-supplied __file__
80 m
.__file
__ = os
.path
.abspath(m
.__file
__)
81 except AttributeError:
86 """ Remove duplicate entries from sys.path along with making them
88 # This ensures that the initial path provided by the interpreter contains
89 # only absolute pathnames, even if we're running from the build directory.
93 # Filter out duplicate paths (on case-insensitive file systems also
94 # if they only differ in case); turn relative paths into absolute
96 dir, dircase
= makepath(dir)
97 if not dircase
in known_paths
:
99 known_paths
.add(dircase
)
103 # XXX This should not be part of site.py, since it is needed even when
104 # using the -S option for Python. See http://www.python.org/sf/586680
106 """Append ./build/lib.<platform> in case we're running in the build dir
107 (especially for Guido :-)"""
108 from distutils
.util
import get_platform
109 s
= "build/lib.%s-%.3s" % (get_platform(), sys
.version
)
110 if hasattr(sys
, 'gettotalrefcount'):
112 s
= os
.path
.join(os
.path
.dirname(sys
.path
[-1]), s
)
116 def _init_pathinfo():
117 """Return a set containing all existing directory entries from sys.path"""
121 if os
.path
.isdir(dir):
122 dir, dircase
= makepath(dir)
129 def addpackage(sitedir
, name
, known_paths
):
130 """Process a .pth file within the site-packages directory:
131 For each line in the file, either combine it with sitedir to a path
132 and add that to known_paths, or execute it if it starts with 'import '.
134 if known_paths
is None:
139 fullname
= os
.path
.join(sitedir
, name
)
141 f
= open(fullname
, "rU")
146 if line
.startswith("#"):
148 if line
.startswith(("import ", "import\t")):
152 dir, dircase
= makepath(sitedir
, line
)
153 if not dircase
in known_paths
and os
.path
.exists(dir):
155 known_paths
.add(dircase
)
161 def addsitedir(sitedir
, known_paths
=None):
162 """Add 'sitedir' argument to sys.path if missing and handle .pth files in
164 if known_paths
is None:
165 known_paths
= _init_pathinfo()
169 sitedir
, sitedircase
= makepath(sitedir
)
170 if not sitedircase
in known_paths
:
171 sys
.path
.append(sitedir
) # Add path component
173 names
= os
.listdir(sitedir
)
176 names
= [name
for name
in names
if name
.endswith(".pth")]
177 for name
in sorted(names
):
178 addpackage(sitedir
, name
, known_paths
)
184 def check_enableusersite():
185 """Check if user site directory is safe for inclusion
187 The function tests for the command line flag (including environment var),
188 process uid/gid equal to effective uid/gid.
190 None: Disabled for security reasons
191 False: Disabled by user (command line option)
192 True: Safe and enabled
194 if sys
.flags
.no_user_site
:
197 if hasattr(os
, "getuid") and hasattr(os
, "geteuid"):
198 # check process uid == effective uid
199 if os
.geteuid() != os
.getuid():
201 if hasattr(os
, "getgid") and hasattr(os
, "getegid"):
202 # check process gid == effective gid
203 if os
.getegid() != os
.getgid():
209 def addusersitepackages(known_paths
):
210 """Add a per user site-package to sys.path
212 Each user has its own python directory with site-packages in the
215 USER_BASE is the root directory for all Python versions
217 USER_SITE is the user specific site-packages directory
219 USER_SITE/.. can be used for data.
221 global USER_BASE
, USER_SITE
, ENABLE_USER_SITE
222 env_base
= os
.environ
.get("PYTHONUSERBASE", None)
225 return os
.path
.expanduser(os
.path
.join(*args
))
227 #if sys.platform in ('os2emx', 'riscos'):
228 # # Don't know what to put here
232 base
= os
.environ
.get("APPDATA") or "~"
233 USER_BASE
= env_base
if env_base
else joinuser(base
, "Python")
234 USER_SITE
= os
.path
.join(USER_BASE
,
235 "Python" + sys
.version
[0] + sys
.version
[2],
238 USER_BASE
= env_base
if env_base
else joinuser("~", ".local")
239 USER_SITE
= os
.path
.join(USER_BASE
, "lib",
240 "python" + sys
.version
[:3],
243 if ENABLE_USER_SITE
and os
.path
.isdir(USER_SITE
):
244 addsitedir(USER_SITE
, known_paths
)
248 def addsitepackages(known_paths
):
249 """Add site-packages (and possibly site-python) to sys.path"""
253 for prefix
in PREFIXES
:
254 if not prefix
or prefix
in seen
:
258 if sys
.platform
in ('os2emx', 'riscos'):
259 sitedirs
.append(os
.path
.join(prefix
, "Lib", "site-packages"))
261 sitedirs
.append(os
.path
.join(prefix
, "lib",
262 "python" + sys
.version
[:3],
264 sitedirs
.append(os
.path
.join(prefix
, "lib", "site-python"))
266 sitedirs
.append(prefix
)
267 sitedirs
.append(os
.path
.join(prefix
, "lib", "site-packages"))
269 if sys
.platform
== "darwin":
270 # for framework builds *only* we add the standard Apple
272 if 'Python.framework' in prefix
:
275 os
.path
.join("~", "Library", "Python",
276 sys
.version
[:3], "site-packages")))
278 os
.path
.join("/Library", "Python",
279 sys
.version
[:3], "site-packages"))
281 for sitedir
in sitedirs
:
282 if os
.path
.isdir(sitedir
):
283 addsitedir(sitedir
, known_paths
)
288 def setBEGINLIBPATH():
289 """The OS/2 EMX port has optional extension modules that do double duty
290 as DLLs (and must use the .DLL file extension) for other extensions.
291 The library search path needs to be amended so these will be found
292 during module import. Use BEGINLIBPATH so that these are at the start
293 of the library search path.
296 dllpath
= os
.path
.join(sys
.prefix
, "Lib", "lib-dynload")
297 libpath
= os
.environ
['BEGINLIBPATH'].split(';')
299 libpath
.append(dllpath
)
301 libpath
[-1] = dllpath
302 os
.environ
['BEGINLIBPATH'] = ';'.join(libpath
)
306 """Define new built-ins 'quit' and 'exit'.
307 These are simply strings that display a hint on how to exit.
313 eof
= 'Ctrl-Z plus Return'
315 eof
= 'Ctrl-D (i.e. EOF)'
317 class Quitter(object):
318 def __init__(self
, name
):
321 return 'Use %s() or %s to exit' % (self
.name
, eof
)
322 def __call__(self
, code
=None):
323 # Shells like IDLE catch the SystemExit, but listen when their
324 # stdin wrapper is closed.
327 if hasattr(sys
.stdin
, "fileno"):
328 fd
= sys
.stdin
.fileno()
330 # Don't close stdin if it wraps fd 0
334 raise SystemExit(code
)
335 builtins
.quit
= Quitter('quit')
336 builtins
.exit
= Quitter('exit')
339 class _Printer(object):
340 """interactive prompt objects for printing the license text, a list of
341 contributors and the copyright notice."""
345 def __init__(self
, name
, data
, files
=(), dirs
=()):
356 for dir in self
.__dirs
:
357 for filename
in self
.__files
:
358 filename
= os
.path
.join(dir, filename
)
360 fp
= open(filename
, "rU")
370 self
.__lines
= data
.split('\n')
371 self
.__linecnt
= len(self
.__lines
)
375 if len(self
.__lines
) <= self
.MAXLINES
:
376 return "\n".join(self
.__lines
)
378 return "Type %s() to see the full %s text" % ((self
.__name
,)*2)
382 prompt
= 'Hit Return for more, or q (and Return) to quit: '
386 for i
in range(lineno
, lineno
+ self
.MAXLINES
):
387 print(self
.__lines
[i
])
391 lineno
+= self
.MAXLINES
395 if key
not in ('', 'q'):
401 """Set 'copyright' and 'credits' in builtins"""
402 builtins
.copyright
= _Printer("copyright", sys
.copyright
)
403 if sys
.platform
[:4] == 'java':
404 builtins
.credits
= _Printer(
406 "Jython is maintained by the Jython developers (www.jython.org).")
408 builtins
.credits
= _Printer("credits", """\
409 Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
410 for supporting Python development. See www.python.org for more information.""")
411 here
= os
.path
.dirname(os
.__file
__)
412 builtins
.license
= _Printer(
413 "license", "See http://www.python.org/%.3s/license.html" % sys
.version
,
414 ["LICENSE.txt", "LICENSE"],
415 [os
.path
.join(here
, os
.pardir
), here
, os
.curdir
])
418 class _Helper(object):
419 """Define the built-in 'help'.
420 This is a wrapper around pydoc.help (with a twist).
425 return "Type help() for interactive help, " \
426 "or help(object) for help about object."
427 def __call__(self
, *args
, **kwds
):
429 return pydoc
.help(*args
, **kwds
)
432 builtins
.help = _Helper()
435 """On Windows, some default encodings are not provided by Python,
436 while they are always available as "mbcs" in each locale. Make
437 them usable by aliasing to "mbcs" in such a case."""
438 if sys
.platform
== 'win32':
439 import locale
, codecs
440 enc
= locale
.getdefaultlocale()[1]
441 if enc
.startswith('cp'): # "cp***" ?
446 encodings
._cache
[enc
] = encodings
._unknown
447 encodings
.aliases
.aliases
[enc
] = 'mbcs'
450 """Set the string encoding used by the Unicode implementation. The
451 default is 'ascii', but if you're willing to experiment, you can
453 encoding
= "ascii" # Default value set by _PyUnicode_Init()
455 # Enable to support locale aware default string encodings.
457 loc
= locale
.getdefaultlocale()
461 # Enable to switch off string to Unicode coercion and implicit
462 # Unicode to string conversion.
463 encoding
= "undefined"
464 if encoding
!= "ascii":
465 # On Non-Unicode builds this will raise an AttributeError...
466 sys
.setdefaultencoding(encoding
) # Needs Python Unicode build !
469 def execsitecustomize():
470 """Run custom site specific code, if available."""
475 except Exception as err
:
476 if os
.environ
.get("PYTHONVERBOSE"):
477 sys
.excepthook(*sys
.exc_info())
480 "Error in sitecustomize; set PYTHONVERBOSE for traceback:\n"
482 (err
.__class
__.__name
__, err
))
485 def execusercustomize():
486 """Run custom user specific code, if available."""
491 except Exception as err
:
492 if os
.environ
.get("PYTHONVERBOSE"):
493 sys
.excepthook(*sys
.exc_info())
496 "Error in usercustomize; set PYTHONVERBOSE for traceback:\n"
498 (err
.__class
__.__name
__, err
))
502 global ENABLE_USER_SITE
505 known_paths
= removeduppaths()
506 if (os
.name
== "posix" and sys
.path
and
507 os
.path
.basename(sys
.path
[-1]) == "Modules"):
509 if ENABLE_USER_SITE
is None:
510 ENABLE_USER_SITE
= check_enableusersite()
511 known_paths
= addusersitepackages(known_paths
)
512 known_paths
= addsitepackages(known_paths
)
513 if sys
.platform
== 'os2emx':
523 # Remove sys.setdefaultencoding() so that users cannot change the
524 # encoding after initialization. The test for presence is needed when
525 # this module is run as a script, because this code is executed twice.
526 if hasattr(sys
, "setdefaultencoding"):
527 del sys
.setdefaultencoding
533 %s [--user-base] [--user-site]
535 Without arguments print some useful information
536 With arguments print the value of USER_BASE and/or USER_SITE separated
539 Exit codes with --user-base or --user-site:
540 0 - user site directory is enabled
541 1 - user site directory is disabled by user
542 2 - uses site directory is disabled by super user
543 or for security reasons
548 print("sys.path = [")
550 print(" %r," % (dir,))
552 print("USER_BASE: %r (%s)" % (USER_BASE
,
553 "exists" if os
.path
.isdir(USER_BASE
) else "doesn't exist"))
554 print("USER_SITE: %r (%s)" % (USER_SITE
,
555 "exists" if os
.path
.isdir(USER_SITE
) else "doesn't exist"))
556 print("ENABLE_USER_SITE: %r" % ENABLE_USER_SITE
)
560 if '--user-base' in args
:
561 buffer.append(USER_BASE
)
562 if '--user-site' in args
:
563 buffer.append(USER_SITE
)
566 print(os
.pathsep
.join(buffer))
569 elif ENABLE_USER_SITE
is False:
571 elif ENABLE_USER_SITE
is None:
577 print(textwrap
.dedent(help % (sys
.argv
[0], os
.pathsep
)))
580 if __name__
== '__main__':