- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Python's Sys Module
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.
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,
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, sys.argv)
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.
Note − In the above example we started with sys.argv and not sys.argv, because sys.argv 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”)
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)
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.
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.
- What is Python's OS Module
- What is calendar module in python?
- What is Python module for date manipulation?
- What is Module Testing? (Definition, Examples)
- What is the difference between a python module and a python package?
- What is Module System in Java 9?
- Python getpass Module
- What is an unnamed module in Java 9?
- What is the associativity of Python's ** operator?
- What is S-Box Substitution?
- Fraction module in Python
- Import module in Python
- Keyboard module in Python
- colorsys module in Python
- struct module in Python