sys — System-specific parameters and functions¶
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. Unless explicitly noted otherwise, all variables are read-only.
- sys.abi_info¶
Added in version 3.15.
An object containing information about the ABI of the currently running Python interpreter. It should include information that affect the CPython ABI in ways that require a specific build of the interpreter chosen from variants that can co-exist on a single machine. For example, it does not encode the base OS (Linux or Windows), but does include pointer size since some systems support both 32- and 64-bit builds. The available entries are the same on all platforms; e.g. pointer_size is available even on 64-bit-only architectures.
The following attributes are available:
- abi_info.pointer_bits¶
The width of pointers in bits, as an integer, equivalent to
8 * sizeof(void *). Usually, this is32or64.
- abi_info.free_threaded¶
A Boolean indicating whether the interpreter was built with free threading support. This reflects either the presence of the
--disable-gilconfigureoption (on Unix) or setting theDisableGilproperty (on Windows).
- abi_info.debug¶
A Boolean indicating whether the interpreter was built in debug mode. This reflects either the presence of the
--with-pydebugconfigureoption (on Unix) or theDebugconfiguration (on Windows).
- sys.abiflags¶
On POSIX systems where Python was built with the standard
configurescript, this contains the ABI flags as specified by PEP 3149.Added in version 3.2.
Changed in version 3.8: Default flags became an empty string (
mflag for pymalloc has been removed).Availability: Unix.
- sys.addaudithook(hook)¶
Append the callable hook to the list of active auditing hooks for the current (sub)interpreter.
When an auditing event is raised through the
sys.audit()function, each hook will be called in the order it was added with the event name and the tuple of arguments. Native hooks added byPySys_AddAuditHook()are called first, followed by hooks added in the current (sub)interpreter. Hooks can then log the event, raise an exception to abort the operation, or terminate the process entirely.Note that audit hooks are primarily for collecting information about internal or otherwise unobservable actions, whether by Python or libraries written in Python. They are not suitable for implementing a “sandbox”. In particular, malicious code can trivially disable or bypass hooks added using this function. At a minimum, any security-sensitive hooks must be added using the C API
PySys_AddAuditHook()before initialising the runtime, and any modules allowing arbitrary memory modification (such asctypes) should be completely removed or closely monitored.Calling
sys.addaudithook()will itself raise an auditing event namedsys.addaudithookwith no arguments. If any existing hooks raise an exception derived fromRuntimeError, the new hook will not be added and the exception suppressed. As a result, callers cannot assume that their hook has been added unless they control all existing hooks.See the audit events table for all events raised by CPython, and PEP 578 for the original design discussion.
Added in version 3.8.
Changed in version 3.8.1: Exceptions derived from
Exceptionbut notRuntimeErrorare no longer suppressed.CPython implementation detail: When tracing is enabled (see
settrace()), Python hooks are only traced if the callable has a__cantrace__member that is set to a true value. Otherwise, trace functions will skip the hook.
- sys.argv¶
The list of command line arguments passed to a Python script.
argv[0]is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the-ccommand line option to the interpreter,argv[0]is set to the string'-c'. If no script name was passed to the Python interpreter,argv[0]is the empty string.To loop over the standard input, or the list of files given on the command line, see the
fileinputmodule.See also
sys.orig_argv.Note
On Unix, command line arguments are passed by bytes from OS. Python decodes them with filesystem encoding and “surrogateescape” error handler. When you need original bytes, you can get it by
[os.fsencode(arg) for arg in sys.argv].
- sys.audit(event, *args)¶
Raise an auditing event and trigger any active auditing hooks. event is a string identifying the event, and args may contain optional arguments with more information about the event. The number and types of arguments for a given event are considered a public and stable API and should not be modified between releases.
For example, one auditing event is named
os.chdir. This event has one argument called path that will contain the requested new working directory.sys.audit()will call the existing auditing hooks, passing the event name and arguments, and will re-raise the first exception from any hook. In general, if an exception is raised, it should not be handled and the process should be terminated as quickly as possible. This allows hook implementations to decide how to respond to particular events: they can merely log the event or abort the operation by raising an exception.Hooks are added using the
sys.addaudithook()orPySys_AddAuditHook()functions.The native equivalent of this function is
PySys_Audit(). Using the native function is preferred when possible.See the audit events table for all events raised by CPython.
Added in version 3.8.
- sys.base_exec_prefix¶
Equivalent to
exec_prefix, but referring to the base Python installation.When running under Virtual Environments,
exec_prefixgets overwritten to the virtual environment prefix.base_exec_prefix, conversely, does not change, and always points to the base Python installation. Refer to Virtual Environments for more information.Added in version 3.3.
- sys.base_prefix¶
Equivalent to
prefix, but referring to the base Python installation.When running under virtual environment,
prefixgets overwritten to the virtual environment prefix.base_prefix, conversely, does not change, and always points to the base Python installation. Refer to Virtual Environments for more information.Added in version 3.3.
- sys.byteorder¶
An indicator of the native byte order. This will have the value
'big'on big-endian (most-significant byte first) platforms, and'little'on little-endian (least-significant byte first) platforms.
- sys.builtin_module_names¶
A tuple of strings containing the names of all modules that are compiled into this Python interpreter. (This information is not available in any other way —
modules.keys()only lists the imported modules.)See also the
sys.stdlib_module_nameslist.
- sys.call_tracing(func, args)¶
Call
func(*args), while tracing is enabled. The tracing state is saved, and restored afterwards. This is intended to be called from a debugger from a checkpoint, to recursively debug or profile some other code.Tracing is suspended while calling a tracing function set by
settrace()orsetprofile()to avoid infinite recursion.call_tracing()enables explicit recursion of the tracing function.
- sys.copyright¶
A string containing the copyright pertaining to the Python interpreter.
- sys._clear_type_cache()¶
Clear the internal type cache. The type cache is used to speed up attribute and method lookups. Use the function only to drop unnecessary references during reference leak debugging.
This function should be used for internal and specialized purposes only.
Deprecated since version 3.13: Use the more general
_clear_internal_caches()function instead.
- sys._clear_internal_caches()¶
Clear all internal performance-related caches. Use this function only to release unnecessary references and memory blocks when hunting for leaks.
Added in version 3.13.
- sys._current_frames()¶
Return a dictionary mapping each thread’s identifier to the topmost stack frame currently active in that thread at the time the function is called. Note that functions in the
tracebackmodule can build the call stack given such a frame.This is most useful for debugging deadlock: this function does not require the deadlocked threads’ cooperation, and such threads’ call stacks are frozen for as long as they remain deadlocked. The frame returned for a non-deadlocked thread may bear no relationship to that thread’s current activity by the time calling code examines the frame.
This function should be used for internal and specialized purposes only.
Raises an auditing event
sys._current_frameswith no arguments.
- sys._current_exceptions()¶
Return a dictionary mapping each thread’s identifier to the topmost exception currently active in that thread at the time the function is called. If a thread is not currently handling an exception, it is not included in the result dictionary.
This is most useful for statistical profiling.
This function should be used for internal and specialized purposes only.
Raises an auditing event
sys._current_exceptionswith no arguments.Changed in version 3.12: Each value in the dictionary is now a single exception instance, rather than a 3-tuple as returned from
sys.exc_info().
- sys.breakpointhook()¶
This hook function is called by built-in
breakpoint(). By default, it drops you into thepdbdebugger, but it can be set to any other function so that you can choose which debugger gets used.The signature of this function is dependent on what it calls. For example, the default binding (e.g.
pdb.set_trace()) expects no arguments, but you might bind it to a function that expects additional arguments (positional and/or keyword). The built-inbreakpoint()function passes its*argsand**kwsstraight through. Whateverbreakpointhooks()returns is returned frombreakpoint().The default implementation first consults the environment variable
PYTHONBREAKPOINT. If that is set to"0"then this function returns immediately; i.e. it is a no-op. If the environment variable is not set, or is set to the empty string,pdb.set_trace()is called. Otherwise this variable should name a function to run, using Python’s dotted-import nomenclature, e.g.package.subpackage.module.function. In this case,package.subpackage.modulewould be imported and the resulting module must have a callable namedfunction(). This is run, passing in*argsand**kws, and whateverfunction()returns,sys.breakpointhook()returns to the built-inbreakpoint()function.Note that if anything goes wrong while importing the callable named by
PYTHONBREAKPOINT, aRuntimeWarningis reported and the breakpoint is ignored.Also note that if
sys.breakpointhook()is overridden programmatically,PYTHONBREAKPOINTis not consulted.Added in version 3.7.
- sys._debugmallocstats()¶
Print low-level information to stderr about the state of CPython’s memory allocator.
If Python is built in debug mode (
configure --with-pydebug option), it also performs some expensive internal consistency checks.Added in version 3.3.
CPython implementation detail: This function is specific to CPython. The exact output format is not defined here, and may change.
- sys.dllhandle¶
Integer specifying the handle of the Python DLL.
Availability: Windows.
- sys.displayhook(value)¶
If value is not
None, this function printsrepr(value)tosys.stdout, and saves value inbuiltins._. Ifrepr(value)is not encodable tosys.stdout.encodingwithsys.stdout.errorserror handler (which is probably'strict'), encode it tosys.stdout.encodingwith'backslashreplace'error handler.sys.displayhookis called on the result of evaluating an expression entered in an interactive Python session. The display of these values can be customized by assigning another one-argument function tosys.displayhook.Pseudo-code:
def displayhook(value): if value is None: return # Set '_' to None to avoid recursion builtins._ = None text = repr(value) try: sys.stdout.write(text) except UnicodeEncodeError: bytes = text.encode(sys.stdout.encoding, 'backslashreplace') if hasattr(sys.stdout, 'buffer'): sys.stdout.buffer.write(bytes) else: text = bytes.decode(sys.stdout.encoding, 'strict') sys.stdout.write(text) sys.stdout.write("\n") builtins._ = value
Changed in version 3.2: Use
'backslashreplace'error handler onUnicodeEncodeError.
- sys.dont_write_bytecode¶
If this is true, Python won’t try to write
.pycfiles on the import of source modules. This value is initially set toTrueorFalsedepending on the-Bcommand line option and thePYTHONDONTWRITEBYTECODEenvironment variable, but you can set it yourself to control bytecode file generation.
- sys._emscripten_info¶
A named tuple holding information about the environment on the wasm32-emscripten platform. The named tuple is provisional and may change in the future.
- _emscripten_info.emscripten_version¶
Emscripten version as tuple of ints (major, minor, micro), e.g.
(3, 1, 8).
- _emscripten_info.runtime¶
Runtime string, e.g. browser user agent,
'Node.js v14.18.2', or'UNKNOWN'.
- _emscripten_info.pthreads¶
Trueif Python is compiled with Emscripten pthreads support.
Trueif Python is compiled with shared memory support.
Availability: Emscripten.
Added in version 3.11.
- sys.pycache_prefix¶
If this is set (not
None), Python will write bytecode-cache.pycfiles to (and read them from) a parallel directory tree rooted at this directory, rather than from__pycache__directories in the source code tree. Any__pycache__directories in the source code tree will be ignored and new.pycfiles written within the pycache prefix. Thus if you usecompileallas a pre-build step, you must ensure you run it with the same pycache prefix (if any) that you will use at runtime.A relative path is interpreted relative to the current working directory.
This value is initially set based on the value of the
-Xpycache_prefix=PATHcommand-line option or thePYTHONPYCACHEPREFIXenvironment variable (command-line takes precedence). If neither are set, it isNone.Added in version 3.8.
- sys.excepthook(type, value, traceback)¶
This function prints out a given traceback and exception to
sys.stderr.When an exception other than
SystemExitis raised and uncaught, the interpreter callssys.excepthookwith three arguments, the exception class, exception instance, and a traceback object. In an interactive session this happens just before control is returned to the prompt; in a Python program this happens just before the program exits. The handling of such top-level exceptions can be customized by assigning another three-argument function tosys.excepthook.Raise an auditing event
sys.excepthookwith argumentshook,type,value,tracebackwhen an uncaught exception occurs. If no hook has been set,hookmay beNone. If any hook raises an exception derived fromRuntimeErrorthe call to the hook will be suppressed. Otherwise, the audit hook exception will be reported as unraisable andsys.excepthookwill be called.See also
The
sys.unraisablehook()function handles unraisable exceptions and thethreading.excepthook()function handles exception raised bythreading.Thread.run().
- sys.__breakpointhook__¶
- sys.__displayhook__