blob: e4cd8756ccd61e7df06286bc7e5b21b45993dd5c [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
9\renewcommand{\indexsubitem}{(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.
Guido van Rossum470be141995-03-17 16:07:09 +000013 \code{sys.argv[0]} is the script name (it is operating system
14 dependent whether this is a full pathname or not).
15 If the command was executed using the \samp{-c} command line option
16 to the interpreter, \code{sys.argv[0]} is set to the string
17 \code{"-c"}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018 If no script name was passed to the Python interpreter,
Guido van Rossum470be141995-03-17 16:07:09 +000019 \code{sys.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
25 any other way --- \code{sys.modules.keys()} only lists the imported
26 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
37executed an \code{except} clause.'' For any stack frame, only
38information 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
47to \code{raise}, which is always a class instance if the exception
48type 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
62\code{try-finally} statement) or to call \code{sys.exc_info()} in a
63function that does not itself handle an exception.
64\end{funcdesc}
65
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066\begin{datadesc}{exc_type}
67\dataline{exc_value}
68\dataline{exc_traceback}
Guido van Rossum871cf161997-10-20 22:38:43 +000069Use of these three variables is deprecated; they contain the same
70values as returned by \code{sys.exc_info()} above. However, since
71they are global variables, they are not specific to the current
72thread, so their use is not safe in a multi-threaded program. When no
73exception is being handled, \code{sys.exc_type} is set to \code{None}
74and the other two are undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075\end{datadesc}
76
Guido van Rossum0a3c7531997-06-02 17:32:41 +000077\begin{datadesc}{exec_prefix}
78A string giving the site-specific
79directory prefix where the platform-dependent Python files are
80installed; by default, this is also \code{"/usr/local"}. This can be
81set at build time with the \code{--exec-prefix} argument to the
82\code{configure} script. Specifically, all configuration files
83(e.g. the \code{config.h} header file) are installed in the directory
84\code{sys.exec_prefix+"/lib/python\emph{VER}/config"}, and shared library
85modules are installed in
Fred Drake54820dc1997-12-15 21:56:05 +000086\code{sys.exec_prefix+"/lib/python\emph{VER}/lib-dynload"},
Guido van Rossum0a3c7531997-06-02 17:32:41 +000087where \emph{VER} is equal to \code{sys.version[:3]}.
88\end{datadesc}
89
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000090\begin{funcdesc}{exit}{n}
91 Exit from Python with numeric exit status \var{n}. This is
92 implemented by raising the \code{SystemExit} exception, so cleanup
93 actions specified by \code{finally} clauses of \code{try} statements
94 are honored, and it is possible to catch the exit attempt at an outer
95 level.
96\end{funcdesc}
97
98\begin{datadesc}{exitfunc}
99 This value is not actually defined by the module, but can be set by
100 the user (or by a program) to specify a clean-up action at program
101 exit. When set, it should be a parameterless function. This function
Guido van Rossum6b686e91995-07-07 23:00:35 +0000102 will be called when the interpreter exits in any way (except when a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000103 fatal error occurs: in that case the interpreter's internal state
104 cannot be trusted).
105\end{datadesc}
106
107\begin{datadesc}{last_type}
108\dataline{last_value}
109\dataline{last_traceback}
Guido van Rossum871cf161997-10-20 22:38:43 +0000110These three variables are not always defined; they are set when an
111exception is not handled and the interpreter prints an error message
112and a stack traceback. Their intended use is to allow an interactive
113user to import a debugger module and engage in post-mortem debugging
114without having to re-execute the command that caused the error.
115(Typical use is \code{import pdb; pdb.pm()} to enter the post-mortem
116debugger; see the chapter ``The Python Debugger'' for more
117information.)
Fred Drake54820dc1997-12-15 21:56:05 +0000118\refstmodindex{pdb}
Guido van Rossum871cf161997-10-20 22:38:43 +0000119
120The meaning of the variables is the same
121as that of the return values from \code{sys.exc_info()} above.
122(Since there is only one interactive thread, thread-safety is not a
123concern for these variables, unlike for \code{sys.exc_type} etc.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124\end{datadesc}
125
126\begin{datadesc}{modules}
127 Gives the list of modules that have already been loaded.
128 This can be manipulated to force reloading of modules and other tricks.
129\end{datadesc}
130
131\begin{datadesc}{path}
Fred Drake2b67bee1998-01-13 18:35:51 +0000132\indexiii{module}{search}{path}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000133 A list of strings that specifies the search path for modules.
134 Initialized from the environment variable \code{PYTHONPATH}, or an
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000135 installation-dependent default.
136
137The first item of this list, \code{sys.path[0]}, is the
138directory containing the script that was used to invoke the Python
139interpreter. If the script directory is not available (e.g. if the
140interpreter is invoked interactively or if the script is read from
141standard input), \code{sys.path[0]} is the empty string, which directs
142Python to search modules in the current directory first. Notice that
Fred Drake54820dc1997-12-15 21:56:05 +0000143the script directory is inserted \emph{before} the entries inserted as
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000144a result of \code{\$PYTHONPATH}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000145\end{datadesc}
146
Guido van Rossum6b686e91995-07-07 23:00:35 +0000147\begin{datadesc}{platform}
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000148This string contains a platform identifier, e.g. \code{sunos5} or
149\code{linux1}. This can be used to append platform-specific
150components to \code{sys.path}, for instance.
151\end{datadesc}
152
153\begin{datadesc}{prefix}
154A string giving the site-specific directory prefix where the platform
155independent Python files are installed; by default, this is the string
156\code{"/usr/local"}. This can be set at build time with the
157\code{--prefix} argument to the \code{configure} script. The main
158collection of Python library modules is installed in the directory
159\code{sys.prefix+"/lib/python\emph{VER}"} while the platform
160independent header files (all except \code{config.h}) are stored in
161\code{sys.prefix+"/include/python\emph{VER}"},
162where \emph{VER} is equal to \code{sys.version[:3]}.
163
Guido van Rossum6b686e91995-07-07 23:00:35 +0000164\end{datadesc}
165
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000166\begin{datadesc}{ps1}
167\dataline{ps2}
168 Strings specifying the primary and secondary prompt of the
169 interpreter. These are only defined if the interpreter is in
170 interactive mode. Their initial values in this case are
Guido van Rossumee9f8201997-11-25 21:12:27 +0000171 \code{'>>> '} and \code{'... '}. If a non-string object is assigned
172 to either variable, its \code{str()} is re-evaluated each time the
173 interpreter prepares to read a new interactive command; this can be
174 used to implement a dynamic prompt.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000175\end{datadesc}
176
Guido van Rossum9c51e411995-01-10 10:50:58 +0000177\begin{funcdesc}{setcheckinterval}{interval}
178Set the interpreter's ``check interval''. This integer value
179determines how often the interpreter checks for periodic things such
180as thread switches and signal handlers. The default is 10, meaning
181the check is performed every 10 Python virtual instructions. Setting
182it to a larger value may increase performance for programs using
Guido van Rossumf259efe1997-11-25 01:00:40 +0000183threads. Setting it to a value \code{<=} 0 checks every virtual instruction,
Guido van Rossum9c51e411995-01-10 10:50:58 +0000184maximizing responsiveness as well as overhead.
Guido van Rossum7f49b7a1995-01-12 12:38:46 +0000185\end{funcdesc}
Guido van Rossum9c51e411995-01-10 10:50:58 +0000186
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000187\begin{funcdesc}{settrace}{tracefunc}
188 Set the system's trace function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000189 Python source code debugger in Python. See section ``How It Works''
190 in the chapter on the Python Debugger.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000191\end{funcdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000192\index{trace function}
Guido van Rossum470be141995-03-17 16:07:09 +0000193\index{debugger}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000194
195\begin{funcdesc}{setprofile}{profilefunc}
196 Set the system's profile function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000197 Python source code profiler in Python. See the chapter on the
198 Python Profiler. The system's profile function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000199 is called similarly to the system's trace function (see
200 \code{sys.settrace}), but it isn't called for each executed line of
201 code (only on call and return and when an exception occurs). Also,
202 its return value is not used, so it can just return \code{None}.
203\end{funcdesc}
204\index{profile function}
Guido van Rossum470be141995-03-17 16:07:09 +0000205\index{profiler}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000206
207\begin{datadesc}{stdin}
208\dataline{stdout}
209\dataline{stderr}
210 File objects corresponding to the interpreter's standard input,
211 output and error streams. \code{sys.stdin} is used for all
212 interpreter input except for scripts but including calls to
213 \code{input()} and \code{raw_input()}. \code{sys.stdout} is used
214 for the output of \code{print} and expression statements and for the
215 prompts of \code{input()} and \code{raw_input()}. The interpreter's
216 own prompts and (almost all of) its error messages go to
217 \code{sys.stderr}. \code{sys.stdout} and \code{sys.stderr} needn't
218 be built-in file objects: any object is acceptable as long as it has
Fred Drake54820dc1997-12-15 21:56:05 +0000219 a \code{write()} method that takes a string argument. (Changing these
Guido van Rossum470be141995-03-17 16:07:09 +0000220 objects doesn't affect the standard I/O streams of processes
221 executed by \code{popen()}, \code{system()} or the \code{exec*()}
222 family of functions in the \code{os} module.)
Fred Drake54820dc1997-12-15 21:56:05 +0000223\refstmodindex{os}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000224\end{datadesc}
225
226\begin{datadesc}{tracebacklimit}
227When this variable is set to an integer value, it determines the
228maximum number of levels of traceback information printed when an
229unhandled exception occurs. The default is 1000. When set to 0 or
230less, all traceback information is suppressed and only the exception
231type and value are printed.
232\end{datadesc}
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000233
234\begin{datadesc}{version}
235A string containing the version number of the Python interpreter.
236\end{datadesc}