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