Python sys Module
sys is a module in the Python standard library that provides functions for interacting with the Python interpreter and its environment.
Through the sys library, you can access variables and functions related to the Python interpreter, such as command-line arguments, standard input/output, program exit, etc.
Importing the sys Library
Section titled “Importing the sys Library”Before using the sys library, you need to import it first. The import method is as follows:
Example
Section titled “Example”Common Features of the sys Library
Section titled “Common Features of the sys Library”1. Command-Line Arguments
Section titled “1. Command-Line Arguments”sys.argv is a list containing command-line arguments. sys.argv[0] is the script name, and subsequent elements are the arguments passed to the script.
Example Code:
Example
Section titled “Example”How to Run:
Output Result:
2. Program Exit
Section titled “2. Program Exit”sys.exit() is used to exit the program. You can pass an integer as the exit status code. Typically, 0 indicates success, and non-zero values indicate errors.
Example Code:
Example
Section titled “Example”3. Standard Input/Output
Section titled “3. Standard Input/Output”sys.stdin, sys.stdout, and sys.stderr represent standard input, standard output, and standard error streams respectively. You can redirect these streams to implement custom input/output behavior.
Example Code:
Example
Section titled “Example”4. Python Version Information
Section titled “4. Python Version Information”sys.version and sys.version_info provide the current Python interpreter’s version information.
Example Code:
Example
Section titled “Example”Output Result:
5. Module Search Path
Section titled “5. Module Search Path”sys.path is a list containing the paths that the Python interpreter searches when importing modules. You can modify this list to add custom module search paths.
Example Code:
Example
Section titled “Example”sys Module Common Attributes
Section titled “sys Module Common Attributes”| Attribute | Description |
|---|---|
sys.argv |
Command-line argument list, sys.argv[0] is the script name |
sys.path |
Python module search path (PYTHONPATH) |
sys.modules |
Dictionary of loaded modules |
sys.platform |
Operating system platform identifier (e.g., 'win32', 'linux', 'darwin') |
sys.version |
Python interpreter version information |
sys.executable |
Absolute path of the Python interpreter |
sys.stdin |
Standard input stream (file object) |
sys.stdout |
Standard output stream (file object) |
sys.stderr |
Standard error stream (file object) |
sys.byteorder |
Byte order ('little' or 'big') |
sys.maxsize |
Maximum integer value (2**31-1 or 2**63-1) |
sys Module Common Methods
Section titled “sys Module Common Methods”| Method | Description |
|---|---|
sys.exit([status]) |
Exit the program. status=0 indicates normal exit |
sys.getsizeof(obj) |
Return the memory bytes occupied by the object |
sys.getdefaultencoding() |
Get the default string encoding (usually 'utf-8') |
sys.setrecursionlimit(limit) |
Set the recursion depth limit (default 1000) |
sys.getrecursionlimit() |
Get the current recursion depth limit |
sys.getrefcount(obj) |
Return the reference count of the object |
sys.exc_info() |
Get the current exception information ((type, value, traceback)) |
sys.settrace(tracefunc) |
Set the debug tracing function |
sys.setprofile(profilefunc) |
Set the profiling function |