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