blob: 67317a2ccd1478389773089df4865efb02d0476b [file] [log] [blame]
Guido van Rossum36764b81997-08-30 20:02:25 +00001\section{Standard Module \sectcode{user}}
2\label{module-user}
3\stmodindex{user}
Fred Drake18f9f531998-01-13 18:45:34 +00004\indexii{.pythonrc.py}{file}
5\indexiii{user}{configuration}{file}
Guido van Rossum36764b81997-08-30 20:02:25 +00006
7As a policy, Python doesn't run user-specified code on startup of
8Python programs. (Only interactive sessions execute the script
Guido van Rossum8291bb31997-12-09 19:46:45 +00009specified in the \code{PYTHONSTARTUP} environment variable if it exists).
Guido van Rossum36764b81997-08-30 20:02:25 +000010
11However, some programs or sites may find it convenient to allow users
12to have a standard customization file, which gets run when a program
13requests it. This module implements such a mechanism. A program
14that wishes to use the mechanism must execute the statement
15
Fred Drake8d0ff311998-01-09 22:27:55 +000016\begin{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000017import user
Fred Drake8d0ff311998-01-09 22:27:55 +000018\end{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000019
Fred Drake8fab8cf1998-03-08 07:14:20 +000020The \module{user} module looks for a file \file{.pythonrc.py} in the user's
Guido van Rossum36764b81997-08-30 20:02:25 +000021home directory and if it can be opened, exececutes it (using
Fred Drake8fab8cf1998-03-08 07:14:20 +000022\function{execfile()}\bifuncindex{execfile}) in its own (i.e. the
23module \module{user}'s) global namespace. Errors during this phase
24are not caught; that's up to the program that imports the
25\module{user} module, if it wishes. The home directory is assumed to
26be named by the \code{HOME} environment variable; if this is not set,
27the current directory is used.
Guido van Rossum36764b81997-08-30 20:02:25 +000028
29The user's \file{.pythonrc.py} could conceivably test for
30\code{sys.version} if it wishes to do different things depending on
31the Python version.
32
33A warning to users: be very conservative in what you place in your
34\file{.pythonrc.py} file. Since you don't know which programs will
35use it, changing the behavior of standard modules or functions is
36generally not a good idea.
37
38A suggestion for programmers who wish to use this mechanism: a simple
39way to let users specify options for your package is to have them
Fred Drake9d2f7d41997-12-16 14:43:37 +000040define variables in their \file{.pythonrc.py} file that you test in
Fred Drake8fab8cf1998-03-08 07:14:20 +000041your module. For example, a module \module{spam} that has a verbosity
Guido van Rossum36764b81997-08-30 20:02:25 +000042level can look for a variable \code{user.spam_verbose}, as follows:
43
Fred Drake19479911998-02-13 06:58:54 +000044\begin{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000045import user
46try:
47 verbose = user.spam_verbose # user's verbosity preference
48except AttributeError:
49 verbose = 0 # default verbosity
Fred Drake19479911998-02-13 06:58:54 +000050\end{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000051
52Programs with extensive customization needs are better off reading a
53program-specific customization file.
54
55Programs with security or privacy concerns should \emph{not} import
56this module; a user can easily break into a a program by placing
57arbitrary code in the \file{.pythonrc.py} file.
58
59Modules for general use should \emph{not} import this module; it may
60interfere with the operation of the importing program.
61
Fred Drake18f9f531998-01-13 18:45:34 +000062\begin{seealso}
63\seemodule{site}{site-wide customization mechanism}
Fred Drake54932051997-12-16 14:41:36 +000064\refstmodindex{site}
Fred Drake18f9f531998-01-13 18:45:34 +000065\end{seealso}