blob: f137052301b5f66d9cddb5a569b47c6d37d3482c [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
29\begin{datadesc}{exc_type}
30\dataline{exc_value}
31\dataline{exc_traceback}
32 These three variables are not always defined; they are set when an
33 exception handler (an \code{except} clause of a \code{try} statement) is
34 invoked. Their meaning is: \code{exc_type} gets the exception type of
35 the exception being handled; \code{exc_value} gets the exception
36 parameter (its \dfn{associated value} or the second argument to
Guido van Rossum470be141995-03-17 16:07:09 +000037 \code{raise}); \code{exc_traceback} gets a traceback object (see the
38 Reference Manual) which
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000039 encapsulates the call stack at the point where the exception
40 originally occurred.
Guido van Rossum470be141995-03-17 16:07:09 +000041\obindex{traceback}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042\end{datadesc}
43
Guido van Rossum0a3c7531997-06-02 17:32:41 +000044\begin{datadesc}{exec_prefix}
45A string giving the site-specific
46directory prefix where the platform-dependent Python files are
47installed; by default, this is also \code{"/usr/local"}. This can be
48set at build time with the \code{--exec-prefix} argument to the
49\code{configure} script. Specifically, all configuration files
50(e.g. the \code{config.h} header file) are installed in the directory
51\code{sys.exec_prefix+"/lib/python\emph{VER}/config"}, and shared library
52modules are installed in
53\code{sys.exec_prefix+"/lib/python\emph{VER}/sharedmodules"},
54where \emph{VER} is equal to \code{sys.version[:3]}.
55\end{datadesc}
56
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057\begin{funcdesc}{exit}{n}
58 Exit from Python with numeric exit status \var{n}. This is
59 implemented by raising the \code{SystemExit} exception, so cleanup
60 actions specified by \code{finally} clauses of \code{try} statements
61 are honored, and it is possible to catch the exit attempt at an outer
62 level.
63\end{funcdesc}
64
65\begin{datadesc}{exitfunc}
66 This value is not actually defined by the module, but can be set by
67 the user (or by a program) to specify a clean-up action at program
68 exit. When set, it should be a parameterless function. This function
Guido van Rossum6b686e91995-07-07 23:00:35 +000069 will be called when the interpreter exits in any way (except when a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000070 fatal error occurs: in that case the interpreter's internal state
71 cannot be trusted).
72\end{datadesc}
73
74\begin{datadesc}{last_type}
75\dataline{last_value}
76\dataline{last_traceback}
77 These three variables are not always defined; they are set when an
78 exception is not handled and the interpreter prints an error message
79 and a stack traceback. Their intended use is to allow an interactive
80 user to import a debugger module and engage in post-mortem debugging
Guido van Rossum16d6e711994-08-08 12:30:22 +000081 without having to re-execute the command that caused the error (which
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082 may be hard to reproduce). The meaning of the variables is the same
83 as that of \code{exc_type}, \code{exc_value} and \code{exc_tracaback},
84 respectively.
85\end{datadesc}
86
87\begin{datadesc}{modules}
88 Gives the list of modules that have already been loaded.
89 This can be manipulated to force reloading of modules and other tricks.
90\end{datadesc}
91
92\begin{datadesc}{path}
93 A list of strings that specifies the search path for modules.
94 Initialized from the environment variable \code{PYTHONPATH}, or an
Guido van Rossum0a3c7531997-06-02 17:32:41 +000095 installation-dependent default.
96
97The first item of this list, \code{sys.path[0]}, is the
98directory containing the script that was used to invoke the Python
99interpreter. If the script directory is not available (e.g. if the
100interpreter is invoked interactively or if the script is read from
101standard input), \code{sys.path[0]} is the empty string, which directs
102Python to search modules in the current directory first. Notice that
103the script directory is inserted {\em before} the entries inserted as
104a result of \code{\$PYTHONPATH}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105\end{datadesc}
106
Guido van Rossum6b686e91995-07-07 23:00:35 +0000107\begin{datadesc}{platform}
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000108This string contains a platform identifier, e.g. \code{sunos5} or
109\code{linux1}. This can be used to append platform-specific
110components to \code{sys.path}, for instance.
111\end{datadesc}
112
113\begin{datadesc}{prefix}
114A string giving the site-specific directory prefix where the platform
115independent Python files are installed; by default, this is the string
116\code{"/usr/local"}. This can be set at build time with the
117\code{--prefix} argument to the \code{configure} script. The main
118collection of Python library modules is installed in the directory
119\code{sys.prefix+"/lib/python\emph{VER}"} while the platform
120independent header files (all except \code{config.h}) are stored in
121\code{sys.prefix+"/include/python\emph{VER}"},
122where \emph{VER} is equal to \code{sys.version[:3]}.
123
Guido van Rossum6b686e91995-07-07 23:00:35 +0000124\end{datadesc}
125
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000126\begin{datadesc}{ps1}
127\dataline{ps2}
128 Strings specifying the primary and secondary prompt of the
129 interpreter. These are only defined if the interpreter is in
130 interactive mode. Their initial values in this case are
131 \code{'>>> '} and \code{'... '}.
132\end{datadesc}
133
Guido van Rossum9c51e411995-01-10 10:50:58 +0000134\begin{funcdesc}{setcheckinterval}{interval}
135Set the interpreter's ``check interval''. This integer value
136determines how often the interpreter checks for periodic things such
137as thread switches and signal handlers. The default is 10, meaning
138the check is performed every 10 Python virtual instructions. Setting
139it to a larger value may increase performance for programs using
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000140threads. Setting it to a value $\leq 0$ checks every virtual instruction,
Guido van Rossum9c51e411995-01-10 10:50:58 +0000141maximizing responsiveness as well as overhead.
Guido van Rossum7f49b7a1995-01-12 12:38:46 +0000142\end{funcdesc}
Guido van Rossum9c51e411995-01-10 10:50:58 +0000143
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144\begin{funcdesc}{settrace}{tracefunc}
145 Set the system's trace function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000146 Python source code debugger in Python. See section ``How It Works''
147 in the chapter on the Python Debugger.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000148\end{funcdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000149\index{trace function}
Guido van Rossum470be141995-03-17 16:07:09 +0000150\index{debugger}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000151
152\begin{funcdesc}{setprofile}{profilefunc}
153 Set the system's profile function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000154 Python source code profiler in Python. See the chapter on the
155 Python Profiler. The system's profile function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000156 is called similarly to the system's trace function (see
157 \code{sys.settrace}), but it isn't called for each executed line of
158 code (only on call and return and when an exception occurs). Also,
159 its return value is not used, so it can just return \code{None}.
160\end{funcdesc}
161\index{profile function}
Guido van Rossum470be141995-03-17 16:07:09 +0000162\index{profiler}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000163
164\begin{datadesc}{stdin}
165\dataline{stdout}
166\dataline{stderr}
167 File objects corresponding to the interpreter's standard input,
168 output and error streams. \code{sys.stdin} is used for all
169 interpreter input except for scripts but including calls to
170 \code{input()} and \code{raw_input()}. \code{sys.stdout} is used
171 for the output of \code{print} and expression statements and for the
172 prompts of \code{input()} and \code{raw_input()}. The interpreter's
173 own prompts and (almost all of) its error messages go to
174 \code{sys.stderr}. \code{sys.stdout} and \code{sys.stderr} needn't
175 be built-in file objects: any object is acceptable as long as it has
Guido van Rossum470be141995-03-17 16:07:09 +0000176 a \code{write} method that takes a string argument. (Changing these
177 objects doesn't affect the standard I/O streams of processes
178 executed by \code{popen()}, \code{system()} or the \code{exec*()}
179 family of functions in the \code{os} module.)
180\stmodindex{os}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000181\end{datadesc}
182
183\begin{datadesc}{tracebacklimit}
184When this variable is set to an integer value, it determines the
185maximum number of levels of traceback information printed when an
186unhandled exception occurs. The default is 1000. When set to 0 or
187less, all traceback information is suppressed and only the exception
188type and value are printed.
189\end{datadesc}
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000190
191\begin{datadesc}{version}
192A string containing the version number of the Python interpreter.
193\end{datadesc}