Working with the Python system using the sys library - ProgrammerTech


Working with the Python system using the sys library

  • Share this:
Working with the Python system using the sys library

Python system library sys module

The sys library is the system library, which is an abbreviation of the word System. It specializes and is concerned with the functions of the system and gives many possibilities for the environment that Python runs on, and the sys module deals with stdout, stdin, stderr and argv inputs.


Finding the Python version using the sys library

To find out the version of Python in our system, we write the following command in the shell or any editor we use for Python.

>>> import sys
>>> print("Python version", sys.version)
Python version 3.9.2 (default, Feb 28 2021, 17:03:44)

We notice that when we run the code, the current Python version I'm working with is printed to us, which is 3.9.2.


Learn information about establishing Python

To find out some information about Python, i.e. Copyright, through the sys library, such as information about establishing Python and other special information.

>>> import sys
>>> print(sys.copyright)

When we press the Enter button, it will give us the result, which is some information about Python in the form.

Copyright (c) 2001-2021 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000
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.

As we noticed he gave us some information about Python in general.


Knowledge of the current working platform by sys library

There is a good sys library, which is the platform, and its task is to display the working platform that we are working on in Python, and to write its command in the form.

>>> import sys
>>> print(sys.platform)

And when I hit the Enter button, it will give me the result.


As we noted, it gave me the result Linux because I use the Linux system in my computer, if your computer has a Windows system, the result will be win32 or win64 depending on the computer kernel.


Display the current file name with the sys library

To find out the name of the file I am working on at the moment in Python we use argv and the result will give us the name of the file as I named it to start the work.

>>> import sys
>>> print(sys.argv)

When we press Enter it will give us the name of the file placed in an array for example or if we are working on the shell directly it will give us an array with empty content [' '].


View all Python paths on PC

We use the path method in displaying Python paths. This method enables us to find out all the private paths in Python on the current computer.

>>> import sys
>>> print(sys.path)

When we press Enter, it will show us the Python paths on the computer.


View output by stdout.write sys library

Through the Python system library, we can read any text and pass this text via stdout.write as an output and it contains variables and methods that interact with and control the compiler as well, and stdout is used to display the output directly to the screen console and the output can be in any form and can be Take it out from the print statement.

>>>import sys
>>>sys.stdout.write("hello world")
'hello world11'

In this example, we told the shell to read to us the text str that would be printed to us on the screen, which is the word hello world, and it actually printed it for us and printed next to it the number 11, and this number means the number of letters of the word with the space as well.


View operating system errors by stderr.write

We use stderr.write in the sys library to display errors.

>>>import sys

We told him to print an error on the screen, and the error was printed in red with a number 5 next to it, the number of characters.


Read user-entered content

The method is used to print input from the user and we can pass an integer value specifying the length of the string.

Example 1

>>>import sys

This means reading the content entered by the user, and any word from five digits must be entered, because we have specified a number 5 for it. If we write something less than 5 digits, it will still ask for the rest of the letters. From five letters, he will take only the first five letters.

Example 2

import sys
ilen = len(sys.argv)
if ilen != 1 :
    print("no Inputs")

When we run this file, it will print no Inputs for us. This means that we did not enter any word in this file. This is why no Inputs was printed for us.

Example 3

import sys
ilen = len(sys.argv)
if ilen != 1 :
    name = sys.argv[1]
    age = sys.argv[2]
    print("no Inputs")

He will ask us for two inputs, namely the name and age, when we write it to him he will print it on the screen.

To know all the methods that are used in the sys library because they are too many that we cannot explain in one or two lessons, to know all the methods we write the command.

>>> import sys
>>> print(dir.(sys))

When pressing the Enter button, it will display all the methods used in the sys library, and we can search for a way to use any method we want.

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.