Home / Linux / Learn Python Sys Module Functions

Learn Python Sys Module Functions

Learn Python Sys Module Functions

In this text, we are going to check out the Python Sys Module. There are variables and features which are maintained by the interpreter and the sys module supplies a approach of interacting with them. These variables can be found till the interpreter is alive. We can have a look at a few of the generally used sys features.

To work with the sys module you need to first import the module.

sys.model – This shops the details about the present model of python.

$ python3
>>> import sys
>>> sys.model
Show Current Python VersionShow Current Python Version

Show Current Python Version

sys.path – Path variable shops the listing path within the type of a listing of strings. Whenever you import a module or run a program utilizing a relative path, python interpreter seek for the required module or script utilizing the trail variable.

Path index shops the listing containing the script that was used to invoke the Python interpreter on the index “Zero”. If the interpreter is invoked interactively or if the script is learn from commonplace enter, path[0] will probably be an empty string.

>>> sys.path
Invoking in InterpreterInvoking in Interpreter

Invoking in Interpreter

When invoking the script the path[0] shops the listing path.

$ vim 1.py
$ python3 1.py
Invoking as ScriptInvoking as Script

Invoking as Script

If you could have modules in a customized listing then you’ll be able to add the listing path to the trail variable utilizing a path.append() technique (because the path is a listing object we’re utilizing the record technique “append”).

$ python3
>>> import sys
>>> sys.path
>>> sys.path.append('/root/check/')
>>> sys.path
Python Append MethodPython Append Method

Python Append Method

sys.argvargv is used to cross run time arguments to your python program. Argv is a listing that shops the script title as the first worth adopted by the arguments we cross. Argv values are saved as sort string and you need to explicitly convert it in accordance with your wants.

>>> sys.argv

When you run under snippet, the tip worth of vary operate is handed through sys.argv[1] as 10 and few different values are additionally handed to print the record of argv values on the finish of this system.

#!/usr/bin/python3

import sys

for x in vary(1,int(sys.argv[1])):
    print(x)
    
# Print all of the arguments handed
print("Arguments passed:",sys.argv)
Passing Arguments in PythonPassing Arguments in Python

Passing Arguments in Python

sys.executable – Prints absolutely the path of the python interpreter binary.

>>> sys.executable
'/usr/bin/python3'

sys.platform – Prints the os platform sort. This operate will probably be very helpful if you run your program as a platform dependent.

>>> sys.platform
'linux'

sys.exit – Exit the interpreter by elevating SystemExit(standing). By default, standing is claimed to be Zero and is claimed to achieve success. We can both use an integer worth as Exit Status or different kinds of objects like string(“failed”) as proven within the under instance.

Below the pattern, a snippet is used to examine if the platform is home windows after which run the code. If not increase exit() operate.

#!/usr/bin/python3

import sys

if sys.platform == 'home windows':  # CHECK ENVIRONMENT
    #code goes right here
    cross
else:
    print("This script is intended to run only on Windows, Detected platform: ", sys.platform)
    sys.exit("Failed")
Check OS PlatformCheck OS Platform

Check OS Platform

sys.maxsize – This is an integer worth representing most worth a variable can maintain.

On a 32-bit platform it's 2**31 - 1 
On a 64-bit platform it's 2**63 - 1
Wrapup

We have seen a few of the necessary features of the sys module and there are much more features. Until we provide you with the following article you’ll be able to learn extra concerning the sys module right here.

About Agent

Check Also

SpaceX Successfully Launches 60 More Starlink Satellites as it Continues Towards 2020 Service Debut

SpaceX Successfully Launches 60 More Starlink Satellites as it Continues Towards 2020 Service Debut SpaceX …

Leave a Reply

Your email address will not be published. Required fields are marked *