30.1. 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.
-
sys.abiflags¶ On POSIX systems where Python was built with the standard
configurescript, this contains the ABI flags as specified by PEP 3149.3.2 版新加入.
-
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.
-
sys.base_exec_prefix¶ Set during Python startup, before
site.pyis run, to the same value asexec_prefix. If not running in a virtual environment, the values will stay the same; ifsite.pyfinds that a virtual environment is in use, the values ofprefixandexec_prefixwill be changed to point to the virtual environment, whereasbase_prefixandbase_exec_prefixwill remain pointing to the base Python installation (the one which the virtual environment was created from).3.3 版新加入.
-
sys.base_prefix¶ Set during Python startup, before
site.pyis run, to the same value asprefix. If not running in a virtual environment, the values will stay the same; ifsite.pyfinds that a virtual environment is in use, the values ofprefixandexec_prefixwill be changed to point to the virtual environment, whereasbase_prefixandbase_exec_prefixwill remain pointing to the base Python installation (the one which the virtual environment was created from).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 giving 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.)
-
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 some other code.
-
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.
-
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.
-
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.3.7 版新加入.
-
sys._debugmallocstats()¶ Print low-level information to stderr about the state of CPython’s memory allocator.
If Python is configured –with-pydebug, it also performs some expensive internal consistency checks.
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.
