The sys
module contains interpreter-related exports. It also
provides access to some environment components, such as the command
line, standard streams, and so on.
argv
Command-line argument strings list: [command,
arguments...]
. Like C’s argv
array.
byteorder
builtin_module_names
Tuple of string names of C modules compiled into this Python interpreter.
copyright
dllhandle
Python DLL integer handle; Windows only (see the Python Library Reference).
displayhook(value)
Called by Python to display result values in interactive
sessions; assign sys.displayhook
to a one-argument
function to customize output.
excepthook(type, value,
traceback)
Called by Python to display uncaught exception details to
stderr
; assign sys.excepthook
to a three-argument
function to customize exception displays.
exc_info()
Returns tuple of three values describing the exception
currently being handled (type
,
value
, traceback)
, where type
is the exception class, value
is the instance of the exception
class raised, and traceback
is
an object that gives access to the runtime call stack as it
existed when the exception occurred. Specific to current thread.
Subsumes exc_type
, exc_value
, and exc_traceback
in Python 1.5 and later
(all three of which are removed completely in Python 3.0). See the
traceback
module in the Python
Library Reference for processing traceback objects, and The try Statement for more on exceptions.
exec_prefix
Assign to a string giving the site-specific directory prefix where the platform-dependent Python files are installed; defaults to /usr/local or a build-time argument. Use this to locate shared library modules (in <exec_prefix>/lib/python<version>/lib-dynload) and configuration files.
executable
String giving the full file pathname of the Python interpreter program running the caller.
exit([N])
Exits from a Python process with status N
(default 0) by raising a SystemExit
built-in exception (can be
caught in a try
statement and
ignored). See also SystemExit
(in Built-in Exceptions) and the os._exit()
function (in The os System Module), which exits immediately
without exception processing (useful in child processes after an
os.fork()
). Also see the
atexit
module for exit function
specification.
getcheckinterval()
Returns the interpreter’s “check interval”; see setcheckinterval
,
later in this list.
getdefaultencoding()
Returns the name of the current default string encoding used by the Unicode implementation.
getfilesystemencoding()
Returns the name of the encoding used to convert Unicode
filenames into system file names, or None
if the system default encoding is
used.
getrefcount(object)
Returns object
’s current
reference count value (+1 for the call’s argument).
getrecursionlimit()
Returns the maximum depth limit of the Python call stack;
see also setrecursionlimit
,
later in this list.
getsizeof(object [,
default])
Returns the size of an object in bytes. The object can be
any type of object. All built-in objects return correct results,
but third-party extension results are implementation specific.
default
provides a value that
will be returned if the object type does not implement the size
retrieval interface.
_getframe([depth])
Returns a frame object from the Python call stack (see the Python Library Reference).
hexversion
Python version number, encoded as a single integer (viewed
best with the hex()
built-in
function). Increases with each new release.
intern(string)
Enters string
in the
table of “interned” strings and returns the interned string—the
string itself or a copy. Interning strings provides a small
performance improvement for dictionary lookup: if both the keys in
a dictionary and the lookup key are interned, key comparisons
(after hashing) can be done by comparing pointers instead of
strings. Normally, names used in Python programs are automatically
interned, and the dictionaries used to hold module, class, and
instance attributes have interned keys.
last_type
last_value
last_traceback
Type, value, and traceback objects of last uncaught exception (mostly for postmortem debugging).
maxsize
An integer giving the maximum value a variable of type
Py_ssize_t
can take. It’s
usually 2**31 − 1
on a 32-bit
platform and 2**63 − 1
on a
64-bit platform.
maxunicode
An integer giving the largest supported code point for a Unicode character. The value of this depends on the configuration option that specifies whether Unicode characters are stored as UCS-2 or UCS-4.
modules
Dictionary of modules that are already loaded; there is one
name:object
entry per module.
Writable (for example, del sys.modules['name']
forces a
module to be reloaded on next import).
path
List of strings specifying module import search path.
Initialized from PYTHONPATH
shell variable, .pth path
files, and any installation-dependent defaults. Writable (e.g.,
sys.path.append('C:\dir')
adds
a directory to the search path within a script).
The first item, path[0]
,
is the directory containing the script that was used to invoke the
Python interpreter. If the script directory is not available
(e.g., if the interpreter is invoked interactively or if the
script is read from standard input), path[0]
is the empty string, which
directs Python to search modules in the current working directory
first. The script directory is inserted before the entries
inserted from PYTHONPATH
.
platform
String identifying the system on which Python is running:
e.g., 'sunos5'
, 'darwin'
, 'linux2'
, 'win32'
, 'cygwin'
, 'PalmOS3'
. Useful for tests in
platform-dependent code.
Hint: 'win32'
means
all current flavors of Windows, or
test as sys.platform[:3]=='win'
or sys.platform.startswith('win')
.
prefix
Assign to a string giving the site-specific directory prefix, where platform-independent Python files are installed; defaults to /usr/local or a build-time argument. Python library modules are installed in the directory <prefix>/lib/python<version>; platform-independent header files are stored in <prefix>/include/python<version>.
ps1
String specifying primary prompt in interactive mode;
defaults to >>>
unless
assigned.
ps2
String specifying secondary prompt for compound statement
continuations, in interactive mode; defaults to ...
unless assigned.
dont_write_bytecode
If this is true, Python won’t try to write “.pyc” or “.pyo” files on the import of source modules (see also “-B” command-line option).
setcheckinterval(reps)
Call to set how often the interpreter checks for
periodic tasks (e.g., thread switches, signal handlers) to
reps
. Measured in virtual
machine instructions (default is 100). In general, a Python
statement translates to multiple virtual machine instructions.
Lower values maximize thread responsiveness but also maximize
thread switch overhead.
setdefaultencoding(name)
Call to set the current default string encoding used by
the Unicode implementation. Intended for use by the site
module and is available during
start-up only.
setprofile(func)
Call to set the system profile function to func
: the profiler’s “hook” (not run for
each line). See the Python Library Reference for details.
setrecursionlimit(depth)
Call to set maximum depth of the Python call stack to
depth
. This limit prevents
infinite recursion from causing an overflow of the C stack and
crashing Python. The default is 1,000 on Windows, but this may
vary.
settrace(func)
Call to set the system trace function to func
: the program location or state
change callback “hook” used by debuggers, etc. See the Python
Library Reference for details.
stdin
Standard input stream: a preopened file object. Can be
assigned to any object with read
methods to reset input within a
script (e.g., sys.stdin=MyObj()
). Used for interpreter
input, including the input()
built-in function (and raw_input()
in Python 2).
stdout
Standard output stream: a preopened file object. Can be
assigned to any object with write
methods to reset output within a
script (e.g., sys.stdout=open('log',
'a')
). Used for some prompts and the print()
built-in function (and print
statement in Python 2).
stderr
Standard error stream: a preopened file object. Can be
assigned to any object with write
methods to reset stderr
within a script (e.g., sys.stderr=wrappedsocket
). Used for
interpreter prompts/errors.
__stdin__
__stdout__
__stderr__
Original values of stdin
,
stderr
, and stdout
at program start (e.g., for
restores as a last resort; normally, when assigning to sys.stdout
, etc., save the old value and
restore it in a finally
clause). Can be None
for GUI
apps on Windows with no console.
tracebacklimit
Maximum number of traceback levels to print on uncaught exceptions; defaults to 1,000 unless assigned.
version
String containing the version number of the Python interpreter.
version_info
Tuple containing five version identification components:
major, minor, micro, release level, and serial. For Python 3.0.1,
this is (3, 0, 1, 'final', 0)
(see the Python Library Reference).
winver
Version number used to form registry keys on Windows platforms (available only on Windows; see the Python Library Reference).