SYS MODULE IN PYTHON

Monisha Kumar
3 min readMay 18, 2022

--

What is Python sys Module?

→ Sys module in Python basically provides access to some variables used or maintained by the interpreter and some functions that interact strongly with the interpreter.

→ It is always available in the code library.

→ sys.modules return the name of the Python modules that the current shell has imported.

Sys Module Function

→ It provides information about constants, functions, and methods of the interpreter. But another possibility is the help() function.

→ Using help(sys) provides valuable detailed information about the interpreter and function.

Input and Output using sys:

→This module provides variable for better control over input and output.

→They using 3 variable in sys module are stdin, stdout, stderr.

stdin:

→ It is nothing but to get a input from command line directly.

→ It internally calls the input() method.

stdout:

→ It is used to display output directly to the screen console. Whenever a print function is called within the code, it is first written and then finally on the screen.

syntax: sys.stdout

stderr:

→ Whenever an exception occurs in Python it is written.

Syntax: sys.stderr

Basic Code of sys() in Python

Command Line Argument :

→ Command-line arguments are those which are passed the calling of the program along with the calling statement.

→ Sys module provides a variable called sys.argv.

It’s main purpose are:

  • It is a list of command-line arguments.
  • len(sys.argv) provides the number of command-line arguments.
  • sys.argv[0] is the name of the current Python script.

Exiting the Program :

→ This causes the program to exit in a runtime freeing the space occupied.

sys.exit([arg]) can be used to exit the program.

Reference Count:

sys.getrefcount() is used to get the reference count for any given object.

→ When this value becomes 0, the memory for that particular value is deleted.

Working with Modules:

sys.path is a built-in variable within the sys module. Its returns the list of directories that the interpreter will search for the required module.

sys.setrecursionlimit() method is used to set the maximum depth of the Python interpreter stack to the required limit.

sys.maxsize() fetches the largest value a variable of data type Py_ssize_t can store.

and so on…

Resources:

https://www.geeksforgeeks.org/python-sys-module/#:~:text=The%20sys%20module%20in%20Python,interact%20strongly%20with%20the%20interpreter.

This is all about the sys module in python.

I think this is easy to know the concept in my blog.:)

MONISHA K…!

--

--