blob: ee8ed88d638773b4a5ec03cd0852e613dacf51e3 [file] [log] [blame]
Guido van Rossum02840fd1997-08-28 14:32:14 +00001"""Hook to allow user-specified customization code to run.
2
3As a policy, Python doesn't run user-specified code on startup of
4Python programs (interactive sessions execute the script specified in
Guido van Rossumbf453222000-03-30 15:00:33 +00005the PYTHONSTARTUP environment variable if it exists).
Guido van Rossum02840fd1997-08-28 14:32:14 +00006
7However, some programs or sites may find it convenient to allow users
8to have a standard customization file, which gets run when a program
9requests it. This module implements such a mechanism. A program
Guido van Rossum625f40d1997-08-30 20:04:42 +000010that wishes to use the mechanism must execute the statement
Guido van Rossum02840fd1997-08-28 14:32:14 +000011
12 import user
13
14The user module looks for a file .pythonrc.py in the user's home
15directory and if it can be opened, execfile()s it in its own global
16namespace. Errors during this phase are not caught; that's up to the
17program that imports the user module, if it wishes.
18
19The user's .pythonrc.py could conceivably test for sys.version if it
20wishes to do different things depending on the Python version.
21
22"""
23
24import os
25
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000026home = os.curdir # Default
Guido van Rossum3fa440e1997-12-03 22:34:03 +000027if os.environ.has_key('HOME'):
Guido van Rossum02840fd1997-08-28 14:32:14 +000028 home = os.environ['HOME']
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000029elif os.name == 'nt': # Contributed by Jeff Bauer
Guido van Rossum3fa440e1997-12-03 22:34:03 +000030 if os.environ.has_key('HOMEPATH'):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000031 if os.environ.has_key('HOMEDRIVE'):
32 home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
33 else:
34 home = os.environ['HOMEPATH']
Guido van Rossum02840fd1997-08-28 14:32:14 +000035
36pythonrc = os.path.join(home, ".pythonrc.py")
37try:
38 f = open(pythonrc)
39except IOError:
40 pass
41else:
42 f.close()
43 execfile(pythonrc)