blob: d5d8d5b7d107fb22d86b3906587d595812dae85f [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 Drake54932051997-12-16 14:41:36 +000020The \code{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
22\code{execfile()}) in its own (i.e. the module \code{user}'s) global
23namespace. Errors during this phase are not caught; that's up to the
Fred Drake54932051997-12-16 14:41:36 +000024program that imports the \code{user} module, if it wishes. The home
Guido van Rossum36764b81997-08-30 20:02:25 +000025directory is assumed to be named by the \code{HOME} environment
26variable; if this is not set, the current directory is used.
27
28The user's \file{.pythonrc.py} could conceivably test for
29\code{sys.version} if it wishes to do different things depending on
30the Python version.
31
32A warning to users: be very conservative in what you place in your
33\file{.pythonrc.py} file. Since you don't know which programs will
34use it, changing the behavior of standard modules or functions is
35generally not a good idea.
36
37A suggestion for programmers who wish to use this mechanism: a simple
38way to let users specify options for your package is to have them
Fred Drake9d2f7d41997-12-16 14:43:37 +000039define variables in their \file{.pythonrc.py} file that you test in
Guido van Rossum36764b81997-08-30 20:02:25 +000040your module. For example, a module \code{spam} that has a verbosity
41level can look for a variable \code{user.spam_verbose}, as follows:
42
Fred Drake19479911998-02-13 06:58:54 +000043\begin{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000044import user
45try:
46 verbose = user.spam_verbose # user's verbosity preference
47except AttributeError:
48 verbose = 0 # default verbosity
Fred Drake19479911998-02-13 06:58:54 +000049\end{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000050
51Programs with extensive customization needs are better off reading a
52program-specific customization file.
53
54Programs with security or privacy concerns should \emph{not} import
55this module; a user can easily break into a a program by placing
56arbitrary code in the \file{.pythonrc.py} file.
57
58Modules for general use should \emph{not} import this module; it may
59interfere with the operation of the importing program.
60
Fred Drake18f9f531998-01-13 18:45:34 +000061\begin{seealso}
62\seemodule{site}{site-wide customization mechanism}
Fred Drake54932051997-12-16 14:41:36 +000063\refstmodindex{site}
Fred Drake18f9f531998-01-13 18:45:34 +000064\end{seealso}