Python sys module provides easy functions that allow us to interact with the interpreter directly.

Python sys module

The functions python sys module provides allows us to operate on underlying interpreter, irrespective of it being a Windows Platform, Macintosh or Linux. In this lesson, we will review these functions and what we can do with these.

Python import sys

Let us start our journey with these functions and what information they offer. Please note that before running any functions, we need to import it using below command.


import sys

Python sys.modules

This function gives the names of the existing python modules current shell has imported. Let’s execute this on the system:


>>> sys.modules.keys()
dict_keys(['builtins', 'sys', '_frozen_importlib', '_imp'])

I have removed many modules as initially Python imports many modules by default. So the output may differ when you will execute this command in your python setup.

Python sys.argv

This function collects the String arguments passed to the python script. Let’s execute this on the system by making a script:


import sys
print('The command line arguments are:')
for i in sys.argv:
    print(i)

Run this script on terminal now:

python-sys_argv_output

Python sys.path

This function just displays the PYTHONPATH set in current system. Let’s execute this on the system by making a script:


import sys
print('nnThe PYTHONPATH is', sys.path, '.n')

Run this script on terminal now:

python-sys_argv_output

Python sys.stdin

This function is used to take . Let’s execute this on the system by making a script:


import sys
user_input = sys.stdin.readline()
print("Input : " + user_input)

Run this script on terminal now:

python-sys_argv_output

This is probably the most commonly used function in sys module as it is the standard way of taking input from user.

Python sys.copyright

This String just displays the copyright information on currently installed Python version. Let’s execute this on the system by making a script:


import sys
print(sys.copyright)

Run this script on terminal now:

python_sys_copyright

Python sys.exit

This method makes the Python interpretor exits the current flow of execution abruptly. Let’s execute this on the system by making a script:


import sys
print("JournalDev")
sys.exit(1)
print("Hello")

Run this script on terminal now:

functools-partial-documented-1

Python sys.getrefcount

This python sys module method returns the count for references to an object where it is used. Python keeps track of this value, as, when this value reaches 0 in a program, the memory for this variable is cleaned up. Let’s execute this on the system by making a script:


import sys
variable = "JournalDev"
print(sys.getrefcount(0))
print(sys.getrefcount(variable))
print(sys.getrefcount(None))

Run this script on terminal now:

python_sys-getrefcount

In this lesson, we learned about various functions provided by sys module in Python and saw how they work. See more lessons on Python here.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: