Python sys module - functions, variables

Python "sys" module has variety of functions and variables. Generally, it deals with interpreter and runtime environment.

Python has variety of versions. when you want to find the version of python, you can use the following command in command prompt.


If you want to write the python code to find the version and its information, then the below program used to do it.

#First, import an package “sys”.

import sys

#Next, use print function to print the message “Python version of your system”

print(“Python version of your system”)

#Here,the code to print the python version

print(sys.version)

#Again, use print function to print the message “Your system’s version information”

print(“Your system’s version information”)

#The code for print version information

print(sys.version_info)

Save this program into a file “sysver.py”.

Run this file in command prompt by following way. You will get the below output.

Some of the functions in sys module is given below.

sys.setrecursionlimit():

It deals with interpreter stack. It is used to set the depth of the stack. Usually, it has the limit of 1000. But using this function, you can set it more.

sys.settrace(tracefunc) :

 This inbuilt function is used for debuggers. The parameter “tracefunc” accepts three arguments. One is for stack frame. second one is for the purpose(function call,function return, exceptions or opcode). Third one is for event type.

sys.maxsize() :

It displays the maximum size of integer value.

sys.path() :

It displays the system paths.

sys.modules() :

It displays the system modules.

sys.argv :

It reads the command line arguments.

sys.stdin :

It is used to read the data from command line or input stream.

sys.stdout:

It is standard output stream which is used to write the output in output screen or any output device.

sys.stderr:

It writes the error messages or warnings.

sys.exit() :

It  exits the python environment.

These the inbuilt functions available in sys module in python.

No comments:

Post a Comment