Issue #6629: Fix a data corruption issue in the new `io` package, which could
[python.git] / Lib / user.py
blob596f0a74628eaa144a6437fec644a2d00df4a392
1 """Hook to allow user-specified customization code to run.
3 As a policy, Python doesn't run user-specified code on startup of
4 Python programs (interactive sessions execute the script specified in
5 the PYTHONSTARTUP environment variable if it exists).
7 However, some programs or sites may find it convenient to allow users
8 to have a standard customization file, which gets run when a program
9 requests it. This module implements such a mechanism. A program
10 that wishes to use the mechanism must execute the statement
12 import user
14 The user module looks for a file .pythonrc.py in the user's home
15 directory and if it can be opened, execfile()s it in its own global
16 namespace. Errors during this phase are not caught; that's up to the
17 program that imports the user module, if it wishes.
19 The user's .pythonrc.py could conceivably test for sys.version if it
20 wishes to do different things depending on the Python version.
22 """
23 from warnings import warnpy3k
24 warnpy3k("the user module has been removed in Python 3.0", stacklevel=2)
25 del warnpy3k
27 import os
29 home = os.curdir # Default
30 if 'HOME' in os.environ:
31 home = os.environ['HOME']
32 elif os.name == 'posix':
33 home = os.path.expanduser("~/")
34 elif os.name == 'nt': # Contributed by Jeff Bauer
35 if 'HOMEPATH' in os.environ:
36 if 'HOMEDRIVE' in os.environ:
37 home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
38 else:
39 home = os.environ['HOMEPATH']
41 pythonrc = os.path.join(home, ".pythonrc.py")
42 try:
43 f = open(pythonrc)
44 except IOError:
45 pass
46 else:
47 f.close()
48 execfile(pythonrc)