Skip to main content

sys

The sys module exposes the running interpreter: command line, search path, streams, hooks, recursion limits, exception handling, GC and performance counters. Almost every CPython internal that needs a Python-visible knob surfaces it here.

Source-of-record: Python/sysmodule.c, sys docs.

Streams and command line

NameTypeNotes
sys.argvlist[str]Script name + args.
sys.stdin / stdout / stderrTextIOWrapperLine-buffered by default.
sys.__stdin__ / __stdout__ / __stderr__originalsSnapshot at interpreter start.
sys.pathlist[str]Module search path.
sys.path_hookslist[callable]Path-entry finders.
sys.path_importer_cachedictMemoised path importers.
sys.modulesdict[str, Module]Import cache.
sys.meta_pathlist[MetaPathFinder]Top-of-import dispatcher.
sys.builtin_module_namestuple[str, ...]Names compiled into the interpreter.
sys.stdlib_module_namesfrozenset[str]Stdlib module names (PEP 643).

Version and platform

NameTypeValue
sys.versionstrHuman-readable version string.
sys.version_infosys.version_info(major, minor, micro, releaselevel, serial).
sys.hexversionintPacked version number.
sys.implementationSimpleNamespace(name, version, hexversion, cache_tag).
sys.platformstr'linux', 'darwin', 'win32', ...
sys.maxsizeintLargest Py_ssize_t.
sys.maxunicodeint0x10FFFF.
sys.byteorderstr'little' or 'big'.
sys.float_infonamed tupleDBL_MAX, epsilon, etc.
sys.int_infonamed tupleInternal int representation info.
sys.hash_infonamed tupleHash algorithm, width, modulus.
sys.thread_infonamed tupleThread implementation info.
sys.flagsnamed tupleCommand-line flag values.

Hooks

FunctionRole
sys.excepthook(type, value, traceback)Called on unhandled top-level exception.
sys.unraisablehook(args)Called when an exception is logged unraisable.
sys.displayhook(value)REPL output.
sys.audit(event, *args)Emit audit event (PEP 578).
sys.addaudithook(hook)Register audit hook.
sys.breakpointhook(*args, **kw)Backend for the breakpoint() builtin.
sys.exit(code=None)Raise SystemExit(code).
sys.exception()Currently handled exception, or None.
sys.exc_info()(type, value, traceback) of current exception.
sys.last_type / last_value / last_traceback / last_excMost recent unhandled exception in REPL.

Runtime control

FunctionEffect
sys.setrecursionlimit(n) / getrecursionlimit()Frame depth cap.
sys.setswitchinterval(n) / getswitchinterval()GIL switch interval in seconds.
sys.settrace(fn) / gettrace()Per-frame trace function.
sys.setprofile(fn) / getprofile()Per-call profile function.
sys.monitoringPEP 669 monitoring API namespace.
sys.set_int_max_str_digits(n) / get_int_max_str_digits()int/str length cap.
sys.set_coroutine_origin_tracking_depth(n)Coroutine origin tracking depth.
sys.set_asyncgen_hooks(firstiter=, finalizer=)Async generator hooks.
sys.intern(s)Intern a string.
sys.getsizeof(obj, default=...)Memory footprint of obj.
sys.getrefcount(obj)Reference count incl. temp.
sys._getframe(depth=0)Internal: caller frame.
sys._current_frames()Frame per thread.
sys._current_exceptions()Active exception per thread.
sys.is_finalizing()True during interpreter shutdown.

Filesystem encoding

NameValue
sys.getfilesystemencoding()Encoding used for paths ('utf-8' on POSIX).
sys.getfilesystemencodeerrors()Error handler ('surrogateescape').
sys.getdefaultencoding()'utf-8'.

Interpreter shutdown

sys.exit(code) raises SystemExit. Cleanup runs atexit callbacks in LIFO order, then finalises GC. sys.is_finalizing() reports the finalisation phase.

Gopy status

AreaState
Streams, command line, pathsComplete.
Version / platform / impl infoComplete; cache_tag is gopy-NNN.
Hooks (excepthook, audit, display)Complete.
settrace/setprofileComplete.
sys.monitoring (PEP 669)Complete.
setrecursionlimitComplete; enforced by frame depth check.
set_int_max_str_digitsComplete.

Reference

  • CPython 3.14: sys module.
  • Python/sysmodule.c. C side.
  • module/sys/. gopy port.