Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{user}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 2 | \declaremodule{standard}{user} |
| 3 | |
| 4 | \modulesynopsis{A standard way to reference user-specific modules.} |
| 5 | |
Fred Drake | 18f9f53 | 1998-01-13 18:45:34 +0000 | [diff] [blame] | 6 | \indexii{.pythonrc.py}{file} |
| 7 | \indexiii{user}{configuration}{file} |
Guido van Rossum | 36764b8 | 1997-08-30 20:02:25 +0000 | [diff] [blame] | 8 | |
| 9 | As a policy, Python doesn't run user-specified code on startup of |
| 10 | Python programs. (Only interactive sessions execute the script |
Fred Drake | 7be8fcb | 1998-03-12 06:47:48 +0000 | [diff] [blame] | 11 | specified in the \envvar{PYTHONSTARTUP} environment variable if it |
| 12 | exists). |
Guido van Rossum | 36764b8 | 1997-08-30 20:02:25 +0000 | [diff] [blame] | 13 | |
| 14 | However, some programs or sites may find it convenient to allow users |
| 15 | to have a standard customization file, which gets run when a program |
| 16 | requests it. This module implements such a mechanism. A program |
| 17 | that wishes to use the mechanism must execute the statement |
| 18 | |
Fred Drake | 8d0ff31 | 1998-01-09 22:27:55 +0000 | [diff] [blame] | 19 | \begin{verbatim} |
Guido van Rossum | 36764b8 | 1997-08-30 20:02:25 +0000 | [diff] [blame] | 20 | import user |
Fred Drake | 8d0ff31 | 1998-01-09 22:27:55 +0000 | [diff] [blame] | 21 | \end{verbatim} |
Guido van Rossum | 36764b8 | 1997-08-30 20:02:25 +0000 | [diff] [blame] | 22 | |
Fred Drake | 8fab8cf | 1998-03-08 07:14:20 +0000 | [diff] [blame] | 23 | The \module{user} module looks for a file \file{.pythonrc.py} in the user's |
Guido van Rossum | 36764b8 | 1997-08-30 20:02:25 +0000 | [diff] [blame] | 24 | home directory and if it can be opened, exececutes it (using |
Fred Drake | 8fab8cf | 1998-03-08 07:14:20 +0000 | [diff] [blame] | 25 | \function{execfile()}\bifuncindex{execfile}) in its own (i.e. the |
| 26 | module \module{user}'s) global namespace. Errors during this phase |
| 27 | are not caught; that's up to the program that imports the |
| 28 | \module{user} module, if it wishes. The home directory is assumed to |
Fred Drake | 7be8fcb | 1998-03-12 06:47:48 +0000 | [diff] [blame] | 29 | be named by the \envvar{HOME} environment variable; if this is not set, |
Fred Drake | 8fab8cf | 1998-03-08 07:14:20 +0000 | [diff] [blame] | 30 | the current directory is used. |
Guido van Rossum | 36764b8 | 1997-08-30 20:02:25 +0000 | [diff] [blame] | 31 | |
| 32 | The user's \file{.pythonrc.py} could conceivably test for |
| 33 | \code{sys.version} if it wishes to do different things depending on |
| 34 | the Python version. |
| 35 | |
| 36 | A warning to users: be very conservative in what you place in your |
| 37 | \file{.pythonrc.py} file. Since you don't know which programs will |
| 38 | use it, changing the behavior of standard modules or functions is |
| 39 | generally not a good idea. |
| 40 | |
| 41 | A suggestion for programmers who wish to use this mechanism: a simple |
| 42 | way to let users specify options for your package is to have them |
Fred Drake | 9d2f7d4 | 1997-12-16 14:43:37 +0000 | [diff] [blame] | 43 | define variables in their \file{.pythonrc.py} file that you test in |
Fred Drake | 8fab8cf | 1998-03-08 07:14:20 +0000 | [diff] [blame] | 44 | your module. For example, a module \module{spam} that has a verbosity |
Guido van Rossum | 36764b8 | 1997-08-30 20:02:25 +0000 | [diff] [blame] | 45 | level can look for a variable \code{user.spam_verbose}, as follows: |
| 46 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 47 | \begin{verbatim} |
Guido van Rossum | 36764b8 | 1997-08-30 20:02:25 +0000 | [diff] [blame] | 48 | import user |
| 49 | try: |
| 50 | verbose = user.spam_verbose # user's verbosity preference |
| 51 | except AttributeError: |
| 52 | verbose = 0 # default verbosity |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 53 | \end{verbatim} |
Guido van Rossum | 36764b8 | 1997-08-30 20:02:25 +0000 | [diff] [blame] | 54 | |
| 55 | Programs with extensive customization needs are better off reading a |
| 56 | program-specific customization file. |
| 57 | |
| 58 | Programs with security or privacy concerns should \emph{not} import |
| 59 | this module; a user can easily break into a a program by placing |
| 60 | arbitrary code in the \file{.pythonrc.py} file. |
| 61 | |
| 62 | Modules for general use should \emph{not} import this module; it may |
| 63 | interfere with the operation of the importing program. |
| 64 | |
Fred Drake | 18f9f53 | 1998-01-13 18:45:34 +0000 | [diff] [blame] | 65 | \begin{seealso} |
| 66 | \seemodule{site}{site-wide customization mechanism} |
Fred Drake | 5493205 | 1997-12-16 14:41:36 +0000 | [diff] [blame] | 67 | \refstmodindex{site} |
Fred Drake | 18f9f53 | 1998-01-13 18:45:34 +0000 | [diff] [blame] | 68 | \end{seealso} |