blob: d7e8e468219d5ef17a56ff718282b9fcf948ada2 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{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
Guido van Rossum470be141995-03-17 16:07:09 +00009
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000010\begin{datadesc}{argv}
11 The list of command line arguments passed to a Python script.
Fred Drake0fd72ee1998-03-08 05:43:51 +000012 \code{argv[0]} is the script name (it is operating system
Guido van Rossum470be141995-03-17 16:07:09 +000013 dependent whether this is a full pathname or not).
14 If the command was executed using the \samp{-c} command line option
Fred Drake0fd72ee1998-03-08 05:43:51 +000015 to the interpreter, \code{argv[0]} is set to the string
Guido van Rossum470be141995-03-17 16:07:09 +000016 \code{"-c"}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017 If no script name was passed to the Python interpreter,
Fred Drake0fd72ee1998-03-08 05:43:51 +000018 \code{argv} has zero length.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000019\end{datadesc}
20
21\begin{datadesc}{builtin_module_names}
Guido van Rossum0d2971b1997-01-06 23:01:02 +000022 A tuple of strings giving the names of all modules that are compiled
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023 into this Python interpreter. (This information is not available in
Fred Drake0fd72ee1998-03-08 05:43:51 +000024 any other way --- \code{modules.keys()} only lists the imported
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025 modules.)
26\end{datadesc}
27
Guido van Rossum3e5fe421998-06-10 17:57:44 +000028\begin{datadesc}{copyright}
29A string containing the copyright pertaining to the Python interpreter.
30\end{datadesc}
31
Guido van Rossum871cf161997-10-20 22:38:43 +000032\begin{funcdesc}{exc_info}{}
33This function returns a tuple of three values that give information
34about the exception that is currently being handled. The information
35returned is specific both to the current thread and to the current
36stack frame. If the current stack frame is not handling an exception,
37the information is taken from the calling stack frame, or its caller,
38and so on until a stack frame is found that is handling an exception.
39Here, ``handling an exception'' is defined as ``executing or having
Fred Drake0fd72ee1998-03-08 05:43:51 +000040executed an except clause.'' For any stack frame, only
Guido van Rossum871cf161997-10-20 22:38:43 +000041information about the most recently handled exception is accessible.
42
43If no exception is being handled anywhere on the stack, a tuple
44containing three \code{None} values is returned. Otherwise, the
45values returned are
46\code{(\var{type}, \var{value}, \var{traceback})}.
47Their meaning is: \var{type} gets the exception type of the exception
48being handled (a string or class object); \var{value} gets the
49exception parameter (its \dfn{associated value} or the second argument
Fred Drake0fd72ee1998-03-08 05:43:51 +000050to \keyword{raise}, which is always a class instance if the exception
Guido van Rossum871cf161997-10-20 22:38:43 +000051type is a class object); \var{traceback} gets a traceback object (see
52the Reference Manual) which encapsulates the call stack at the point
53where the exception originally occurred.
54\obindex{traceback}
55
56\strong{Warning:} assigning the \var{traceback} return value to a
57local variable in a function that is handling an exception will cause
58a circular reference. This will prevent anything referenced by a local
59variable in the same function or by the traceback from being garbage
60collected. Since most functions don't need access to the traceback,
61the best solution is to use something like
62\code{type, value = sys.exc_info()[:2]}
63to extract only the exception type and value. If you do need the
64traceback, make sure to delete it after use (best done with a
Fred Drake0fd72ee1998-03-08 05:43:51 +000065\keyword{try} ... \keyword{finally} statement) or to call
66\function{exc_info()} in a function that does not itself handle an
67exception.
Guido van Rossum871cf161997-10-20 22:38:43 +000068\end{funcdesc}
69
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000070\begin{datadesc}{exc_type}
71\dataline{exc_value}
72\dataline{exc_traceback}
Fred Drake0fd72ee1998-03-08 05:43:51 +000073\deprecated {1.5}
74 {Use \function{exc_info()} instead.}
75Since they are global variables, they are not specific to the current
Guido van Rossum871cf161997-10-20 22:38:43 +000076thread, so their use is not safe in a multi-threaded program. When no
Fred Drake0fd72ee1998-03-08 05:43:51 +000077exception is being handled, \code{exc_type} is set to \code{None} and
78the other two are undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079\end{datadesc}
80
Guido van Rossum0a3c7531997-06-02 17:32:41 +000081\begin{datadesc}{exec_prefix}
82A string giving the site-specific
83directory prefix where the platform-dependent Python files are
84installed; by default, this is also \code{"/usr/local"}. This can be
Fred Drakef76abb51998-03-27 00:37:40 +000085set at build time with the \code{-}\code{-exec-prefix} argument to the
Fred Drake0fd72ee1998-03-08 05:43:51 +000086\program{configure} script. Specifically, all configuration files
87(e.g. the \file{config.h} header file) are installed in the directory
88\code{exec_prefix + "/lib/python\var{version}/config"}, and shared library
Guido van Rossum0a3c7531997-06-02 17:32:41 +000089modules are installed in
Fred Drake0fd72ee1998-03-08 05:43:51 +000090\code{exec_prefix + "/lib/python\var{version}/lib-dynload"},
91where \var{version} is equal to \code{version[:3]}.
Guido van Rossum0a3c7531997-06-02 17:32:41 +000092\end{datadesc}
93
Guido van Rossum3e5fe421998-06-10 17:57:44 +000094\begin{datadesc}{executable}
95A string giving the name of the executable binary for the Python
96interpreter, on systems where this makes sense.
97\end{datadesc}
98
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000099\begin{funcdesc}{exit}{n}
100 Exit from Python with numeric exit status \var{n}. This is
Fred Drake0fd72ee1998-03-08 05:43:51 +0000101 implemented by raising the \exception{SystemExit} exception, so cleanup
102 actions specified by finally clauses of \keyword{try} statements
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000103 are honored, and it is possible to catch the exit attempt at an outer
104 level.
105\end{funcdesc}
106
107\begin{datadesc}{exitfunc}
108 This value is not actually defined by the module, but can be set by
109 the user (or by a program) to specify a clean-up action at program
110 exit. When set, it should be a parameterless function. This function
Guido van Rossum6b686e91995-07-07 23:00:35 +0000111 will be called when the interpreter exits in any way (except when a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000112 fatal error occurs: in that case the interpreter's internal state
113 cannot be trusted).
114\end{datadesc}
115
Guido van Rossum6e91c6a1998-02-07 21:17:05 +0000116\begin{funcdesc}{getrefcount}{object}
117Return the reference count of the \var{object}. The count returned is
118generally one higher than you might expect, because it includes the
119(temporary) reference as an argument to \code{getrefcount()}.
120\end{funcdesc}
121
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000122\begin{datadesc}{last_type}
123\dataline{last_value}
124\dataline{last_traceback}
Guido van Rossum871cf161997-10-20 22:38:43 +0000125These three variables are not always defined; they are set when an
126exception is not handled and the interpreter prints an error message
127and a stack traceback. Their intended use is to allow an interactive
128user to import a debugger module and engage in post-mortem debugging
129without having to re-execute the command that caused the error.
Fred Drake0fd72ee1998-03-08 05:43:51 +0000130(Typical use is \samp{import pdb; pdb.pm()} to enter the post-mortem
Guido van Rossum871cf161997-10-20 22:38:43 +0000131debugger; see the chapter ``The Python Debugger'' for more
132information.)
Fred Drake54820dc1997-12-15 21:56:05 +0000133\refstmodindex{pdb}
Guido van Rossum871cf161997-10-20 22:38:43 +0000134
135The meaning of the variables is the same
Fred Drake0fd72ee1998-03-08 05:43:51 +0000136as that of the return values from \function{exc_info()} above.
Guido van Rossum871cf161997-10-20 22:38:43 +0000137(Since there is only one interactive thread, thread-safety is not a
Fred Drake0fd72ee1998-03-08 05:43:51 +0000138concern for these variables, unlike for \code{exc_type} etc.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000139\end{datadesc}
140
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000141\begin{datadesc}{maxint}
142The largest positive integer supported by Python's regular integer
143type. This is at least 2**31-1. The largest negative integer is
144\code{-maxint-1} -- the asymmetry results from the use of 2's
145complement binary arithmetic.
146\end{datadesc}
147
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000148\begin{datadesc}{modules}
Fred Drake0fd72ee1998-03-08 05:43:51 +0000149 This is a dictionary that maps module names to modules which have
150 already been loaded. This can be manipulated to force reloading of
151 modules and other tricks. Note that removing a module from this
152 dictionary is \emph{not} the same as calling
153 \function{reload()}\bifuncindex{reload} on the corresponding module
154 object.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000155\end{datadesc}
156
157\begin{datadesc}{path}
Fred Drake2b67bee1998-01-13 18:35:51 +0000158\indexiii{module}{search}{path}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000159 A list of strings that specifies the search path for modules.
Fred Drake0fd72ee1998-03-08 05:43:51 +0000160 Initialized from the environment variable \code{\$PYTHONPATH}, or an
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000161 installation-dependent default.
162
Fred Drake0fd72ee1998-03-08 05:43:51 +0000163The first item of this list, \code{path[0]}, is the
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000164directory containing the script that was used to invoke the Python
165interpreter. If the script directory is not available (e.g. if the
166interpreter is invoked interactively or if the script is read from
Fred Drake0fd72ee1998-03-08 05:43:51 +0000167standard input), \code{path[0]} is the empty string, which directs
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000168Python to search modules in the current directory first. Notice that
Fred Drake54820dc1997-12-15 21:56:05 +0000169the script directory is inserted \emph{before} the entries inserted as
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000170a result of \code{\$PYTHONPATH}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000171\end{datadesc}
172
Guido van Rossum6b686e91995-07-07 23:00:35 +0000173\begin{datadesc}{platform}
Fred Drake0fd72ee1998-03-08 05:43:51 +0000174This string contains a platform identifier, e.g. \code{'sunos5'} or
175\code{'linux1'}. This can be used to append platform-specific
176components to \code{path}, for instance.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000177\end{datadesc}
178
179\begin{datadesc}{prefix}
180A string giving the site-specific directory prefix where the platform
181independent Python files are installed; by default, this is the string
182\code{"/usr/local"}. This can be set at build time with the
Fred Drakef76abb51998-03-27 00:37:40 +0000183\code{-}\code{-prefix} argument to the \program{configure} script. The main
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000184collection of Python library modules is installed in the directory
Fred Drake0fd72ee1998-03-08 05:43:51 +0000185\code{prefix + "/lib/python\var{version}"} while the platform
186independent header files (all except \file{config.h}) are stored in
187\code{prefix + "/include/python\var{version}"},
188where \var{version} is equal to \code{version[:3]}.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000189
Guido van Rossum6b686e91995-07-07 23:00:35 +0000190\end{datadesc}
191
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000192\begin{datadesc}{ps1}
193\dataline{ps2}
Fred Drakee6cedb31998-04-03 07:05:16 +0000194\index{interpreter prompts}
195\index{prompts, interpreter}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000196 Strings specifying the primary and secondary prompt of the
197 interpreter. These are only defined if the interpreter is in
198 interactive mode. Their initial values in this case are
Guido van Rossumee9f8201997-11-25 21:12:27 +0000199 \code{'>>> '} and \code{'... '}. If a non-string object is assigned
Fred Drake0fd72ee1998-03-08 05:43:51 +0000200 to either variable, its \function{str()} is re-evaluated each time
201 the interpreter prepares to read a new interactive command; this can
202 be used to implement a dynamic prompt.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000203\end{datadesc}
204
Guido van Rossum9c51e411995-01-10 10:50:58 +0000205\begin{funcdesc}{setcheckinterval}{interval}
206Set the interpreter's ``check interval''. This integer value
207determines how often the interpreter checks for periodic things such
Fred Drake0fd72ee1998-03-08 05:43:51 +0000208as thread switches and signal handlers. The default is \code{10}, meaning
Guido van Rossum9c51e411995-01-10 10:50:58 +0000209the check is performed every 10 Python virtual instructions. Setting
210it to a larger value may increase performance for programs using
Guido van Rossumf259efe1997-11-25 01:00:40 +0000211threads. Setting it to a value \code{<=} 0 checks every virtual instruction,
Guido van Rossum9c51e411995-01-10 10:50:58 +0000212maximizing responsiveness as well as overhead.
Guido van Rossum7f49b7a1995-01-12 12:38:46 +0000213\end{funcdesc}
Guido van Rossum9c51e411995-01-10 10:50:58 +0000214
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000215\begin{funcdesc}{setprofile}{profilefunc}
216 Set the system's profile function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000217 Python source code profiler in Python. See the chapter on the
218 Python Profiler. The system's profile function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000219 is called similarly to the system's trace function (see
Fred Drake0fd72ee1998-03-08 05:43:51 +0000220 \function{settrace()}), but it isn't called for each executed line of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000221 code (only on call and return and when an exception occurs). Also,
222 its return value is not used, so it can just return \code{None}.
223\end{funcdesc}
224\index{profile function}
Guido van Rossum470be141995-03-17 16:07:09 +0000225\index{profiler}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000226
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000227\begin{funcdesc}{settrace}{tracefunc}
228 Set the system's trace function, which allows you to implement a
229 Python source code debugger in Python. See section ``How It Works''
230 in the chapter on the Python Debugger.
231\end{funcdesc}
232\index{trace function}
233\index{debugger}
234
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000235\begin{datadesc}{stdin}
236\dataline{stdout}
237\dataline{stderr}
238 File objects corresponding to the interpreter's standard input,
Fred Drake0fd72ee1998-03-08 05:43:51 +0000239 output and error streams. \code{stdin} is used for all
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000240 interpreter input except for scripts but including calls to
Fred Drake0fd72ee1998-03-08 05:43:51 +0000241 \function{input()}\bifuncindex{input} and
242 \function{raw_input()}\bifuncindex{raw_input}. \code{stdout} is used
243 for the output of \keyword{print} and expression statements and for the
244 prompts of \function{input()} and \function{raw_input()}. The interpreter's
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000245 own prompts and (almost all of) its error messages go to
Fred Drake0fd72ee1998-03-08 05:43:51 +0000246 \code{stderr}. \code{stdout} and \code{stderr} needn't
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000247 be built-in file objects: any object is acceptable as long as it has
Fred Drake0fd72ee1998-03-08 05:43:51 +0000248 a \method{write()} method that takes a string argument. (Changing these
Guido van Rossum470be141995-03-17 16:07:09 +0000249 objects doesn't affect the standard I/O streams of processes
Fred Drake0fd72ee1998-03-08 05:43:51 +0000250 executed by \function{os.popen()}, \function{os.system()} or the
251 \function{exec*()} family of functions in the \module{os} module.)
Fred Drake54820dc1997-12-15 21:56:05 +0000252\refstmodindex{os}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000253\end{datadesc}
254
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000255\begin{datadesc}{__stdin__}
256\dataline{__stdout__}
257\dataline{__stderr__}
258These objects contain the original values of \code{stdin},
259\code{stderr} and \code{stdout} at the start of the program. They are
260used during finalization, and could be useful to restore the actual
261files to known working file objects in case they have been overwritten
262with a broken object.
263\end{datadesc}
264
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000265\begin{datadesc}{tracebacklimit}
266When this variable is set to an integer value, it determines the
267maximum number of levels of traceback information printed when an
Fred Drake0fd72ee1998-03-08 05:43:51 +0000268unhandled exception occurs. The default is \code{1000}. When set to
2690 or less, all traceback information is suppressed and only the
270exception type and value are printed.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000271\end{datadesc}
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000272
273\begin{datadesc}{version}
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000274A string containing the version number of the Python interpreter.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000275\end{datadesc}