Skip to content

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.

Before using the sys library, you need to import it first. The import method is as follows:

import sys
import sys
import os

# List all attributes and methods of the os module
print(dir(os))

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:

import sys

print("Script name:", sys.argv[0])
print("Argument list:", sys.argv[1:])

How to Run:

python script.py arg1 arg2

Output Result:

Script name: script.py
Argument list: ['arg1', 'arg2']

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:

import sys

print("Program starts")
sys.exit(0)
print("This line will not execute")

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:

import sys

# Redirect standard output to a file
with open('output.txt', 'w') as f:
    sys.stdout = f
    print("This content will be written to output.txt")

# Restore standard output
sys.stdout = sys.__stdout__
print("This content will be displayed on the console")

sys.version and sys.version_info provide the current Python interpreter’s version information.

Example Code:

import sys

print("Python version:", sys.version)
print("Version info:", sys.version_info)

Output Result:

<code>Python version: 3.9.7 (default, Aug 31 2021, 13:28:12) 
[GCC 7.5.0]
Version info: sys.version_info(major=3, minor=9, micro=7, releaselevel=&#39;final&#39;, serial=0)
</code>

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:

import sys

print("Module search path:", sys.path)
sys.path.append('/custom/path')
print("Updated module search path:", sys.path)

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)

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