blob: 39daf978c1ddb5355a51fabf1448c0eb834f53af [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
Guido van Rossum8291bb31997-12-09 19:46:45 +00008specified in the \code{PYTHONSTARTUP} environment variable if it exists).
Guido van Rossum36764b81997-08-30 20:02:25 +00009
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
Fred Drake54932051997-12-16 14:41:36 +000019The \code{user} module looks for a file \file{.pythonrc.py} in the user's
Guido van Rossum36764b81997-08-30 20:02:25 +000020home 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
Fred Drake54932051997-12-16 14:41:36 +000023program that imports the \code{user} module, if it wishes. The home
Guido van Rossum36764b81997-08-30 20:02:25 +000024directory 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
Fred Drake9d2f7d41997-12-16 14:43:37 +000038define variables in their \file{.pythonrc.py} file that you test in
Guido van Rossum36764b81997-08-30 20:02:25 +000039your 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}.
Fred Drake54932051997-12-16 14:41:36 +000061\refstmodindex{site}