blob: 484af7ee094e97768fc0a15a519ab61ba733556d [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{user} ---
2 A standard way to reference user-specific modules.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{user}
4
5\modulesynopsis{A standard way to reference user-specific modules.}
6
Fred Drake18f9f531998-01-13 18:45:34 +00007\indexii{.pythonrc.py}{file}
8\indexiii{user}{configuration}{file}
Guido van Rossum36764b81997-08-30 20:02:25 +00009
10As a policy, Python doesn't run user-specified code on startup of
11Python programs. (Only interactive sessions execute the script
Fred Drake7be8fcb1998-03-12 06:47:48 +000012specified in the \envvar{PYTHONSTARTUP} environment variable if it
13exists).
Guido van Rossum36764b81997-08-30 20:02:25 +000014
15However, some programs or sites may find it convenient to allow users
16to have a standard customization file, which gets run when a program
17requests it. This module implements such a mechanism. A program
18that wishes to use the mechanism must execute the statement
19
Fred Drake8d0ff311998-01-09 22:27:55 +000020\begin{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000021import user
Fred Drake8d0ff311998-01-09 22:27:55 +000022\end{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000023
Fred Drake8fab8cf1998-03-08 07:14:20 +000024The \module{user} module looks for a file \file{.pythonrc.py} in the user's
Guido van Rossum36764b81997-08-30 20:02:25 +000025home directory and if it can be opened, exececutes it (using
Fred Drake8fab8cf1998-03-08 07:14:20 +000026\function{execfile()}\bifuncindex{execfile}) in its own (i.e. the
27module \module{user}'s) global namespace. Errors during this phase
28are 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 Drake7be8fcb1998-03-12 06:47:48 +000030be named by the \envvar{HOME} environment variable; if this is not set,
Fred Drake8fab8cf1998-03-08 07:14:20 +000031the current directory is used.
Guido van Rossum36764b81997-08-30 20:02:25 +000032
33The user's \file{.pythonrc.py} could conceivably test for
34\code{sys.version} if it wishes to do different things depending on
35the Python version.
36
37A 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
39use it, changing the behavior of standard modules or functions is
40generally not a good idea.
41
42A suggestion for programmers who wish to use this mechanism: a simple
43way to let users specify options for your package is to have them
Fred Drake9d2f7d41997-12-16 14:43:37 +000044define variables in their \file{.pythonrc.py} file that you test in
Fred Drake8fab8cf1998-03-08 07:14:20 +000045your module. For example, a module \module{spam} that has a verbosity
Guido van Rossum36764b81997-08-30 20:02:25 +000046level can look for a variable \code{user.spam_verbose}, as follows:
47
Fred Drake19479911998-02-13 06:58:54 +000048\begin{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000049import user
50try:
51 verbose = user.spam_verbose # user's verbosity preference
52except AttributeError:
53 verbose = 0 # default verbosity
Fred Drake19479911998-02-13 06:58:54 +000054\end{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000055
56Programs with extensive customization needs are better off reading a
57program-specific customization file.
58
59Programs with security or privacy concerns should \emph{not} import
60this module; a user can easily break into a a program by placing
61arbitrary code in the \file{.pythonrc.py} file.
62
63Modules for general use should \emph{not} import this module; it may
64interfere with the operation of the importing program.
65
Fred Drake18f9f531998-01-13 18:45:34 +000066\begin{seealso}
67\seemodule{site}{site-wide customization mechanism}
Fred Drake54932051997-12-16 14:41:36 +000068\refstmodindex{site}
Fred Drake18f9f531998-01-13 18:45:34 +000069\end{seealso}