What is Python's Sys Module


Introduction

The sys module in Python provides valuable information about the Python interpreter. You can also use it to obtain details about the constants, functions and methods of the Python interpreter.

Getting Started

The sys module comes packaged with Python, which means you do not need to download and install it separately using the PIP package manager.

In order to start using the sys module and its various functions, you need to import it. You can do that using the below line of code,

import sys

Obtaining the list of arguments

In python, we can execute the script directly from our terminal using various arguments. Sometimes it is nice to display the user the different arguments they’ve used while executing the script or to store it for other purposes.

We can achieve this with ease using the argv function present in the sys module.

# Creating a Python script named example.py
import sys
print("You entered: ", sys.argv[1], sys.argv[2])

Now, if you run the above program through a terminal with arguments, we first change the directory to the one where the script is present and then use,

python example.py Hello World

Typing the above line in the terminal will then execute the program which will then in turn print out the arguments we’ve entered.

Output

Hello World

Note − In the above example we started with sys.argv[1] and not sys.argv[0], because sys.argv[0] prints the name of script we are currently executing and not the argument provided.

Stopping the execution of the program

If you want to force quit the application or stop it from executing at any point, we can use the exit() function within the sys module.

import sys
print(“Hello there!”)
sys.exit()
print(“This line is not even executed because the program exited in the last line”)

Output

Hello there!

In the above example, we use the sys.exit() function to stop the execution of the program, thus not printing the last line.

Obtaining the current version of Python

Wondered what version of python you are working on? Or just wanted the user to know what version of Python they are using to execute your script?

You can use the sys.version method to do so.

import sys
print(“You are currently using Python version”, sys.version)

Output

You are currently using Python version 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)]

Getting locations of all the Python modules installed

If you want to know where all your Python modules are downloaded and installed, you can use the sys.path folder.

import sys
sys.path

Reading values from the user

Input() is not the only way to read user input. You can use the sys module’s stdin function to read input as well.

import sys
data = sys.stdin.readline()
print(“You have entered −> ” + data)

The above line of code will read in the data entered until the user hits Enter and then print it back.

Conclusion

You now have a basic understanding of how and where we use the sys module in python.

There are a lot more functions in the sys module with more features and functionalities. To explore and read more about each of them, you can go through its official documentation at https://docs.python.org/3/library/sys.html.

Updated on: 11-Feb-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements