sys --- 系統特定的參數與函式¶
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.abiflags¶
On POSIX systems where Python was built with the standard
configurescript, this contains the ABI flags as specified by PEP 3149.在 3.2 版被加入.
在 3.8 版的變更: Default flags became an empty string (
mflag for pymalloc has been removed).可用性: 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.呼叫
sys.addaudithook()本身會引發一個不帶任何引數、名為sys.addaudithook的稽核事件。如果任何現有的 hook 引發從RuntimeError衍生的例外,則不會添加新的 hook 並抑制異常。因此,除非呼叫者控制所有已存在的 hook,他們不能假設他們的 hook 已被添加。所有會被 CPython 所引發的事件請參考稽核事件總表、設計相關討論請見 PEP 578。
在 3.8 版被加入.
在 3.8.1 版的變更: Exceptions derived from
Exceptionbut notRuntimeErrorare no longer suppressed.CPython 實作細節: 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.另請參閱
sys.orig_argv。備註
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.
舉例來說,一個名為
os.chdir的稽核事件擁有一個引數 path,其內容為所要求的新工作目錄。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.所有會被 CPython 所引發的事件請參考稽核事件總表。
在 3.8 版被加入.
- sys.base_exec_prefix¶
Equivalent to
exec_prefix, but referring to the base Python installation.When running under 虛擬環境,
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 虛擬環境 for more information.在 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 虛擬環境 for more information.在 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.)另請參閱
sys.stdlib_module_names清單。
- 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.