Sys Module
Python - sys Module
Section titled “Python - sys Module”The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. You will learn some of the important features of this module here.
sys.argv
Section titled “sys.argv”sys.argv returns a list of command line arguments passed to a Python script. The item at index 0 in this list is always the name of the script. The rest of the arguments are stored at the subsequent indices.
sys.argv
Here is a Python script (test.py) consuming two arguments from the command line.
import sys
print("You entered: ",sys.argv[1], sys.argv[2], sys.argv[3])`import sys
print(“You entered: “,sys.argv[1], sys.argv[2], sys.argv[3])` This script is executed from command line as follows:
Above, sys.argv[1] contains the first argument ‘Python’, sys.argv[2] contains the second argument ‘Python’, and sys.argv[3] contains the third argument ‘Java’.sys.argv[0] contains the script file name test.py.
sys.argv[1]``sys.argv[2]``sys.argv[3]``sys.argv[0]``test.py
sys.exit
Section titled “sys.exit”This causes the script to exit back to either the Python console or the command prompt. This is generally used to safely exit from the program in case of generation of an exception.
sys.maxsize
Section titled “sys.maxsize”Returns the largest integer a variable can take.
import sys
print(sys.maxsize) #output: 9223372036854775807`import sys
print(sys.maxsize) #output: 9223372036854775807`Try it
sys.path
Section titled “sys.path”This is an environment variable that is a search path for all Python modules.
import sys
print(sys.path)`import sys
print(sys.path)`Try it
sys.version
Section titled “sys.version”This attribute displays a string containing the version number of the current Python interpreter.
import sys
print(sys.version)`import sys
print(sys.version)`Try it Learn more about the sys module in Python docs. sys module in Python docs