Python sys Module Overview
Explore the sys
module in Python, which provides functions and variables for manipulating the Python runtime environment. Learn about sys.argv
for accessing command line arguments passed to a script, with an example demonstrating how to retrieve and print these arguments.
Python - sys Module
The sys module in Python provides functions and variables to manipulate different parts of the Python runtime environment. Below are some of its important features:
sys.argv
sys.argv
returns a list of command line arguments passed to a Python script. Index 0 in this list is always the script name, and subsequent indices store the arguments.
Example
import sys
print("You entered: ", sys.argv[1], sys.argv[2], sys.argv[3])
Output
You entered: Python C# Java
In the above example, if executed with arguments Python C# Java
, sys.argv[1]
would be 'Python', sys.argv[2]
would be 'C#', and sys.argv[3]
would be 'Java'.
sys.exit
sys.exit
causes a script to exit to the Python console or command prompt. It's used to terminate the program safely, especially in case of exceptions.
sys.maxsize
sys.maxsize
returns the largest integer variable can take.
Example: sys.maxsize
import sys
print(sys.maxsize) # Output: 9223372036854775807
sys.path
sys.path
is an environment variable that provides a search path for all Python modules.
Example: sys.path
import sys
print(sys.path)
sys.version
sys.version
displays the version number of the current Python interpreter.
Example: sys.version
import sys
print(sys.version)