1 \section{\module{sys
} ---
2 System-specific parameters and functions
}
4 \declaremodule{builtin
}{sys
}
5 \modulesynopsis{Access system-specific parameters and functions.
}
7 This module provides access to some variables used or maintained by the
8 interpreter and to functions that interact strongly with the interpreter.
9 It is always available.
12 \begin{datadesc
}{argv
}
13 The list of command line arguments passed to a Python script.
14 \code{argv
[0]} is the script name (it is operating system
15 dependent whether this is a full pathname or not).
16 If the command was executed using the
\programopt{-c
} command line
17 option to the interpreter,
\code{argv
[0]} is set to the string
19 If no script name was passed to the Python interpreter,
20 \code{argv
} has zero length.
23 \begin{datadesc
}{byteorder
}
24 An indicator of the native byte order. This will have the value
25 \code{'big'
} on big-endian (most-signigicant byte first) platforms,
26 and
\code{'little'
} on little-endian (least-significant byte first)
31 \begin{datadesc
}{builtin_module_names
}
32 A tuple of strings giving the names of all modules that are compiled
33 into this Python interpreter. (This information is not available in
34 any other way ---
\code{modules.keys()
} only lists the imported
38 \begin{datadesc
}{copyright
}
39 A string containing the copyright pertaining to the Python interpreter.
42 \begin{datadesc
}{dllhandle
}
43 Integer specifying the handle of the Python DLL.
44 Availability: Windows.
47 \begin{funcdesc
}{displayhook
}{\var{value
}}
48 If
\var{value
} is not
\code{None
}, this function prints it to
49 \code{sys.stdout
}, and saves it in
\code{__builtin__._
}.
51 \code{sys.displayhook
} is called on the result of evaluating
52 an expression entered in an interactive Python session.
53 The display of these values can be customized by assigning
54 another one-argument function to
\code{sys.displayhook
}.
57 \begin{funcdesc
}{excepthook
}{\var{type
},
\var{value
},
\var{traceback
}}
58 This function prints out a given traceback and exception to
61 When an exception is raised and uncaught, the interpreter calls
62 \code{sys.excepthook
} with three arguments, the exception class,
63 exception instance, and a traceback object.
64 In an interactive session this happens just before
65 control is returned to the prompt; in a Python program this happens
66 just before the program exits.
67 The handling of such top-level exceptions can be customized by
68 assigning another three-argument function to
\code{sys.excepthook
}.
71 \begin{datadesc
}{__displayhook__
}
72 \dataline{__excepthook__
}
73 These objects contain the original values of
\code{displayhook
}
74 and
\code{excepthook
} at the start of the program. They are saved
75 so that
\code{displayhook
} and
\code{excepthook
} can be restored
76 in case they happen to get replaced with broken objects.
79 \begin{funcdesc
}{exc_info
}{}
80 This function returns a tuple of three values that give information
81 about the exception that is currently being handled. The information
82 returned is specific both to the current thread and to the current
83 stack frame. If the current stack frame is not handling an exception,
84 the information is taken from the calling stack frame, or its caller,
85 and so on until a stack frame is found that is handling an exception.
86 Here, ``handling an exception'' is defined as ``executing or having
87 executed an except clause.'' For any stack frame, only
88 information about the most recently handled exception is accessible.
90 If no exception is being handled anywhere on the stack, a tuple
91 containing three
\code{None
} values is returned. Otherwise, the
93 \code{(
\var{type
},
\var{value
},
\var{traceback
})
}.
94 Their meaning is:
\var{type
} gets the exception type of the exception
95 being handled (a string or class object);
\var{value
} gets the
96 exception parameter (its
\dfn{associated value
} or the second argument
97 to
\keyword{raise
}, which is always a class instance if the exception
98 type is a class object);
\var{traceback
} gets a traceback object (see
99 the Reference Manual) which encapsulates the call stack at the point
100 where the exception originally occurred.
103 \strong{Warning:
} assigning the
\var{traceback
} return value to a
104 local variable in a function that is handling an exception will cause
105 a circular reference. This will prevent anything referenced by a local
106 variable in the same function or by the traceback from being garbage
107 collected. Since most functions don't need access to the traceback,
108 the best solution is to use something like
109 \code{type, value = sys.exc_info()
[:
2]}
110 to extract only the exception type and value. If you do need the
111 traceback, make sure to delete it after use (best done with a
112 \keyword{try
} ...
\keyword{finally
} statement) or to call
113 \function{exc_info()
} in a function that does not itself handle an
117 \begin{datadesc
}{exc_type
}
119 \dataline{exc_traceback
}
121 {Use
\function{exc_info()
} instead.
}
122 Since they are global variables, they are not specific to the current
123 thread, so their use is not safe in a multi-threaded program. When no
124 exception is being handled,
\code{exc_type
} is set to
\code{None
} and
125 the other two are undefined.
128 \begin{datadesc
}{exec_prefix
}
129 A string giving the site-specific directory prefix where the
130 platform-dependent Python files are installed; by default, this is
131 also
\code{'/usr/local'
}. This can be set at build time with the
132 \longprogramopt{exec-prefix
} argument to the
133 \program{configure
} script. Specifically, all configuration files
134 (e.g. the
\file{config.h
} header file) are installed in the directory
135 \code{exec_prefix + '/lib/python
\var{version
}/config'
}, and shared
136 library modules are installed in
\code{exec_prefix +
137 '/lib/python
\var{version
}/lib-dynload'
}, where
\var{version
} is equal
138 to
\code{version
[:
3]}.
141 \begin{datadesc
}{executable
}
142 A string giving the name of the executable binary for the Python
143 interpreter, on systems where this makes sense.
146 \begin{funcdesc
}{exit
}{\optional{arg
}}
147 Exit from Python. This is implemented by raising the
148 \exception{SystemExit
} exception, so cleanup actions specified by
149 finally clauses of
\keyword{try
} statements are honored, and it is
150 possible to intercept the exit attempt at an outer level. The
151 optional argument
\var{arg
} can be an integer giving the exit status
152 (defaulting to zero), or another type of object. If it is an integer,
153 zero is considered ``successful termination'' and any nonzero value is
154 considered ``abnormal termination'' by shells and the like. Most
155 systems require it to be in the range
0-
127, and produce undefined
156 results otherwise. Some systems have a convention for assigning
157 specific meanings to specific exit codes, but these are generally
158 underdeveloped; Unix programs generally use
2 for command line syntax
159 errors and
1 for all other kind of errors. If another type of object
160 is passed,
\code{None
} is equivalent to passing zero, and any other
161 object is printed to
\code{sys.stderr
} and results in an exit code of
162 1. In particular,
\code{sys.exit("some error message")
} is a quick
163 way to exit a program when an error occurs.
166 \begin{datadesc
}{exitfunc
}
167 This value is not actually defined by the module, but can be set by
168 the user (or by a program) to specify a clean-up action at program
169 exit. When set, it should be a parameterless function. This function
170 will be called when the interpreter exits. Only one function may be
171 installed in this way; to allow multiple functions which will be called
172 at termination, use the
\refmodule{atexit
} module. Note: the exit function
173 is not called when the program is killed by a signal, when a Python
174 fatal internal error is detected, or when
\code{os._exit()
} is called.
177 \begin{funcdesc
}{getdefaultencoding
}{}
178 Return the name of the current default string encoding used by the
179 Unicode implementation.
183 \begin{funcdesc
}{getrefcount
}{object
}
184 Return the reference count of the
\var{object
}. The count returned is
185 generally one higher than you might expect, because it includes the
186 (temporary) reference as an argument to
\function{getrefcount()
}.
189 \begin{funcdesc
}{getrecursionlimit
}{}
190 Return the current value of the recursion limit, the maximum depth of
191 the Python interpreter stack. This limit prevents infinite recursion
192 from causing an overflow of the C stack and crashing Python. It can
193 be set by
\function{setrecursionlimit()
}.
196 \begin{funcdesc
}{_getframe
}{\optional{depth
}}
197 Return a frame object from the call stack. If optional integer
198 \var{depth
} is given, return the frame object that many calls below
199 the top of the stack. If that is deeper than the call stack,
200 \exception{ValueError
} is raised. The default for
\var{depth
} is
201 zero, returning the frame at the top of the call stack.
203 This function should be used for internal and specialized
207 \begin{datadesc
}{hexversion
}
208 The version number encoded as a single integer. This is guaranteed to
209 increase with each version, including proper support for
210 non-production releases. For example, to test that the Python
211 interpreter is at least version
1.5.2, use:
214 if sys.hexversion >=
0x010502F0:
215 # use some advanced feature
218 # use an alternative implementation or warn the user
222 This is called
\samp{hexversion
} since it only really looks meaningful
223 when viewed as the result of passing it to the built-in
224 \function{hex()
} function. The
\code{version_info
} value may be used
225 for a more human-friendly encoding of the same information.
229 \begin{datadesc
}{last_type
}
230 \dataline{last_value
}
231 \dataline{last_traceback
}
232 These three variables are not always defined; they are set when an
233 exception is not handled and the interpreter prints an error message
234 and a stack traceback. Their intended use is to allow an interactive
235 user to import a debugger module and engage in post-mortem debugging
236 without having to re-execute the command that caused the error.
237 (Typical use is
\samp{import pdb; pdb.pm()
} to enter the post-mortem
238 debugger; see the chapter ``The Python Debugger'' for more
242 The meaning of the variables is the same
243 as that of the return values from
\function{exc_info()
} above.
244 (Since there is only one interactive thread, thread-safety is not a
245 concern for these variables, unlike for
\code{exc_type
} etc.)
248 \begin{datadesc
}{maxint
}
249 The largest positive integer supported by Python's regular integer
250 type. This is at least
2**
31-
1. The largest negative integer is
251 \code{-maxint-
1} -- the asymmetry results from the use of
2's
252 complement binary arithmetic.
255 \begin{datadesc
}{modules
}
256 This is a dictionary that maps module names to modules which have
257 already been loaded. This can be manipulated to force reloading of
258 modules and other tricks. Note that removing a module from this
259 dictionary is
\emph{not
} the same as calling
260 \function{reload()
}\bifuncindex{reload
} on the corresponding module
264 \begin{datadesc
}{path
}
265 \indexiii{module
}{search
}{path
}
266 A list of strings that specifies the search path for modules.
267 Initialized from the environment variable
\envvar{PYTHONPATH
}, or an
268 installation-dependent default.
270 The first item of this list,
\code{path
[0]}, is the
271 directory containing the script that was used to invoke the Python
272 interpreter. If the script directory is not available (e.g. if the
273 interpreter is invoked interactively or if the script is read from
274 standard input),
\code{path
[0]} is the empty string, which directs
275 Python to search modules in the current directory first. Notice that
276 the script directory is inserted
\emph{before
} the entries inserted as
277 a result of
\envvar{PYTHONPATH
}.
280 \begin{datadesc
}{platform
}
281 This string contains a platform identifier, e.g.
\code{'sunos5'
} or
282 \code{'linux1'
}. This can be used to append platform-specific
283 components to
\code{path
}, for instance.
286 \begin{datadesc
}{prefix
}
287 A string giving the site-specific directory prefix where the platform
288 independent Python files are installed; by default, this is the string
289 \code{'/usr/local'
}. This can be set at build time with the
290 \longprogramopt{prefix
} argument to the
291 \program{configure
} script. The main collection of Python library
292 modules is installed in the directory
\code{prefix +
293 '/lib/python
\var{version
}'
} while the platform independent header
294 files (all except
\file{config.h
}) are stored in
\code{prefix +
295 '/include/python
\var{version
}'
}, where
\var{version
} is equal to
299 \begin{datadesc
}{ps1
}
301 \index{interpreter prompts
}
302 \index{prompts, interpreter
}
303 Strings specifying the primary and secondary prompt of the
304 interpreter. These are only defined if the interpreter is in
305 interactive mode. Their initial values in this case are
306 \code{'>
\code{>
}> '
} and
\code{'... '
}. If a non-string object is assigned
307 to either variable, its
\function{str()
} is re-evaluated each time
308 the interpreter prepares to read a new interactive command; this can
309 be used to implement a dynamic prompt.
312 \begin{funcdesc
}{setcheckinterval
}{interval
}
313 Set the interpreter's ``check interval''. This integer value
314 determines how often the interpreter checks for periodic things such
315 as thread switches and signal handlers. The default is
\code{10}, meaning
316 the check is performed every
10 Python virtual instructions. Setting
317 it to a larger value may increase performance for programs using
318 threads. Setting it to a value
\code{<=
} 0 checks every virtual instruction,
319 maximizing responsiveness as well as overhead.
322 \begin{funcdesc
}{setdefaultencoding
}{name
}
323 Set the current default string encoding used by the Unicode
324 implementation. If
\var{name
} does not match any available
325 encoding,
\exception{LookupError
} is raised. This function is only
326 intended to be used by the
\refmodule{site
} module implementation
327 and, where needed, by
\module{sitecustomize
}. Once used by the
328 \refmodule{site
} module, it is removed from the
\module{sys
}
330 % Note that \refmodule{site} is not imported if
331 % the \programopt{-S} option is passed to the interpreter, in which
332 % case this function will remain available.
336 \begin{funcdesc
}{setprofile
}{profilefunc
}
337 Set the system's profile function, which allows you to implement a
338 Python source code profiler in Python. See the chapter on the
339 Python Profiler. The system's profile function
340 is called similarly to the system's trace function (see
341 \function{settrace()
}), but it isn't called for each executed line of
342 code (only on call and return and when an exception occurs). Also,
343 its return value is not used, so it can just return
\code{None
}.
345 \index{profile function
}
348 \begin{funcdesc
}{setrecursionlimit
}{limit
}
349 Set the maximum depth of the Python interpreter stack to
\var{limit
}.
350 This limit prevents infinite recursion from causing an overflow of the
351 C stack and crashing Python.
353 The highest possible limit is platform-dependent. A user may need to
354 set the limit higher when she has a program that requires deep
355 recursion and a platform that supports a higher limit. This should be
356 done with care, because a too-high limit can lead to a crash.
359 \begin{funcdesc
}{settrace
}{tracefunc
}
360 Set the system's trace function, which allows you to implement a
361 Python source code debugger in Python. See section ``How It Works''
362 in the chapter on the Python Debugger.
364 \index{trace function
}
367 \begin{datadesc
}{stdin
}
370 File objects corresponding to the interpreter's standard input,
371 output and error streams.
\code{stdin
} is used for all
372 interpreter input except for scripts but including calls to
373 \function{input()
}\bifuncindex{input
} and
374 \function{raw_input()
}\bifuncindex{raw_input
}.
\code{stdout
} is used
375 for the output of
\keyword{print
} and expression statements and for the
376 prompts of
\function{input()
} and
\function{raw_input()
}. The interpreter's
377 own prompts and (almost all of) its error messages go to
378 \code{stderr
}.
\code{stdout
} and
\code{stderr
} needn't
379 be built-in file objects: any object is acceptable as long as it has
380 a
\method{write()
} method that takes a string argument. (Changing these
381 objects doesn't affect the standard I/O streams of processes
382 executed by
\function{os.popen()
},
\function{os.system()
} or the
383 \function{exec*()
} family of functions in the
\refmodule{os
} module.)
387 \begin{datadesc
}{__stdin__
}
388 \dataline{__stdout__
}
389 \dataline{__stderr__
}
390 These objects contain the original values of
\code{stdin
},
391 \code{stderr
} and
\code{stdout
} at the start of the program. They are
392 used during finalization, and could be useful to restore the actual
393 files to known working file objects in case they have been overwritten
394 with a broken object.
397 \begin{datadesc
}{tracebacklimit
}
398 When this variable is set to an integer value, it determines the
399 maximum number of levels of traceback information printed when an
400 unhandled exception occurs. The default is
\code{1000}. When set to
401 0 or less, all traceback information is suppressed and only the
402 exception type and value are printed.
405 \begin{datadesc
}{version
}
406 A string containing the version number of the Python interpreter plus
407 additional information on the build number and compiler used. It has
408 a value of the form
\code{'
\var{version
} (\#
\var{build_number
},
409 \var{build_date
},
\var{build_time
})
[\var{compiler
}]'
}. The first
410 three characters are used to identify the version in the installation
411 directories (where appropriate on each platform). An example:
416 '
1.5.2 (
#0 Apr
13 1999,
10:
51:
12)
[MSC
32 bit (Intel)
]'
420 \begin{datadesc
}{version_info
}
421 A tuple containing the five components of the version number:
422 \var{major
},
\var{minor
},
\var{micro
},
\var{releaselevel
}, and
423 \var{serial
}. All values except
\var{releaselevel
} are integers; the
424 release level is
\code{'alpha'
},
\code{'beta'
},
425 \code{'candidate'
}, or
\code{'final'
}. The
\code{version_info
} value
426 corresponding to the Python version
2.0 is
427 \code{(
2,
0,
0, 'final',
0)
}.
431 \begin{datadesc
}{winver
}
432 The version number used to form registry keys on Windows platforms.
433 This is stored as string resource
1000 in the Python DLL. The value
434 is normally the first three characters of
\constant{version
}. It is
435 provided in the
\module{sys
} module for informational purposes;
436 modifying this value has no effect on the registry keys used by
438 Availability: Windows.