blob: 932520de01b8a6967a6e4e9e2f1004c87becb87d [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{sys}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-sys}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003
4\bimodindex{sys}
5This module provides access to some variables used or maintained by the
6interpreter and to functions that interact strongly with the interpreter.
7It is always available.
8
Fred Drake19479911998-02-13 06:58:54 +00009\setindexsubitem{(in module sys)}
Guido van Rossum470be141995-03-17 16:07:09 +000010
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011\begin{datadesc}{argv}
12 The list of command line arguments passed to a Python script.
Fred Drake0fd72ee1998-03-08 05:43:51 +000013 \code{argv[0]} is the script name (it is operating system
Guido van Rossum470be141995-03-17 16:07:09 +000014 dependent whether this is a full pathname or not).
15 If the command was executed using the \samp{-c} command line option
Fred Drake0fd72ee1998-03-08 05:43:51 +000016 to the interpreter, \code{argv[0]} is set to the string
Guido van Rossum470be141995-03-17 16:07:09 +000017 \code{"-c"}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018 If no script name was passed to the Python interpreter,
Fred Drake0fd72ee1998-03-08 05:43:51 +000019 \code{argv} has zero length.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020\end{datadesc}
21
22\begin{datadesc}{builtin_module_names}
Guido van Rossum0d2971b1997-01-06 23:01:02 +000023 A tuple of strings giving the names of all modules that are compiled
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024 into this Python interpreter. (This information is not available in
Fred Drake0fd72ee1998-03-08 05:43:51 +000025 any other way --- \code{modules.keys()} only lists the imported
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026 modules.)
27\end{datadesc}
28
Guido van Rossum871cf161997-10-20 22:38:43 +000029\begin{funcdesc}{exc_info}{}
30This function returns a tuple of three values that give information
31about the exception that is currently being handled. The information
32returned is specific both to the current thread and to the current
33stack frame. If the current stack frame is not handling an exception,
34the information is taken from the calling stack frame, or its caller,
35and so on until a stack frame is found that is handling an exception.
36Here, ``handling an exception'' is defined as ``executing or having
Fred Drake0fd72ee1998-03-08 05:43:51 +000037executed an except clause.'' For any stack frame, only
Guido van Rossum871cf161997-10-20 22:38:43 +000038information about the most recently handled exception is accessible.
39
40If no exception is being handled anywhere on the stack, a tuple
41containing three \code{None} values is returned. Otherwise, the
42values returned are
43\code{(\var{type}, \var{value}, \var{traceback})}.
44Their meaning is: \var{type} gets the exception type of the exception
45being handled (a string or class object); \var{value} gets the
46exception parameter (its \dfn{associated value} or the second argument
Fred Drake0fd72ee1998-03-08 05:43:51 +000047to \keyword{raise}, which is always a class instance if the exception
Guido van Rossum871cf161997-10-20 22:38:43 +000048type is a class object); \var{traceback} gets a traceback object (see
49the Reference Manual) which encapsulates the call stack at the point
50where the exception originally occurred.
51\obindex{traceback}
52
53\strong{Warning:} assigning the \var{traceback} return value to a
54local variable in a function that is handling an exception will cause
55a circular reference. This will prevent anything referenced by a local
56variable in the same function or by the traceback from being garbage
57collected. Since most functions don't need access to the traceback,
58the best solution is to use something like
59\code{type, value = sys.exc_info()[:2]}
60to extract only the exception type and value. If you do need the
61traceback, make sure to delete it after use (best done with a
Fred Drake0fd72ee1998-03-08 05:43:51 +000062\keyword{try} ... \keyword{finally} statement) or to call
63\function{exc_info()} in a function that does not itself handle an
64exception.
Guido van Rossum871cf161997-10-20 22:38:43 +000065\end{funcdesc}
66
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000067\begin{datadesc}{exc_type}
68\dataline{exc_value}
69\dataline{exc_traceback}
Fred Drake0fd72ee1998-03-08 05:43:51 +000070\deprecated {1.5}
71 {Use \function{exc_info()} instead.}
72Since they are global variables, they are not specific to the current
Guido van Rossum871cf161997-10-20 22:38:43 +000073thread, so their use is not safe in a multi-threaded program. When no
Fred Drake0fd72ee1998-03-08 05:43:51 +000074exception is being handled, \code{exc_type} is set to \code{None} and
75the other two are undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076\end{datadesc}
77
Guido van Rossum0a3c7531997-06-02 17:32:41 +000078\begin{datadesc}{exec_prefix}
79A string giving the site-specific
80directory prefix where the platform-dependent Python files are
81installed; by default, this is also \code{"/usr/local"}. This can be
Fred Drakef76abb51998-03-27 00:37:40 +000082set at build time with the \code{-}\code{-exec-prefix} argument to the
Fred Drake0fd72ee1998-03-08 05:43:51 +000083\program{configure} script. Specifically, all configuration files
84(e.g. the \file{config.h} header file) are installed in the directory
85\code{exec_prefix + "/lib/python\var{version}/config"}, and shared library
Guido van Rossum0a3c7531997-06-02 17:32:41 +000086modules are installed in
Fred Drake0fd72ee1998-03-08 05:43:51 +000087\code{exec_prefix + "/lib/python\var{version}/lib-dynload"},
88where \var{version} is equal to \code{version[:3]}.
Guido van Rossum0a3c7531997-06-02 17:32:41 +000089\end{datadesc}
90
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000091\begin{funcdesc}{exit}{n}
92 Exit from Python with numeric exit status \var{n}. This is
Fred Drake0fd72ee1998-03-08 05:43:51 +000093 implemented by raising the \exception{SystemExit} exception, so cleanup
94 actions specified by finally clauses of \keyword{try} statements
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000095 are honored, and it is possible to catch the exit attempt at an outer
96 level.
97\end{funcdesc}
98
99\begin{datadesc}{exitfunc}
100 This value is not actually defined by the module, but can be set by
101 the user (or by a program) to specify a clean-up action at program
102 exit. When set, it should be a parameterless function. This function
Guido van Rossum6b686e91995-07-07 23:00:35 +0000103 will be called when the interpreter exits in any way (except when a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000104 fatal error occurs: in that case the interpreter's internal state
105 cannot be trusted).
106\end{datadesc}
107
Guido van Rossum6e91c6a1998-02-07 21:17:05 +0000108\begin{funcdesc}{getrefcount}{object}
109Return the reference count of the \var{object}. The count returned is
110generally one higher than you might expect, because it includes the
111(temporary) reference as an argument to \code{getrefcount()}.
112\end{funcdesc}
113
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000114\begin{datadesc}{last_type}
115\dataline{last_value}
116\dataline{last_traceback}
Guido van Rossum871cf161997-10-20 22:38:43 +0000117These three variables are not always defined; they are set when an
118exception is not handled and the interpreter prints an error message
119and a stack traceback. Their intended use is to allow an interactive
120user to import a debugger module and engage in post-mortem debugging
121without having to re-execute the command that caused the error.
Fred Drake0fd72ee1998-03-08 05:43:51 +0000122(Typical use is \samp{import pdb; pdb.pm()} to enter the post-mortem
Guido van Rossum871cf161997-10-20 22:38:43 +0000123debugger; see the chapter ``The Python Debugger'' for more
124information.)
Fred Drake54820dc1997-12-15 21:56:05 +0000125\refstmodindex{pdb}
Guido van Rossum871cf161997-10-20 22:38:43 +0000126
127The meaning of the variables is the same
Fred Drake0fd72ee1998-03-08 05:43:51 +0000128as that of the return values from \function{exc_info()} above.
Guido van Rossum871cf161997-10-20 22:38:43 +0000129(Since there is only one interactive thread, thread-safety is not a
Fred Drake0fd72ee1998-03-08 05:43:51 +0000130concern for these variables, unlike for \code{exc_type} etc.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000131\end{datadesc}
132
133\begin{datadesc}{modules}
Fred Drake0fd72ee1998-03-08 05:43:51 +0000134 This is a dictionary that maps module names to modules which have
135 already been loaded. This can be manipulated to force reloading of
136 modules and other tricks. Note that removing a module from this
137 dictionary is \emph{not} the same as calling
138 \function{reload()}\bifuncindex{reload} on the corresponding module
139 object.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000140\end{datadesc}
141
142\begin{datadesc}{path}
Fred Drake2b67bee1998-01-13 18:35:51 +0000143\indexiii{module}{search}{path}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144 A list of strings that specifies the search path for modules.
Fred Drake0fd72ee1998-03-08 05:43:51 +0000145 Initialized from the environment variable \code{\$PYTHONPATH}, or an
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000146 installation-dependent default.
147
Fred Drake0fd72ee1998-03-08 05:43:51 +0000148The first item of this list, \code{path[0]}, is the
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000149directory containing the script that was used to invoke the Python
150interpreter. If the script directory is not available (e.g. if the
151interpreter is invoked interactively or if the script is read from
Fred Drake0fd72ee1998-03-08 05:43:51 +0000152standard input), \code{path[0]} is the empty string, which directs
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000153Python to search modules in the current directory first. Notice that
Fred Drake54820dc1997-12-15 21:56:05 +0000154the script directory is inserted \emph{before} the entries inserted as
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000155a result of \code{\$PYTHONPATH}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000156\end{datadesc}
157
Guido van Rossum6b686e91995-07-07 23:00:35 +0000158\begin{datadesc}{platform}
Fred Drake0fd72ee1998-03-08 05:43:51 +0000159This string contains a platform identifier, e.g. \code{'sunos5'} or
160\code{'linux1'}. This can be used to append platform-specific
161components to \code{path}, for instance.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000162\end{datadesc}
163
164\begin{datadesc}{prefix}
165A string giving the site-specific directory prefix where the platform
166independent Python files are installed; by default, this is the string
167\code{"/usr/local"}. This can be set at build time with the
Fred Drakef76abb51998-03-27 00:37:40 +0000168\code{-}\code{-prefix} argument to the \program{configure} script. The main
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000169collection of Python library modules is installed in the directory
Fred Drake0fd72ee1998-03-08 05:43:51 +0000170\code{prefix + "/lib/python\var{version}"} while the platform
171independent header files (all except \file{config.h}) are stored in
172\code{prefix + "/include/python\var{version}"},
173where \var{version} is equal to \code{version[:3]}.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000174
Guido van Rossum6b686e91995-07-07 23:00:35 +0000175\end{datadesc}
176
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000177\begin{datadesc}{ps1}
178\dataline{ps2}
179 Strings specifying the primary and secondary prompt of the
180 interpreter. These are only defined if the interpreter is in
181 interactive mode. Their initial values in this case are
Guido van Rossumee9f8201997-11-25 21:12:27 +0000182 \code{'>>> '} and \code{'... '}. If a non-string object is assigned
Fred Drake0fd72ee1998-03-08 05:43:51 +0000183 to either variable, its \function{str()} is re-evaluated each time
184 the interpreter prepares to read a new interactive command; this can
185 be used to implement a dynamic prompt.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000186\end{datadesc}
187
Guido van Rossum9c51e411995-01-10 10:50:58 +0000188\begin{funcdesc}{setcheckinterval}{interval}
189Set the interpreter's ``check interval''. This integer value
190determines how often the interpreter checks for periodic things such
Fred Drake0fd72ee1998-03-08 05:43:51 +0000191as thread switches and signal handlers. The default is \code{10}, meaning
Guido van Rossum9c51e411995-01-10 10:50:58 +0000192the check is performed every 10 Python virtual instructions. Setting
193it to a larger value may increase performance for programs using
Guido van Rossumf259efe1997-11-25 01:00:40 +0000194threads. Setting it to a value \code{<=} 0 checks every virtual instruction,
Guido van Rossum9c51e411995-01-10 10:50:58 +0000195maximizing responsiveness as well as overhead.
Guido van Rossum7f49b7a1995-01-12 12:38:46 +0000196\end{funcdesc}
Guido van Rossum9c51e411995-01-10 10:50:58 +0000197
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000198\begin{funcdesc}{settrace}{tracefunc}
199 Set the system's trace function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000200 Python source code debugger in Python. See section ``How It Works''
201 in the chapter on the Python Debugger.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000202\end{funcdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000203\index{trace function}
Guido van Rossum470be141995-03-17 16:07:09 +0000204\index{debugger}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000205
206\begin{funcdesc}{setprofile}{profilefunc}
207 Set the system's profile function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000208 Python source code profiler in Python. See the chapter on the
209 Python Profiler. The system's profile function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000210 is called similarly to the system's trace function (see
Fred Drake0fd72ee1998-03-08 05:43:51 +0000211 \function{settrace()}), but it isn't called for each executed line of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000212 code (only on call and return and when an exception occurs). Also,
213 its return value is not used, so it can just return \code{None}.
214\end{funcdesc}
215\index{profile function}
Guido van Rossum470be141995-03-17 16:07:09 +0000216\index{profiler}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000217
218\begin{datadesc}{stdin}
219\dataline{stdout}
220\dataline{stderr}
221 File objects corresponding to the interpreter's standard input,
Fred Drake0fd72ee1998-03-08 05:43:51 +0000222 output and error streams. \code{stdin} is used for all
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000223 interpreter input except for scripts but including calls to
Fred Drake0fd72ee1998-03-08 05:43:51 +0000224 \function{input()}\bifuncindex{input} and
225 \function{raw_input()}\bifuncindex{raw_input}. \code{stdout} is used
226 for the output of \keyword{print} and expression statements and for the
227 prompts of \function{input()} and \function{raw_input()}. The interpreter's
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000228 own prompts and (almost all of) its error messages go to
Fred Drake0fd72ee1998-03-08 05:43:51 +0000229 \code{stderr}. \code{stdout} and \code{stderr} needn't
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000230 be built-in file objects: any object is acceptable as long as it has
Fred Drake0fd72ee1998-03-08 05:43:51 +0000231 a \method{write()} method that takes a string argument. (Changing these
Guido van Rossum470be141995-03-17 16:07:09 +0000232 objects doesn't affect the standard I/O streams of processes
Fred Drake0fd72ee1998-03-08 05:43:51 +0000233 executed by \function{os.popen()}, \function{os.system()} or the
234 \function{exec*()} family of functions in the \module{os} module.)
Fred Drake54820dc1997-12-15 21:56:05 +0000235\refstmodindex{os}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000236\end{datadesc}
237
238\begin{datadesc}{tracebacklimit}
239When this variable is set to an integer value, it determines the
240maximum number of levels of traceback information printed when an
Fred Drake0fd72ee1998-03-08 05:43:51 +0000241unhandled exception occurs. The default is \code{1000}. When set to
2420 or less, all traceback information is suppressed and only the
243exception type and value are printed.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000244\end{datadesc}
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000245
246\begin{datadesc}{version}
247A string containing the version number of the Python interpreter.
248\end{datadesc}