Day 8–101 Days of DevOps — Python sys module

Prashant Lakhera
3 min readJul 8, 2021

--

Welcome to Day 8 of 101 Days of DevOps. The topic for today is a python platform module.

To view the complete course, please check the below url.

For more info, register via the below link

YouTube Channel link

The sys module provides functions and variables to manipulate different parts of the Python runtime environment(Remember this point, Sys it has nothing to do with your Operating System). This is the common myth that sys is used for interpreting your operating system variables, but conversely, it is only used to manipulate your Python runtime environment.

  • To print the version number of the Python interpreter
>>> sys.version'3.7.9 (default, Apr 30 2021, 20:11:56) \n[GCC 7.3.1 20180712 (Red Hat 7.3.1-12)]'
  • To print the platform-specific information
>>> sys.platform'linux'
  • In the operating system, you have the command echo $PATH to find which directories is shell to check for executables.
echo $PATH/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin
  • Similarly, Python will look for executables in the specified path, which can be determined using the sys.path.
>>> sys.path['', '/usr/lib64/python37.zip', '/usr/lib64/python3.7', '/usr/lib64/python3.7/lib-dynload', '/usr/lib64/python3.7/site-packages', '/usr/lib/python3.7/site-packages']
  • To find out the list of modules Python import to run the script, use sys.modules.
>>> sys.modules{'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, '_imp': <module '_imp' (built-in)>, '_thread': <module '_thread' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'zipimport': <module 'zipimport' (built-in)>, '_frozen_importlib_external': <module '_frozen_importlib_external' (frozen)>, '_io': <module 'io' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'posix': <module 'posix' (built-in)>, 'encodings': <module 'encodings' from '/usr/lib64/python3.7/encodings/__init__.py'>, 'codecs': <module 'codecs' from '/usr/lib64/python3.7/codecs.py'>, '_codecs': <module '_codecs' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from '/usr/lib64/python3.7/encodings/aliases.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from '/usr/lib64/python3.7/encodings/utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' (built-in)>, 'encodings.latin_1': <module 'encodings.latin_1' from '/usr/lib64/python3.7/encodings/latin_1.py'>, 'io': <module 'io' from '/usr/lib64/python3.7/io.py'>, 'abc': <module 'abc' from '/usr/lib64/python3.7/abc.py'>, '_abc': <module '_abc' (built-in)>, 'site': <module 'site' from '/usr/lib64/python3.7/site.py'>, 'os': <module 'os' from '/usr/lib64/python3.7/os.py'>, 'stat': <module 'stat' from '/usr/lib64/python3.7/stat.py'>, '_stat': <module '_stat' (built-in)>, '_collections_abc': <module '_collections_abc' from '/usr/lib64/python3.7/_collections_abc.py'>, 'posixpath': <module 'posixpath' from '/usr/lib64/python3.7/posixpath.py'>, 'genericpath': <module 'genericpath' from '/usr/lib64/python3.7/genericpath.py'>, 'os.path': <module 'posixpath' from '/usr/lib64/python3.7/posixpath.py'>, '_sitebuiltins': <module '_sitebuiltins' from '/usr/lib64/python3.7/_sitebuiltins.py'>, 'readline': <module 'readline' from '/usr/lib64/python3.7/lib-dynload/readline.cpython-37m-x86_64-linux-gnu.so'>, 'atexit': <module 'atexit' (built-in)>, 'rlcompleter': <module 'rlcompleter' from '/usr/lib64/python3.7/rlcompleter.py'>, 're': <module 're' from '/usr/lib64/python3.7/re.py'>, 'enum': <module 'enum' from '/usr/lib64/python3.7/enum.py'>, 'types': <module 'types' from '/usr/lib64/python3.7/types.py'>, '_collections': <module '_collections' (built-in)>, 'sre_compile': <module 'sre_compile' from '/usr/lib64/python3.7/sre_compile.py'>, '_sre': <module '_sre' (built-in)>, 'sre_parse': <module 'sre_parse' from '/usr/lib64/python3.7/sre_parse.py'>, 'sre_constants': <module 'sre_constants' from '/usr/lib64/python3.7/sre_constants.py'>, 'functools': <module 'functools' from '/usr/lib64/python3.7/functools.py'>, '_functools': <module '_functools' (built-in)>, 'collections': <module 'collections' from '/usr/lib64/python3.7/collections/__init__.py'>, 'operator': <module 'operator' from '/usr/lib64/python3.7/operator.py'>, '_operator': <module '_operator' (built-in)>, 'keyword': <module 'keyword' from '/usr/lib64/python3.7/keyword.py'>, 'heapq': <module 'heapq' from '/usr/lib64/python3.7/heapq.py'>, '_heapq': <module '_heapq' from '/usr/lib64/python3.7/lib-dynload/_heapq.cpython-37m-x86_64-linux-gnu.so'>, 'itertools': <module 'itertools' (built-in)>, 'reprlib': <module 'reprlib' from '/usr/lib64/python3.7/reprlib.py'>, '_locale': <module '_locale' (built-in)>, 'copyreg': <module 'copyreg' from '/usr/lib64/python3.7/copyreg.py'>}
  • Suppose you want to exit from Python use sys.exit(). In the below script, the moment it encounters path doesn’t exist, your script with exit using sys.exit(). Python will be going to stop the running of your script and your script exit.
import osimport sysif os.path.exists("/etc/hostss"):sys.exit()
  • To list the command-line argument passed to Python script, use sys.argv. argv[0] is the script name(operating system dependent whether this is a full pathname or not).
import sysprint("hello world")print(sys.argv)
  • If you store the above script in the file newtest.py and execute it, you will see a passing argument (new python) that will not impact the script. Now the last line in the script(print(sys.argv)). As you can see, index 0 is always your script name(newtest.py) and remaining all our command-line arguments.
python3 newtest.py new pythonhello world['newtest.py', 'new', 'python']
  • We can combine this with len built-in function to determine if the correct number of arguments passed to the script.
If len(sys.argv != 3):print(“This script need three command line argument”)sys.exit()

Assignment:

  1. Try to create your own version of the ls command which takes a directory as an argument and prints the content of a directory. So it should be like this
python <script name> /etc----Content of /etc directory ---

I am looking forward to you guys joining the amazing journey.

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet