Python - Sys Module

Python - Sys Module

  • 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.
  • The sys module provides system specific parameters and functions. We will be narrowing our study down to the following:
  • sys.argv
  • sys.executable
  • sys.exit
  • sys.version
  • sys.path
  • sys.platform

1. sys.argv

  • The value of sys.argv is a Python list of command line arguments that were passed to the Python script. The first argument, argv[0] is the name of the Python script itself. Depending on the platform that you are running on, the first argument may contain the full path to the script or just the file name.  
  • import sys
  • sys.argv

2. sys.executable

  • The value of sys.executable is the absolute path to the Python interpreter. This is useful when you are using someone else’s machine and need to know where Python is installed. On some systems, this command will fail and it will return an empty string or None.
  • import sys
  • sys.executable

3. sys.exit

  • 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.
  • import sys
  • sys.exit(0)

4. sys.version

  • This attribute displays a string containing the version number of the current Python interpreter.  
  • import sys
  • sys.version

5. sys.path

  • This is an environment variable that is a search path for all Python modules.  
  • import sys
  • sys.path

6. sys.platform

  • The sys.platform value is a platform identifier. You can use this to append platform specific modules to sys.path, import different modules depending on platform or run different pieces of code.
  • import sys
  • sys.platform

 

 

 

 

 

 

 

Comments