📄 module-sys.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>3.1 sys -- System-specific parameters and functions</title>
<META NAME="description" CONTENT="3.1 sys -- System-specific parameters and functions">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-gc.html" tppabs="http://www.python.org/doc/current/lib/module-gc.html">
<LINK REL="previous" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">
<LINK REL="up" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">
<LINK REL="next" href="module-gc.html" tppabs="http://www.python.org/doc/current/lib/module-gc.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-gc.html" tppabs="http://www.python.org/doc/current/lib/module-gc.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">3. Python Runtime Services</A>
<b class="navlabel">Up:</b> <a class="sectref" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-gc.html" tppabs="http://www.python.org/doc/current/lib/module-gc.html">3.2 gc </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION005100000000000000000">
3.1 <tt class="module">sys</tt> --
System-specific parameters and functions</A>
</H1>
<P>
<P>
This module provides access to some variables used or maintained by the
interpreter and to functions that interact strongly with the interpreter.
It is always available.
<P>
<dl><dt><b><a name='l2h-208'><tt>argv</tt></a></b>
<dd>
The list of command line arguments passed to a Python script.
<code>argv[0]</code> is the script name (it is operating system
dependent whether this is a full pathname or not).
If the command was executed using the <b class="programopt">-c</b> command line
option to the interpreter, <code>argv[0]</code> is set to the string
<code>'-c'</code>.
If no script name was passed to the Python interpreter,
<code>argv</code> has zero length.
</dl>
<P>
<dl><dt><b><a name='l2h-209'><tt>byteorder</tt></a></b>
<dd>
An indicator of the native byte order. This will have the value
<code>'big'</code> on big-endian (most-signigicant byte first) platforms,
and <code>'little'</code> on little-endian (least-significant byte first)
platforms.
New in version 2.0.
</dl>
<P>
<dl><dt><b><a name='l2h-210'><tt>builtin_module_names</tt></a></b>
<dd>
A tuple of strings giving the names of all modules that are compiled
into this Python interpreter. (This information is not available in
any other way -- <code>modules.keys()</code> only lists the imported
modules.)
</dl>
<P>
<dl><dt><b><a name='l2h-211'><tt>copyright</tt></a></b>
<dd>
A string containing the copyright pertaining to the Python interpreter.
</dl>
<P>
<dl><dt><b><a name='l2h-212'><tt>dllhandle</tt></a></b>
<dd>
Integer specifying the handle of the Python DLL.
Availability: Windows.
</dl>
<P>
<dl><dt><b><a name='l2h-213'><tt class='function'>exc_info</tt></a></b> ()
<dd>
This function returns a tuple of three values that give information
about the exception that is currently being handled. The information
returned is specific both to the current thread and to the current
stack frame. If the current stack frame is not handling an exception,
the information is taken from the calling stack frame, or its caller,
and so on until a stack frame is found that is handling an exception.
Here, ``handling an exception'' is defined as ``executing or having
executed an except clause.'' For any stack frame, only
information about the most recently handled exception is accessible.
<P>
If no exception is being handled anywhere on the stack, a tuple
containing three <code>None</code> values is returned. Otherwise, the
values returned are
<code>(<var>type</var>, <var>value</var>, <var>traceback</var>)</code>.
Their meaning is: <var>type</var> gets the exception type of the exception
being handled (a string or class object); <var>value</var> gets the
exception parameter (its <i class="dfn">associated value</i> or the second argument
to <tt class="keyword">raise</tt>, which is always a class instance if the exception
type is a class object); <var>traceback</var> gets a traceback object (see
the Reference Manual) which encapsulates the call stack at the point
where the exception originally occurred.
<P>
<b>Warning:</b> assigning the <var>traceback</var> return value to a
local variable in a function that is handling an exception will cause
a circular reference. This will prevent anything referenced by a local
variable in the same function or by the traceback from being garbage
collected. Since most functions don't need access to the traceback,
the best solution is to use something like
<code>type, value = sys.exc_info()[:2]</code>
to extract only the exception type and value. If you do need the
traceback, make sure to delete it after use (best done with a
<tt class="keyword">try</tt> ... <tt class="keyword">finally</tt> statement) or to call
<tt class="function">exc_info()</tt> in a function that does not itself handle an
exception.
</dl>
<P>
<dl><dt><b><a name='l2h-214'><tt>exc_type</tt></a></b>
<dd>
<dt><b><a name='l2h-240'><tt>exc_value</tt></a></b><dd>
<dt><b><a name='l2h-241'><tt>exc_traceback</tt></a></b><dd>
<b>Deprecated since release 1.5.</b>
Use <tt class="function">exc_info()</tt> instead.<p>
Since they are global variables, they are not specific to the current
thread, so their use is not safe in a multi-threaded program. When no
exception is being handled, <code>exc_type</code> is set to <code>None</code> and
the other two are undefined.
</dl>
<P>
<dl><dt><b><a name='l2h-215'><tt>exec_prefix</tt></a></b>
<dd>
A string giving the site-specific directory prefix where the
platform-dependent Python files are installed; by default, this is
also <code>'/usr/local'</code>. This can be set at build time with the
<b class="programopt">--exec-prefix</b> argument to the
<b class="program">configure</b> script. Specifically, all configuration files
(e.g. the <span class="file">config.h</span> header file) are installed in the directory
<code>exec_prefix + '/lib/python<var>version</var>/config'</code>, and shared
library modules are installed in <code>exec_prefix +
'/lib/python<var>version</var>/lib-dynload'</code>, where <var>version</var> is equal
to <code>version[:3]</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-216'><tt>executable</tt></a></b>
<dd>
A string giving the name of the executable binary for the Python
interpreter, on systems where this makes sense.
</dl>
<P>
<dl><dt><b><a name='l2h-217'><tt class='function'>exit</tt></a></b> (<big>[</big><var>arg</var><big>]</big>)
<dd>
Exit from Python. This is implemented by raising the
<tt class="exception">SystemExit</tt> exception, so cleanup actions specified by
finally clauses of <tt class="keyword">try</tt> statements are honored, and it is
possible to intercept the exit attempt at an outer level. The
optional argument <var>arg</var> can be an integer giving the exit status
(defaulting to zero), or another type of object. If it is an integer,
zero is considered ``successful termination'' and any nonzero value is
considered ``abnormal termination'' by shells and the like. Most
systems require it to be in the range 0-127, and produce undefined
results otherwise. Some systems have a convention for assigning
specific meanings to specific exit codes, but these are generally
underdeveloped; Unix programs generally use 2 for command line syntax
errors and 1 for all other kind of errors. If another type of object
is passed, <code>None</code> is equivalent to passing zero, and any other
object is printed to <code>sys.stderr</code> and results in an exit code of
1. In particular, <code>sys.exit("some error message")</code> is a quick
way to exit a program when an error occurs.
</dl>
<P>
<dl><dt><b><a name='l2h-218'><tt>exitfunc</tt></a></b>
<dd>
This value is not actually defined by the module, but can be set by
the user (or by a program) to specify a clean-up action at program
exit. When set, it should be a parameterless function. This function
will be called when the interpreter exits. Only one function may be
installed in this way; to allow multiple functions which will be called
at termination, use the <tt class='module'><a href="module-atexit.html" tppabs="http://www.python.org/doc/current/lib/module-atexit.html">atexit</a></tt> module. Note: the exit function
is not called when the program is killed by a signal, when a Python
fatal internal error is detected, or when <code>os._exit()</code> is called.
</dl>
<P>
<dl><dt><b><a name='l2h-219'><tt class='function'>getrefcount</tt></a></b> (<var>object</var>)
<dd>
Return the reference count of the <var>object</var>. The count returned is
generally one higher than you might expect, because it includes the
(temporary) reference as an argument to <tt class="function">getrefcount()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-220'><tt class='function'>getrecursionlimit</tt></a></b> ()
<dd>
Return the current value of the recursion limit, the maximum depth of
the Python interpreter stack. This limit prevents infinite recursion
from causing an overflow of the C stack and crashing Python. It can
be set by <tt class="function">setrecursionlimit()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-221'><tt>hexversion</tt></a></b>
<dd>
The version number encoded as a single integer. This is guaranteed to
increase with each version, including proper support for
non-production releases. For example, to test that the Python
interpreter is at least version 1.5.2, use:
<P>
<dl><dd><pre class="verbatim">
if sys.hexversion >= 0x010502F0:
# use some advanced feature
...
else:
# use an alternative implementation or warn the user
...
</pre></dl>
<P>
This is called "<tt class="samp">hexversion</tt>" since it only really looks meaningful
when viewed as the result of passing it to the built-in
<tt class="function">hex()</tt> function. The <code>version_info</code> value may be used
for a more human-friendly encoding of the same information.
New in version 1.5.2.
</dl>
<P>
<dl><dt><b><a name='l2h-222'><tt>last_type</tt></a></b>
<dd>
<dt><b><a name='l2h-242'><tt>last_value</tt></a></b><dd>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -