sys Module in Python with Examples

In this tutorial, we’ll be learning about the sys Module in Python and the main functions provided by this module that are commonly used by many programmers. This Module provides us the ability to interact with the Python interpreter on any platform using some variables and functions and because this is an in-built Module, we do not require to install it. Let us start this tutorial by importing the module.

Importing sys Module in Python

As we know, before using functions and variables provided by any module we need to import it using import keyword.

import sys

sys.version in Python

This returns a string that gives information about the version of the Python interpreter, the Build no. and the compiler used.

print(sys.version)
3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)]

sys.version_info in Python

This will return a tuple that contains the information about the Python Version only.

print(sys.version_info)
sys.version_info(major=3, minor=7, micro=4, releaselevel='final', serial=0)

sys.getwindowsversion in Python

This is a function that returns a tuple that contains the Windows version used to run the program.

print(sys.getwindowsversion())
sys.getwindowsversion(major=10, minor=0, build=18362, platform=2, service_pack='')

sys.copyright in Python

This returns the Copyright © information about the Python Interpreter.

print(sys.copyright)
Copyright (c) 2001-2019 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.

sys.getfilesystemencoding in Python

This is a function that returns the name of the encoding used for conversion between the Unicodes filenames to bytes filenames and the Unicode Transformation Format – 8-bit(UTF-8) is the widely used encoding on any platform.

print(sys.getfilesystemencoding())
utf-8

sys.executable in Python

This returns a string that contains the absolute path of Python Interpreter Executable Binary on the system.

print(sys.executable)
C:\ProgramData\Anaconda3\python.exe

sys.path in Python

This returns a list that contains the search path for the modules. It means that whenever we import any module, then the order of paths to search for its location is specified here. Path[0] will contain the current directory in which the current script is present.

print(sys.path)
['D:\\VS_code_workspace', 'C:\\ProgramData\\Anaconda3\\python37.zip', 'C:\\ProgramData\\Anaconda3\\DLLs', 'C:\\ProgramData\\Anaconda3\\lib', 'C:\\ProgramData\\Anaconda3', 'C:\\Users\\kunal\\AppData\\Roaming\\Python\\Python37\\site-packages', 'C:\\Users\\kunal\\AppData\\Roaming\\Python\\Python37\\site-packages\\picamera-1.13-py3.7.egg', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin']

sys.stdin in Python

This function will take something from the user means it takes input from users like input() function. It is the standard way for interactive input in Python. Let us see the use of this function and compare it with input() function.

a = sys.stdin
print("You write: ",a.readline())
print("This is the msg after newline")
CodeSpeedy Technologies
You write:  CodeSpeedy Technologies

This is the msg after newline

Let us execute the same code using input() function

b = input()
print("You write: ",b)
print("This is the msg after newline")
CodeSpeedy Technologies
You write:  CodeSpeedy Technologies
This is the msg after newline

In both the code above,  we see that sys.stdin will automatically append a newline after the input string while input() function not.

sys.stdout in Python

This function will write something on the console or to the file. It is the standard way for output in Python. Let us see the use of this function and compare it with print() function. It will not do any formatting to the output text string, unlike the print() function which automatically inserts whitespace between each argument and a newline at the end.

c = input()
sys.stdout.write(f"You write: {c}")
print("This is the msg after newline")
CodeSpeedy Technologies
You write: CodeSpeedy TechnologiesThis is the msg after newline

sys.argv in Python

A widely used function used to take the arguments from the command line. We can learn more about this function from this tutorial – Command line arguments in Python.

We hope you like this tutorial and if you have any doubts, feel free to ask in the comment section below.

You may like to read.

Literals in Python with examples

Statistics Module in Python with Examples

OS Module in Python

Leave a Reply

Your email address will not be published. Required fields are marked *