sys Module in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The python sys module contains methods and variables for modifying many elements of the Python Runtime Environment. It allows us to access parameters and functionalities specific to the system.

This module gives you access to some variables that the interpreter uses or maintains, as well as functions that interact with it. It is available at all times.

Introduction To SYS Module In Python

These days, Python is a very popular and sophisticated scripting language. Python is becoming increasingly popular in various fields, including application development, website development, and the creation of analytical products and production-level code. One of the reasons for this is the flexibility and ease of usage of the modules. Let's look at one of Python's most popular modules, Sys. The system is specified by the Sys module. It essentially defines the surroundings.

Before performing any functions, we must import the sys module into our application. To import the sys module, put import sys into the command prompt.

Important Variables In The SYS Module

This sys module offers you access to certain variables that the interpreter uses or maintains, as well as functions that have a lot of interaction with it. It is available at all times.

1. sys.argv:

In Python, this includes a list of arguments (command prompt) supplied to the script. If you write len(sys.argv), you'll get a count of how many arguments there are. sys.argv is used by people who work with command-line arguments. The name of the script is referred to as Sys.argv[0].

Code:

Assume this python code is saved as sys.py at a specific location. Now type python sys.py into the command prompt, and the output will be as follows.

Output

If you type python sys.py arg1, the output will be as follow.

Output

It's worth noting that the first parameter will always be the script name, which will be counted as part of the total number of arguments.

2. sys.path :

This function displays the current system's python path. It's an environmental variable that contains the search path for all of the python modules.

3. sys.maxsize :

This maxsize function returns the variable's greatest integer.

4. sys.version :

This version variable returns a string that contains the current python interpreter's version number.

Important Functions In The SYS Module

1. sys.exit():

The program will exit the python console or the command prompt. It is usually used to exit the application if an exception is thrown securely.

Output

So from the above output, it is observable that the program has ended up in the python console after an encounter or execution of the sys.exit. After that, it can't even execute the last line of the program in which is_even(list) is again called by passing the list as a parameter.

2. sys.setdefaultencoding():

This method allows you to adjust the default encoding for strings in the Python environment.

Let's take a view at the example given below.

3. sys.getsizeof():

After using this function, the size of an object in bytes is returned. Any object can be used as an object.

Output

As it is clear from the above example that the sys.getsize() is returning the size of the object created, that is, i here with integer value 3.

4. sys.getrecursionlimit():

This sys.getrecursionlimit() function is used to determine the interpreter's current recursion limit or the maximum possible size or depth of the Python interpreter stack. This limit protects any program from going into infinite recursion, which would otherwise cause a program crash in Python.

Output

Input And Output Using SYS

Variables in the sys modules provide for good control of input and output. We can even send input and output to different devices using these variables. We can use two variables to do this, which are listed below.

1. stdin:

This stdin can be used to take input from the command line directly. This stdin method is used for taking standard input. It uses the input() function internally. It also adds a \n that is a new line at the end of each sentence. Let's understand this with an example given below.

Output:

2. stdout:

The interpreter's standard output stream in Python is analogous to this built-in file object. The stdout command sends output to the screen console. The output that comes out using the print statement, an expression statement, or even a prompt direct for input, might take many different forms. Streams are set to text mode by default. Whenever a print function is used in the code, it is written first to sys.stdout, then to the screen. Let's understand this in a better way from the example that is given below:

Output:

All Variables In The SYS Module

Serial No.VariableDescription
1.sys.addaudithookAdd the callable hook to the current (sub)interpreter's list of active auditing hooks.
2.sys.executableA string containing the absolute path of the Python interpreter's executable binary.
3.sys.displayhook(value)This keeps value in builtins._ and prints repr(value) to sys.stdout.
4.sys.argvThis includes a list of arguments (command prompt) supplied to the script.
5.sys.abiflagsThis contains the ABI flags provided by PEP 3149 for POSIX systems where Python was produced with the normal configure script.
6.sys.pathIt displays the current system's python path
7.sys.maxsizeThis maxsize function returns the variable's greatest integer.
8.sys.versionThis version variable returns a string which contains the current python interpreter's version number.
9.sys.flagsThe state of command-line flags are exposed by the named tuple flags.
10.sys.getrefcount(object)The object's reference count is returned.

All Functions In The SYS Module

Serial No.FunctionDescription
1.sys.base_exec_prefixThis function returns the same value as exec prefix more efficiently. The value will not change if you are not running a virtual environment.
2.sys.byteorderIt's a symbol for the native byteorder, which gives you a quick way to perform anything.
3.sys.stdinIt's an object that holds the stdin values at the start of the program and is used for finalisation. It is capable of restoring the files.
4.sys.outThe stdout command sends output to the screen console.
5.sys.exitThe program will exit to the python console or the command prompt as a result of this.
6.sys.getsizeofThe size of an object in bytes is returned.
7.sys.setdefaultencodingThis method allows you to adjust the default encoding for strings.
8.sys.getrecursionlimitReturns the interpreter's current recursion limit or the maximum possible size or depth of the Python interpreter stack.
9.sys.platformThis function's value is used to determine the platform in which we're currently working.
10.sys.modulesThis function returns the names of the Python modules that have already been imported.

Conclusion

  • We have got to know about the sys module's various inbuilt functions and variables.
  • One can know about his/her system version and environment in which he/she is currently working.
  • Functions of this sys module can be griped over once you start practicing with these inbuilt functions and variables of the sys module.

See Also: