blob: ff5d65e9f5b4f4676249f517972ff2b040f53807 [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)}
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.
Guido van Rossum470be141995-03-17 16:07:09 +000012 \code{sys.argv[0]} is the script name (it is operating system
13 dependent whether this is a full pathname or not).
14 If the command was executed using the \samp{-c} command line option
15 to the interpreter, \code{sys.argv[0]} is set to the string
16 \code{"-c"}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017 If no script name was passed to the Python interpreter,
Guido van Rossum470be141995-03-17 16:07:09 +000018 \code{sys.argv} has zero length.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000019\end{datadesc}
20
21\begin{datadesc}{builtin_module_names}
22 A list of strings giving the names of all modules that are compiled
23 into this Python interpreter. (This information is not available in
24 any other way --- \code{sys.modules.keys()} only lists the imported
25 modules.)
26\end{datadesc}
27
28\begin{datadesc}{exc_type}
29\dataline{exc_value}
30\dataline{exc_traceback}
31 These three variables are not always defined; they are set when an
32 exception handler (an \code{except} clause of a \code{try} statement) is
33 invoked. Their meaning is: \code{exc_type} gets the exception type of
34 the exception being handled; \code{exc_value} gets the exception
35 parameter (its \dfn{associated value} or the second argument to
Guido van Rossum470be141995-03-17 16:07:09 +000036 \code{raise}); \code{exc_traceback} gets a traceback object (see the
37 Reference Manual) which
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038 encapsulates the call stack at the point where the exception
39 originally occurred.
Guido van Rossum470be141995-03-17 16:07:09 +000040\obindex{traceback}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041\end{datadesc}
42
43\begin{funcdesc}{exit}{n}
44 Exit from Python with numeric exit status \var{n}. This is
45 implemented by raising the \code{SystemExit} exception, so cleanup
46 actions specified by \code{finally} clauses of \code{try} statements
47 are honored, and it is possible to catch the exit attempt at an outer
48 level.
49\end{funcdesc}
50
51\begin{datadesc}{exitfunc}
52 This value is not actually defined by the module, but can be set by
53 the user (or by a program) to specify a clean-up action at program
54 exit. When set, it should be a parameterless function. This function
Guido van Rossum6b686e91995-07-07 23:00:35 +000055 will be called when the interpreter exits in any way (except when a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056 fatal error occurs: in that case the interpreter's internal state
57 cannot be trusted).
58\end{datadesc}
59
60\begin{datadesc}{last_type}
61\dataline{last_value}
62\dataline{last_traceback}
63 These three variables are not always defined; they are set when an
64 exception is not handled and the interpreter prints an error message
65 and a stack traceback. Their intended use is to allow an interactive
66 user to import a debugger module and engage in post-mortem debugging
Guido van Rossum16d6e711994-08-08 12:30:22 +000067 without having to re-execute the command that caused the error (which
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000068 may be hard to reproduce). The meaning of the variables is the same
69 as that of \code{exc_type}, \code{exc_value} and \code{exc_tracaback},
70 respectively.
71\end{datadesc}
72
73\begin{datadesc}{modules}
74 Gives the list of modules that have already been loaded.
75 This can be manipulated to force reloading of modules and other tricks.
76\end{datadesc}
77
78\begin{datadesc}{path}
79 A list of strings that specifies the search path for modules.
80 Initialized from the environment variable \code{PYTHONPATH}, or an
81 installation-dependent default.
82\end{datadesc}
83
Guido van Rossum6b686e91995-07-07 23:00:35 +000084\begin{datadesc}{platform}
85This string contains a platform identifier. This can be used to
86append platform-specific components to \code{sys.path}, for instance.
87\end{datadesc}
88
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000089\begin{datadesc}{ps1}
90\dataline{ps2}
91 Strings specifying the primary and secondary prompt of the
92 interpreter. These are only defined if the interpreter is in
93 interactive mode. Their initial values in this case are
94 \code{'>>> '} and \code{'... '}.
95\end{datadesc}
96
Guido van Rossum9c51e411995-01-10 10:50:58 +000097\begin{funcdesc}{setcheckinterval}{interval}
98Set the interpreter's ``check interval''. This integer value
99determines how often the interpreter checks for periodic things such
100as thread switches and signal handlers. The default is 10, meaning
101the check is performed every 10 Python virtual instructions. Setting
102it to a larger value may increase performance for programs using
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000103threads. Setting it to a value $\leq 0$ checks every virtual instruction,
Guido van Rossum9c51e411995-01-10 10:50:58 +0000104maximizing responsiveness as well as overhead.
Guido van Rossum7f49b7a1995-01-12 12:38:46 +0000105\end{funcdesc}
Guido van Rossum9c51e411995-01-10 10:50:58 +0000106
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000107\begin{funcdesc}{settrace}{tracefunc}
108 Set the system's trace function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000109 Python source code debugger in Python. See section ``How It Works''
110 in the chapter on the Python Debugger.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000111\end{funcdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000112\index{trace function}
Guido van Rossum470be141995-03-17 16:07:09 +0000113\index{debugger}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000114
115\begin{funcdesc}{setprofile}{profilefunc}
116 Set the system's profile function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000117 Python source code profiler in Python. See the chapter on the
118 Python Profiler. The system's profile function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000119 is called similarly to the system's trace function (see
120 \code{sys.settrace}), but it isn't called for each executed line of
121 code (only on call and return and when an exception occurs). Also,
122 its return value is not used, so it can just return \code{None}.
123\end{funcdesc}
124\index{profile function}
Guido van Rossum470be141995-03-17 16:07:09 +0000125\index{profiler}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000126
127\begin{datadesc}{stdin}
128\dataline{stdout}
129\dataline{stderr}
130 File objects corresponding to the interpreter's standard input,
131 output and error streams. \code{sys.stdin} is used for all
132 interpreter input except for scripts but including calls to
133 \code{input()} and \code{raw_input()}. \code{sys.stdout} is used
134 for the output of \code{print} and expression statements and for the
135 prompts of \code{input()} and \code{raw_input()}. The interpreter's
136 own prompts and (almost all of) its error messages go to
137 \code{sys.stderr}. \code{sys.stdout} and \code{sys.stderr} needn't
138 be built-in file objects: any object is acceptable as long as it has
Guido van Rossum470be141995-03-17 16:07:09 +0000139 a \code{write} method that takes a string argument. (Changing these
140 objects doesn't affect the standard I/O streams of processes
141 executed by \code{popen()}, \code{system()} or the \code{exec*()}
142 family of functions in the \code{os} module.)
143\stmodindex{os}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144\end{datadesc}
145
146\begin{datadesc}{tracebacklimit}
147When this variable is set to an integer value, it determines the
148maximum number of levels of traceback information printed when an
149unhandled exception occurs. The default is 1000. When set to 0 or
150less, all traceback information is suppressed and only the exception
151type and value are printed.
152\end{datadesc}