Python sys module tutorial | sys module in python 3

The function of the sys module allows us to operate interpreter of being window platform. The sys module is same as that of other modules.  This module is been imported i.e. sys module provides information about the constants and the functions, methods. Another is to help the function as the help () and you will learn some important features of this module here. The sys module contains the system specific functionality. We have already seen sys.argy command line argument and are used to display warnings to the end-user. If the Python version is not at least 3, we display a corresponding warning. If we want to check version of the python command being used so that we want to ensure that we are using 3 versions. We use the module from the standard library which is called warnings. The parameters and the functions of the sys module are as follows,
sys.argv
sys.executable
sys.exit
sys.modules
sys.path
sys.platform
sys.stdin/stdout/stderr
Before running any function we need to import using below command as,
e,g:-
Import sys

sys.argv:-

This will return a list of commands line arguments passed to the python script.
The index 0 in this always name of the script.
e.g:-

Import sys
sys.argv 
[''] 

O/p:-

['sysargv.py', '-v', 'somefile.py']

sys.exit:-

It will cause the script to exit back to either python console or the command prompt.They are used safely exit from program in case of exception.
It will return the highest value.
e.g:-

 Import sys
 sys.exit(0) 
 Traceback (most recent call last)
 File "<pyshell#5>", line 1, in <module>
 sys.exit(0) 
 SystemExit: 0

O/p:-

python sys module tutorial

sys.executable:-

It is the absolute path of to the python interpreter and is useful when you are someone else.
On some system the command will fail and will return an empty string .
e.g:-

import sys
 sys.executable 
'C:\\Python27\\pythonw.exe' 

sys.path:-

  The path values are the list of strings and specify the search path for the modules.
The python documentation sys path initialize from environments variable called PYTHONPATH.
This will tell the python what location to look and when try to import a module.

sys.version:-

This attribute displays a string containing the version number of the current Python interpreter.
Sys version
e.g:-
3.7.0(v3.7.0:f59c0932b4, mar 28 2019)

Command-line Arguments in Python sys Module:-

We can store the command-line arguments we pass to a script in a list we call sys.argv.
The first item of this is the name of the script; the arguments are next. Save the following code in a script.
Import sys
print (sys.argv)
for i in range(len(sys.argv)):
if i==0:
print("The function is",sys.argv[0])
else: print("Argument:",sys.argv[i])

Changing the Output Behavior of the Shell:-

We’ve so often used the Python shell interactively and even as a calculator. But what if we wanted to change how it delivers the output as Well,we simply rebind sys.displayhook to a callable object.
import sys
x=42

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India