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