The Python sys module provides functions and variables that are used to manipulate different parts of the Python Runtime Environment. It lets us access system-specific parameters and functions.
First, we have to import the sys module in our program before running any functions.
Let us take an example to demonstrate how to check the python version with sys.version.
Output:
3.12.11 (main, Jun 4 2025, 08:56:18) [GCC 11.4.0]
Explanation:
The above code prints the value of the version of the Python Interpreter that is currently in use.
There are several main points of the sys module in Python. Some of them are as follows:
The program input, output, and error streams, along with getting the precise data, can be controlled by the sys module.
It is an object that contains the original values of stdin at the start of the program and is used during finalization. It can restore the files. stdin means standard input.
Output:
Exit
The sys.stdout is a built-in file object that allows writing the output to the standard output stream. At the same time, it also allows low-level control over the printed output.
Output:
TpointTech 10
sys.stderr is a built-in file object that is used to send messages to the standard error stream; it keeps the error messages separate from the regular program output.
Output:
Welcome to TpoinTech
Command-line arguments are a type of argument that we pass when calling a statement when calling a program. To do this, the sys module gives a variable called sys.argv
Let us take an example to demonstrate the sys module with command-line arguments in Python.
Output:
Total number arguments passed: 1 Name of Python script: C:\Users\mk\Desktop\import sys.py Arguments passed: 0
Let's look at some functions which are provided by the sys module:
This function provides the names of the existing Python modules that have been imported.
This function returns a list of command-line arguments passed to a Python script. The name of the script is always the item at index 0, and the rest of the arguments are stored at subsequent indices.
This function provides an efficient way to the same value as exec_prefix. If not running a virtual environment, the value will remain the same.
It is set up during Python startup, before site.py is run, to the same value as prefix.
It is an indication of the native byteorder that provides an efficient way to do something.
This function returns the largest integer of a variable.
This function shows the PYTHONPATH set in the current system. It is an environment variable that is a search path for all the Python modules.
This function returns the reference count of an object.
This function is used to exit from either the Python console or command prompt, and is also used to exit from the program in case of an exception.
The value of this function is the absolute path to a Python interpreter. It is useful for knowing where Python is installed on someone else's machine.
The value of this function is used to identify the platform on which we are working.
The Python sys module provides functions and variables that are used to manipulate different parts of the Python Runtime Environment. We have studied about Input and Output using Sys. We also looked at the example of Command-line arguments and some more variables in the Sys module, such as: sys.argv, sys.base_exec_prefix, sys.base_prefix, sys.byteorder, sys.maxsize, sys.path, sys.exit, etc.
We request you to subscribe our newsletter for upcoming updates.