blob: 2ed7f62822fbf6ee462f1d7432677775f85070ca [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
Fred Drake7be8fcb1998-03-12 06:47:48 +00009specified in the \envvar{PYTHONSTARTUP} environment variable if it
10exists).
Guido van Rossum36764b81997-08-30 20:02:25 +000011
12However, some programs or sites may find it convenient to allow users
13to have a standard customization file, which gets run when a program
14requests it. This module implements such a mechanism. A program
15that wishes to use the mechanism must execute the statement
16
Fred Drake8d0ff311998-01-09 22:27:55 +000017\begin{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000018import user
Fred Drake8d0ff311998-01-09 22:27:55 +000019\end{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000020
Fred Drake8fab8cf1998-03-08 07:14:20 +000021The \module{user} module looks for a file \file{.pythonrc.py} in the user's
Guido van Rossum36764b81997-08-30 20:02:25 +000022home directory and if it can be opened, exececutes it (using
Fred Drake8fab8cf1998-03-08 07:14:20 +000023\function{execfile()}\bifuncindex{execfile}) in its own (i.e. the
24module \module{user}'s) global namespace. Errors during this phase
25are not caught; that's up to the program that imports the
26\module{user} module, if it wishes. The home directory is assumed to
Fred Drake7be8fcb1998-03-12 06:47:48 +000027be named by the \envvar{HOME} environment variable; if this is not set,
Fred Drake8fab8cf1998-03-08 07:14:20 +000028the current directory is used.
Guido van Rossum36764b81997-08-30 20:02:25 +000029
30The user's \file{.pythonrc.py} could conceivably test for
31\code{sys.version} if it wishes to do different things depending on
32the Python version.
33
34A warning to users: be very conservative in what you place in your
35\file{.pythonrc.py} file. Since you don't know which programs will
36use it, changing the behavior of standard modules or functions is
37generally not a good idea.
38
39A suggestion for programmers who wish to use this mechanism: a simple
40way to let users specify options for your package is to have them
Fred Drake9d2f7d41997-12-16 14:43:37 +000041define variables in their \file{.pythonrc.py} file that you test in
Fred Drake8fab8cf1998-03-08 07:14:20 +000042your module. For example, a module \module{spam} that has a verbosity
Guido van Rossum36764b81997-08-30 20:02:25 +000043level can look for a variable \code{user.spam_verbose}, as follows:
44
Fred Drake19479911998-02-13 06:58:54 +000045\begin{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000046import user
47try:
48 verbose = user.spam_verbose # user's verbosity preference
49except AttributeError:
50 verbose = 0 # default verbosity
Fred Drake19479911998-02-13 06:58:54 +000051\end{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000052
53Programs with extensive customization needs are better off reading a
54program-specific customization file.
55
56Programs with security or privacy concerns should \emph{not} import
57this module; a user can easily break into a a program by placing
58arbitrary code in the \file{.pythonrc.py} file.
59
60Modules for general use should \emph{not} import this module; it may
61interfere with the operation of the importing program.
62
Fred Drake18f9f531998-01-13 18:45:34 +000063\begin{seealso}
64\seemodule{site}{site-wide customization mechanism}
Fred Drake54932051997-12-16 14:41:36 +000065\refstmodindex{site}
Fred Drake18f9f531998-01-13 18:45:34 +000066\end{seealso}