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