The sys
module provides methods that help work with different sections of the runtime environment of Python. This module helps interact with the Python interpreter with the help of many functions. Before using any of the functions, the sys
module has to be imported.
import sys
The following functionalities from the sys module will be covered in this post:
sys.argv
sys.maxsize
sys.path
sys.version
sys.exit
1. sys.argv
The argv
method returns the command line arguments in the form of a list that had been passed to the Python script/code. Here it is important to note that the first item in the list returned (indexed 0) refers to the name of the script. The other arguments are present in the subsequent index of the list.
In the example below, the name of the script was test.py,
Time for an example:
import sys
print("Hello {}. Welcome to {} tutorial at {}".format(sys.argv[1], sys.argv[2], sys.argv[3]))
Save this file with a name and extension as .py. Open the command prompt and enter the following line:
python test.py Students Python Studytonight
If your command line isn't open in the tab where the code has been saved, you will be required to give the entire absolute path of the test.py file.
In the above command, Students is the first argument passed to the argv
function. On similar lines, Python and Studytonight are the second and third arguments passed to the argv
function.
As mentioned earlier, the 0th parameter is the name of the Python script, which the command line automatically identifies from the command.
Output:
Hello Students. Welcome to Python tutorial at Studytonight
2. sys.maxsize
This function returns an integer with maximum value a variable of type Py_ssize_t
can take in a Python script. This value returned depends on the platform where it is run. On a 32 bit platform, the value is usually 2**31 - 1 (2147483647), whereas, on a 64-bit platform, the value is 2**63 - 1 (9223372036854775807).
Time for an example:
import sys
print(sys.maxsize)
Output:
9223372036854775807
3. sys.path
The path
method from the sys module gives all the search paths of the Python modules. It gives output in the form of a list of strings.
It is similar to looking at the PYTHONPATH in the environment variables of the control panel. In the output, the first element in the list indicates the directory wherein the script(currently being executed) was saved, that was used to invoke the Python interpreter. In case of non-availability of the directory, the first element in the list is empty.
This happens if the script was invoked interactively(using IDLE) or it was read from the standard input(using exec
statement). This list can further be modified as per requirements. Only strings and bytes should be added (other types will be ignored before it is imported).
Time for an example:
import sys
print(sys.path)
Output:
['C:\\Users\\user_name, 'C:\\Users\\ user_name \\Anaconda3\\pkgs\\nb_anacondacloud-1.2.0-py35_0\\Lib\\site-packages\\nb_anacondacloud-1.2.0-py3.5.egg-info', 'C:\\Users\\ user_name \\AppData\\Local\\Programs\\Python\\Python36\\python.exe', 'C:\\ProgramData\\Anaconda3\\DLLs', 'C:\\ProgramData\\Anaconda3\\lib', 'C:\\ProgramData\\Anaconda3', '', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\ user_name \\.ipython']
4. sys.version
This method gives the version of the Python interpreter that is currently being used. In addition to this, it gives information about the build number and the compiler that is used. This information is displayed in the form of a string whenever the interactive interpreter has been started. It is strongly suggested that the version_info
method be used to get the version information rather than extract it from the output of sys.version
.
Time for an example:
import sys
print(sys.version)
Output:
'3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]'
5. sys.exit
As the name suggests, the exit
method exits out of the Python program or that specific process. It is used to exit safely out of the program in case an exception is generated. The sys.exit
raises a SystemExit
exception under the hood. This is similar to the exit
or quit
in Python.
Since the SystemExit
is raised, the cleanup functions specified by finally
block of the try
statement is also executed.
This method can have an optional arg as an argument. This argument has a default value of 0, wherein the 0 indicates that the program terminated normally. If any value other than 0 is present, it indicates abnormal termination of the program.
Time for an example:
import sys
sys.exit()
sys.exit(0)
Conclusion:
In this post, we saw a few basic methods from the sys module that help users interact with the Python runtime environment.
You may also like: