blob: 2ea4c379ac6d14bbebab6125256a852641894ce5 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{sys}}
2
3\bimodindex{sys}
4This module provides access to some variables used or maintained by the
5interpreter and to functions that interact strongly with the interpreter.
6It is always available.
7
8\renewcommand{\indexsubitem}{(in module sys)}
9\begin{datadesc}{argv}
10 The list of command line arguments passed to a Python script.
11 \code{sys.argv[0]} is the script name.
12 If no script name was passed to the Python interpreter,
13 \code{sys.argv} is empty.
14\end{datadesc}
15
16\begin{datadesc}{builtin_module_names}
17 A list of strings giving the names of all modules that are compiled
18 into this Python interpreter. (This information is not available in
19 any other way --- \code{sys.modules.keys()} only lists the imported
20 modules.)
21\end{datadesc}
22
Guido van Rossum16cd7f91994-10-06 10:29:26 +000023\begin{datadesc}{check_interval}
24When this variable is set to an integer value, it determines how often
25the interpreter checks for periodic things such as thread switches and
26signal handlers. The default is 10, meaning the check is performed
27every 10 Python virtual instructions. Setting this to a large value
28may increase performance for programs using threads. Setting it to a
29value <= 0 checks every virtual instruction, maximizing responsiveness
30as well as overhead.
Sjoerd Mullender9fd48ab1994-10-06 15:18:03 +000031\end{datadesc}
Guido van Rossum16cd7f91994-10-06 10:29:26 +000032
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033\begin{datadesc}{exc_type}
34\dataline{exc_value}
35\dataline{exc_traceback}
36 These three variables are not always defined; they are set when an
37 exception handler (an \code{except} clause of a \code{try} statement) is
38 invoked. Their meaning is: \code{exc_type} gets the exception type of
39 the exception being handled; \code{exc_value} gets the exception
40 parameter (its \dfn{associated value} or the second argument to
41 \code{raise}); \code{exc_traceback} gets a traceback object which
42 encapsulates the call stack at the point where the exception
43 originally occurred.
44\end{datadesc}
45
46\begin{funcdesc}{exit}{n}
47 Exit from Python with numeric exit status \var{n}. This is
48 implemented by raising the \code{SystemExit} exception, so cleanup
49 actions specified by \code{finally} clauses of \code{try} statements
50 are honored, and it is possible to catch the exit attempt at an outer
51 level.
52\end{funcdesc}
53
54\begin{datadesc}{exitfunc}
55 This value is not actually defined by the module, but can be set by
56 the user (or by a program) to specify a clean-up action at program
57 exit. When set, it should be a parameterless function. This function
58 will be called when the interpreter exits in any way (but not when a
59 fatal error occurs: in that case the interpreter's internal state
60 cannot be trusted).
61\end{datadesc}
62
63\begin{datadesc}{last_type}
64\dataline{last_value}
65\dataline{last_traceback}
66 These three variables are not always defined; they are set when an
67 exception is not handled and the interpreter prints an error message
68 and a stack traceback. Their intended use is to allow an interactive
69 user to import a debugger module and engage in post-mortem debugging
Guido van Rossum16d6e711994-08-08 12:30:22 +000070 without having to re-execute the command that caused the error (which
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000071 may be hard to reproduce). The meaning of the variables is the same
72 as that of \code{exc_type}, \code{exc_value} and \code{exc_tracaback},
73 respectively.
74\end{datadesc}
75
76\begin{datadesc}{modules}
77 Gives the list of modules that have already been loaded.
78 This can be manipulated to force reloading of modules and other tricks.
79\end{datadesc}
80
81\begin{datadesc}{path}
82 A list of strings that specifies the search path for modules.
83 Initialized from the environment variable \code{PYTHONPATH}, or an
84 installation-dependent default.
85\end{datadesc}
86
87\begin{datadesc}{ps1}
88\dataline{ps2}
89 Strings specifying the primary and secondary prompt of the
90 interpreter. These are only defined if the interpreter is in
91 interactive mode. Their initial values in this case are
92 \code{'>>> '} and \code{'... '}.
93\end{datadesc}
94
95\begin{funcdesc}{settrace}{tracefunc}
96 Set the system's trace function, which allows you to implement a
97 Python source code debugger in Python. The standard modules
98 \code{pdb} and \code{wdb} are such debuggers; the difference is that
99 \code{wdb} uses windows and needs STDWIN, while \code{pdb} has a
100 line-oriented interface not unlike dbx. See the file \file{pdb.doc}
101 in the Python library source directory for more documentation (both
102 about \code{pdb} and \code{sys.trace}).
103\end{funcdesc}
104\ttindex{pdb}
105\ttindex{wdb}
106\index{trace function}
107
108\begin{funcdesc}{setprofile}{profilefunc}
109 Set the system's profile function, which allows you to implement a
110 Python source code profiler in Python. The system's profile function
111 is called similarly to the system's trace function (see
112 \code{sys.settrace}), but it isn't called for each executed line of
113 code (only on call and return and when an exception occurs). Also,
114 its return value is not used, so it can just return \code{None}.
115\end{funcdesc}
116\index{profile function}
117
118\begin{datadesc}{stdin}
119\dataline{stdout}
120\dataline{stderr}
121 File objects corresponding to the interpreter's standard input,
122 output and error streams. \code{sys.stdin} is used for all
123 interpreter input except for scripts but including calls to
124 \code{input()} and \code{raw_input()}. \code{sys.stdout} is used
125 for the output of \code{print} and expression statements and for the
126 prompts of \code{input()} and \code{raw_input()}. The interpreter's
127 own prompts and (almost all of) its error messages go to
128 \code{sys.stderr}. \code{sys.stdout} and \code{sys.stderr} needn't
129 be built-in file objects: any object is acceptable as long as it has
130 a \code{write} method that takes a string argument.
131\end{datadesc}
132
133\begin{datadesc}{tracebacklimit}
134When this variable is set to an integer value, it determines the
135maximum number of levels of traceback information printed when an
136unhandled exception occurs. The default is 1000. When set to 0 or
137less, all traceback information is suppressed and only the exception
138type and value are printed.
139\end{datadesc}