Sys Module

Sys Module in Python with Examples



The sys module provides information about constants, functions and methods of the Python interpreter. dir(system) gives a summary of the available constants, functions and methods. Another possibility is the help() function. Using help(sys) provides valuable detail information.


The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. You will learn some of the important features of this module here.


1. sys.version This stores the information about the current version of python.

>>> import sys
>>> sys.version
'3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)]'

2. sys.platform - Prints the os platform type. This function will be very useful when you run your program as a platform dependent.

>>> sys.platform

Windows

3. sys.path Path variable stores the directory path in the form of a list of strings. Whenever you import a module or run a program using a relative path, python interpreter search for the necessary module or script using the path variable.

Path index stores the directory containing the script that was used to invoke the Python interpreter at the index “Zero”. If the interpreter is invoked interactively or if the script is read from standard input, path[0] will be an empty string.

>>> sys.path
['', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\idlelib', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\lib', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\win32', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\win32\\lib', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\Pythonwin']
When invoking the script the path[0] stores the directory path.

Exp: Inside, Test.py
  import sys
  print(sys.path)


['C:\\Users\\Greg\\Desktop', 
'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip', 
'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs', 
'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32', 
'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages',
 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages
\\win32', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\

site-packages\\win32\\lib', 'C:\\Users\\Greg\\AppData\\Local\\Programs\\Python
\\Python37-32\\lib\\site-packages\\Pythonwin']

4. sys.argv argv is used to pass run time arguments to your python program. Argv is a list that stores the script name as the 1st value followed by the arguments we pass. Argv values are stored as type string and you have to explicitly convert it according to your needs.

"sys.argv" returns a list of command line arguments passed to a Python script. The item at index 0 in this list is always the name of the script. The rest of the arguments are stored at the subsequent indices. Here is a Python script (test.py) consuming two arguments from the command line.


Example: (test.py)
>>> import sys
>>> print ("Hello {}. Welcome to {} class".format(sys.argv[1], sys.argv[2]))

If we run this file, with command argument:
>> C:\user\python3\python test.py  Greg  KT
>> Hello Greg, Welcome to KT class
As you can see, sys.argv[1] contains the first parameter 'Martin', while sys.argv[2] contains the second parameter 'Python'. sys.argv[0] contains the script file name test.py.

5. sys.exit Exit the interpreter by raising SystemExit(status). By default, status is said to be Zero and is said to be successful. We can either use an integer value as Exit Status or other kinds of objects like string(“failed”) as shown in the below example.
Below the sample, a snippet is used to check if the platform is windows and then run the code. If not raise exit() function.
or we can say, This causes the script to exit back to either the Python console or the command prompt. This is generally used to safely exit from the program in case of generation of an exception.
Example:
if sys.platform == 'windows':  # CHECK ENVIRONMENT
    #code goes here
    pass
else:
    print("This script is intended to run only on Windows, Detected platform: ", sys.platform)

    sys.exit("Failed")

Output:
>> The script is intended to run only on windows, Detected platform: Linux
      Failed
6. sys.maxsize This is an integer value representing maximum value a variable can hold. 

Example:
>>> import sys
>>> sys.maxsize

Output:
>> 2147483647

7. sys.executable - Prints the absolute path of the python interpreter binary.

>>> sys.executable

Output:
>> C:\Users\Greg\AppData\Local\Programs\Python\Python37-32\python.exe




Comments

Popular posts from this blog

Django Template Tags