blob: 0d0ecb6809131a88af54b953ebc82030258c569d [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{user} ---
Fred Drakebbac4321999-02-20 00:14:17 +00002 User-specific configuration hook}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakebbac4321999-02-20 00:14:17 +00004\declaremodule{standard}{user}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{A standard way to reference user-specific modules.}
6
Fred Drakebbac4321999-02-20 00:14:17 +00007
Fred Drake18f9f531998-01-13 18:45:34 +00008\indexii{.pythonrc.py}{file}
9\indexiii{user}{configuration}{file}
Guido van Rossum36764b81997-08-30 20:02:25 +000010
11As a policy, Python doesn't run user-specified code on startup of
12Python programs. (Only interactive sessions execute the script
Fred Drake7be8fcb1998-03-12 06:47:48 +000013specified in the \envvar{PYTHONSTARTUP} environment variable if it
14exists).
Guido van Rossum36764b81997-08-30 20:02:25 +000015
16However, some programs or sites may find it convenient to allow users
17to have a standard customization file, which gets run when a program
18requests it. This module implements such a mechanism. A program
19that wishes to use the mechanism must execute the statement
20
Fred Drake8d0ff311998-01-09 22:27:55 +000021\begin{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000022import user
Fred Drake8d0ff311998-01-09 22:27:55 +000023\end{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000024
Fred Drake8fab8cf1998-03-08 07:14:20 +000025The \module{user} module looks for a file \file{.pythonrc.py} in the user's
Thomas Woutersf8316632000-07-16 19:01:10 +000026home directory and if it can be opened, executes it (using
Fred Drake907e76b2001-07-06 20:30:11 +000027\function{execfile()}\bifuncindex{execfile}) in its own (the
Fred Drake8fab8cf1998-03-08 07:14:20 +000028module \module{user}'s) global namespace. Errors during this phase
29are not caught; that's up to the program that imports the
30\module{user} module, if it wishes. The home directory is assumed to
Fred Drake7be8fcb1998-03-12 06:47:48 +000031be named by the \envvar{HOME} environment variable; if this is not set,
Fred Drake8fab8cf1998-03-08 07:14:20 +000032the current directory is used.
Guido van Rossum36764b81997-08-30 20:02:25 +000033
34The user's \file{.pythonrc.py} could conceivably test for
35\code{sys.version} if it wishes to do different things depending on
36the Python version.
37
38A warning to users: be very conservative in what you place in your
39\file{.pythonrc.py} file. Since you don't know which programs will
40use it, changing the behavior of standard modules or functions is
41generally not a good idea.
42
43A suggestion for programmers who wish to use this mechanism: a simple
44way to let users specify options for your package is to have them
Fred Drake9d2f7d41997-12-16 14:43:37 +000045define variables in their \file{.pythonrc.py} file that you test in
Fred Drake8fab8cf1998-03-08 07:14:20 +000046your module. For example, a module \module{spam} that has a verbosity
Guido van Rossum36764b81997-08-30 20:02:25 +000047level can look for a variable \code{user.spam_verbose}, as follows:
48
Fred Drake19479911998-02-13 06:58:54 +000049\begin{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000050import user
51try:
52 verbose = user.spam_verbose # user's verbosity preference
53except AttributeError:
54 verbose = 0 # default verbosity
Fred Drake19479911998-02-13 06:58:54 +000055\end{verbatim}
Guido van Rossum36764b81997-08-30 20:02:25 +000056
57Programs with extensive customization needs are better off reading a
58program-specific customization file.
59
60Programs with security or privacy concerns should \emph{not} import
Fred Draked8a41e61999-02-19 17:54:10 +000061this module; a user can easily break into a program by placing
Guido van Rossum36764b81997-08-30 20:02:25 +000062arbitrary code in the \file{.pythonrc.py} file.
63
64Modules for general use should \emph{not} import this module; it may
65interfere with the operation of the importing program.
66
Fred Drake18f9f531998-01-13 18:45:34 +000067\begin{seealso}
Fred Drakeba0a9892000-10-18 17:43:06 +000068 \seemodule{site}{Site-wide customization mechanism.}
Fred Drake18f9f531998-01-13 18:45:34 +000069\end{seealso}