Python

What is Python SYS module

Python sys module has several methods and variables that can alter various aspects of the Python runtime environment. It permits operating on the interpreter by giving access and information about the constants, variables, and functions that have a strong interaction with the interpreter.

In this post, you will discuss some of the essential features of the Python sys module. For the demonstration of Python sys module examples, you should have Python installed on your Ubuntu system. If you do not have it, then install it by following the method outlined below.

How to install Python on Ubuntu

To install Python on your system, firstly check out if it is already available in your distribution by utilizing this command:

/usr/bin/python3

The output declares that “python3” executable is present in our “/usr/bin/python3/”:

The next step is to update your system repositories before starting the Python installation:

$ sudo apt-get update

Now, execute the below-given command for the installation of Python on your system:

$ sudo apt-get install python3.7 python3-pip

The error-free output declares that Python now exists on your system. Now move forward to understand some Python important sys modules:

How to use sys.argv in Python

A list of command-line arguments provided to a Python script is returned by “sys.argv“. The index 0 will save the script name. The remaining arguments are saved at the index 1, 2,3, and so on. For instance, in the below-given Python script, we will utilize the sys.argv function to take three arguments from the terminal at the time of execution. However, before calling the “sys.argv” function, we have to import the Python sys module in our Python script:

import sys
print("The values are: ",sys.argv[1], sys.argv[2], sys.argv[3])

Save this Python script by clicking the “Save” button present at the right side of the title bar:

We will save our file as “testfile.py”, where “.py” is the extension indicating that “testfile” is a Python script:

Execute the “testfile.py” python script in your terminal with three values:

$ python3 testfile.py Ubuntu CentOS Windows

The “testfile.py” stores the first argument “Ubuntu” in sys.argv[1], “CentOS” in sys.argv[2], “Windows” in sys.argv[3]. At this moment, you should be wondering about the sys.argv[0]? sys.argv[0] stores the Python script name in it.

The output of “testfile.py” will print out the arguments stored in the sys.argv[1], sys.argv[2], sys.argv[3]:

How to use sys.version in Python

The “sys.version” attribute lets you know about the version of your current Python interpreter. Now, save the below-given code in your “testfile.py” script, and execute this Python script in your terminal:

import sys
print(sys.version)

$ python3 testfile.py

You can check the version of your current Python interpreter.

How to use sys.stdin in Python

In a Python script, “sys.stdin” is utilized to extract input from the terminal. It uses the input() function internally. “sys.stdin” also adds a ‘\n’ at the end of each sentence.

Now, we will show a practical example in which we will take input from the user using the stdin. After that, the Python script will check if the user entered “e“; it will print out “exiting”. In the other case, it will print out the user input until “e” is not given as input:

import sys
for line in sys.stdin:
    if 'e' == line.rstrip():
        break
    print(f'Input is: {line}')
print("Exiting...")

Save the above-given code in your “testfile.py” and execute it in your Ubuntu terminal:

$ python3 testfile.py

At first, we have entered “linuxhint” as input; in return, the Python script prints it out on our terminal. However, entering “e” terminated the program:

How to use sys.stdout in Python

Utilize the “sys.stdout” command for sending any output directly to the terminal screen in your Python script. The output from a print statement, an expression statement, or even a prompt direct for input might take many different forms. For instance, in the below-given example, we will “sys.stdout” command to write “linuxhint” on the terminal:

import sys
sys.stdout.write('linuxhint \n')

Now, execute this Python script by utilizing the below-given command:

$ python3 testfile.py

How to use sys.stderr in Python

A Python script handles exceptions by using the “stderr“. Whenever an exception occurs in Python, write it to the “sys.stderr”. To understand the working of “stderr“, write out the below-given code in your “testfile.py” Python script:

import sys
def print_to_stderr(*b):
    # Here b is the array having the objects
    # Now pass b as the argument of the function
    print(*b, file = sys.stderr)
print_to_stderr("linuxhint.com")

We have defined a “print_to_stderr” function in our script, which contains an array of objects. This function will store exceptions in it, and by using the “print” function, we can print the exception related information on our terminal:

$ python3 testfile.py

How to use sys.path in Python

The sys.path finds the Python modules paths and then stores them in itself. It is also utilized for showing the current system’s PYTHONPATH. For instance, in our “testfile.py”, we will add the “sys.path” variable to show our system PYTHONPATH:

import sys
print(sys.path)

$ python3 testfile.py

Execution of the “testfile.py” Python script will now show you the following output:

How to use sys.copyright in Python

The sys.copyright provides you all copyright information of your current Python version. To display the retrieved information of “sys.copyright”, add the following code in your “testfile.py”:

import sys
print(sys.copyright)

Now, save this “testfile.py” and execute it in your terminal:

$ python3 testfile.py

How to use sys.module in Python

The Python module name that the current shell imported is returned by “sys.module”. You can check out imported modules on your terminal by adding the following lines in the “testfile.py” Python script:

import sys
print(sys.modules)

$ python3 testfile.py

How to use sys.executable in Python

You can use the “sys.executable” to know where Python is installed in your system. To do so, write out these lines in your “testfile.py” script and save it:

import sys
print(sys.executable)

$ python3 testfile.py

The output declares that on our system, we have installed Python in the “usr/bin/python” directory:

How to use sys.platform in Python

The sys.platform informs you about the platform on which you are currently working. To know about your platform, add the following lines in the “testfile.py” script:

import sys
print(sys.platform)

Add the above-given code in your “testfile.py” and save this script:

$ python3 testfile.py

The output will show you the name of your platform:

Conclusion

The Python sys module contains several methods and variables for manipulating various aspects of the Python runtime environment. It permits us to access system-specific parameters and functions. This article provided you useful information related to some Python sys modules, including sys.argv, sys.platform, sys.modules, sys.executable, sys.stdin, sys.stdout, sys.stderr, sys.path, sys.copyright, and sys.version. Moreover, we have also demonstrated examples of the mentioned python sys modules.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.