Guido van Rossum | 02840fd | 1997-08-28 14:32:14 +0000 | [diff] [blame] | 1 | """Hook to allow user-specified customization code to run. |
| 2 | |
| 3 | As a policy, Python doesn't run user-specified code on startup of |
| 4 | Python programs (interactive sessions execute the script specified in |
Guido van Rossum | bf45322 | 2000-03-30 15:00:33 +0000 | [diff] [blame] | 5 | the PYTHONSTARTUP environment variable if it exists). |
Guido van Rossum | 02840fd | 1997-08-28 14:32:14 +0000 | [diff] [blame] | 6 | |
| 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 |
Guido van Rossum | 625f40d | 1997-08-30 20:04:42 +0000 | [diff] [blame] | 10 | that wishes to use the mechanism must execute the statement |
Guido van Rossum | 02840fd | 1997-08-28 14:32:14 +0000 | [diff] [blame] | 11 | |
| 12 | import user |
| 13 | |
| 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. |
| 18 | |
| 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. |
| 21 | |
| 22 | """ |
| 23 | |
| 24 | import os |
| 25 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 26 | home = os.curdir # Default |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 27 | if 'HOME' in os.environ: |
Guido van Rossum | 02840fd | 1997-08-28 14:32:14 +0000 | [diff] [blame] | 28 | home = os.environ['HOME'] |
Neal Norwitz | 609ba81 | 2002-09-05 21:08:25 +0000 | [diff] [blame] | 29 | elif os.name == 'posix': |
| 30 | home = os.path.expanduser("~/") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 31 | elif os.name == 'nt': # Contributed by Jeff Bauer |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 32 | if 'HOMEPATH' in os.environ: |
| 33 | if 'HOMEDRIVE' in os.environ: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 34 | home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH'] |
| 35 | else: |
| 36 | home = os.environ['HOMEPATH'] |
Guido van Rossum | 02840fd | 1997-08-28 14:32:14 +0000 | [diff] [blame] | 37 | |
| 38 | pythonrc = os.path.join(home, ".pythonrc.py") |
| 39 | try: |
| 40 | f = open(pythonrc) |
| 41 | except IOError: |
| 42 | pass |
| 43 | else: |
| 44 | f.close() |
| 45 | execfile(pythonrc) |