Python Sys Module
Last Updated : 16 Feb 2026
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.
Checking the Python Version with sys.version
Let us take an example to demonstrate how to check the python version with sys.version.
Example
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.
Importance of the sys module
There are several main points of the sys module in Python. Some of them are as follows:
- It allows us to work with functions and system-specific parameters, such as command-line arguments.
- It helps us establish control over the functions like sys.exit() and sys.getsizeof() of an interpreter.
- We can interact with the Python Runtime environment.
- Makes the debugging in the Python code comparatively easier.
Input and Output using the sys module
The program input, output, and error streams, along with getting the precise data, can be controlled by the sys module.
1. sys.stdin
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.
Example
Output:
2. sys.stdout:
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.
Example
Output:
3. sys.stderr:
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.
Example
Output:
Command-Line Arguments
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
Python sys module Example with Command-Line Arguments
Let us take an example to demonstrate the sys module with command-line arguments in Python.
Example
Output:
Total number arguments passed: 1 Name of Python script: C:\Users\mk\Desktop\import sys.py Arguments passed: 0
Understanding Other Variables in the Sys Module
Let's look at some functions which are provided by the sys module:
sys.modules
This function provides the names of the existing Python modules that have been imported.
sys.argv
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.
sys.base_exec_prefix
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.
sys.base_prefix
It is set up during Python startup, before site.py is run, to the same value as prefix.
sys.byteorder
It is an indication of the native byteorder that provides an efficient way to do something.
sys.maxsize
This function returns the largest integer of a variable.
sys.path
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.
sys.getrefcount
This function returns the reference count of an object.
sys.exit
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.
sys executable
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.
sys.platform
The value of this function is used to identify the platform on which we are working.
Conclusion
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.