Notable Standard Library Modules - the sys Module

Standard Library Modules

Standard Library Modules

Overview

Python's Standard Library is a rich collection of modules that you can use for various purposes without the need for external installations
The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming
Reference: The Python Standard Library @python.org

Popular Standard Library Modules

datetime - for working with dates and times
collections - for specialized container datatypes
typing - for support of type hints
sqlite3 - for SQLite database interactions
json - for encoding and decoding JSON data
re - for regular expressions
unittest - for unit testing
os - for interacting with the operating system
sys - for access to variables and functions used by the Python interpreter.
asyncio - for asynchronous I/O
multiprocessing - for parallel programming
socket - for network communication
subprocess - for spawning new processes

the sys module

the sys module

Overview

sys is a built-in module, which contains Python interpreter's specific parameters and functions
Reference: sys @python.org

            >>> import sys

            >>> dir(sys)
            ['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '
            _clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe', '_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv', 'base_exec_prefix', 'ba
            se_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix
            ', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getdlopen
            flags', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'hash_info', 'hexversion', 'implementa
            tion', 'int_info', 'intern', 'is_finalizing', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path
            _importer_cache', 'platform', 'prefix', 'set_coroutine_wrapper', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrac
            e', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions']
        

get current version number of Python

sys.version - A string containing the version number of the Python interpreter plus additional information.
sys.version_info - A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial.

                import sys

                print(sys.version)
                # 3.12.0 (main, Jan  2 2024, 16:26:01) [GCC 11.4.0]

                print(sys.version_info)
                # sys.version_info(major=3, minor=12, micro=0, releaselevel='final', serial=0)
            
Use case: specify version requirements directly in the code

                import sys

                if sys.version_info[0] == 2:
                    print("This program requires Python 3 or above. You are using Python 2.")
                elif sys.version_info[0] == 3:
                    print("Python 3 is running")
            

get interpreter absolute path

sys.executable - A string giving the absolute path of the executable binary for the Python interpreter

                import sys

                print(sys.executable)

                #/usr/bin/python3
            

get/set module search path

sys.path - A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

                import sys

                for path in sys.path:
                    print(path)

                #/media/user/data/projects/examples
                #/usr/lib/python312.zip
                #/usr/lib/python3.12
                #/usr/lib/python3.12/lib-dynload
                #/usr/local/lib/python3.12/dist-packages
                #/usr/lib/python3/dist-packages
                #/usr/lib/python3.12/dist-packages
            
You can adjust the module search path at runtime by appending the desired path to sys.path list:
If you want to use 'module1' which is in "/media/user/data/my_modules":

                import sys

                # add 'my_modules' to path:
                sys.path.append('/media/user/data/my_modules')

                from module1 import foo

                foo()
            

get the size of an object in bytes

sys.getsizeof() - Return the size of an object in bytes.
Note, that sys.getsizeof() function returns the size of the object itself, not the size of the elements it contains or references.
For a list, the size reported by sys.getsizeof() includes the size of the list's overhead plus space for pointers to the elements it contains, but not the size of the elements themselves

            import sys
            from functools import reduce

            x = 1000
            l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

            int_object_memory = sys.getsizeof(x)
            list_object_memory = sys.getsizeof(l)
            list_elements_memory = sum(sys.getsizeof(el) for el in l)

            print(int_object_memory)  # 28
            print(list_object_memory)  # 136
            print(list_elements_memory)  # 280
        

Terminate the script

sys.exit([status]) - Terminate the script with an optional exit status.
exit status zero is considered "successful termination" and any nonzero value is considered "abnormal termination" by shells and the like

                import sys

                sys.exit(0) # successful termination
            

sys.argv: accessing command-line arguments

sys.argv: accessing command-line arguments

Overview of command-line arguments

Command-line arguments are strings of text used to pass additional information to a program at the time of its invocation. They are typically used to specify options, flags, or data for program operations.
Ussually these arguments are used to control the program's operation, to provide necessary data for processing, or customize the program's behavior without needing to modify the program's source code
Command-line arguments are particularly useful for automating tasks, configuring scripts to run in different modes, or when the program is part of a larger pipeline in software workflows.
A simple use case is allowing users to query the script for usage information - a common feature in command-line tools and applications.

                $ python greet_user.py --help

                Usage: python greet_user.py [options]
                Options:
                  --help    Show this help message
                  --name    User name (string)
                  --age     User age (integer)
            
Command line arguments must follow the script name by a space.
Multiple command line arguments must be separated from each other by space.

                $ python greet_user.py --name=Ada --age=23
            

Accsess command line arguments in Python

In a Python script you can get command-line argument with sys.argv list:
Note that the script name itself is passed implicitly as the first argument!
i.e. sys.argv[0] will always store the script name.
If you need only the list of command-line argmuments, you can just slice sys.argv: sys.argv[1:]

                import sys

                print(f"Script Name: {sys.argv[0]}")
                print(f"Arguments: {sys.argv[1:]}")

                ###Output of:  python greet_user.py --name=Ada --age=23
                #Script Name: greet_user.py
                #Arguments: ['--name=Ada', '--age=23']
            

Command line arguments - examples

Lets have next script, which just prints argv list:

                import sys

                print('Argument List:', sys.argv)
            
Now, let's execute script with different command-line arguments:

                # call script without arguments:

                $ python argv_examples.py
                Argument List: ['argv_examples.py']
            

                # call script with 1 argument:

                $ python argv_examples.py arg1
                Argument List: ['argv_examples.py', 'arg1']
            

                # call script with 3 arguments:

                $ python argv_examples.py arg1 23 -45.6
                Argument List: ['argv_examples.py', 'arg1', '23', '-45.6']
            

Using argparse for Advanced Argument Parsing

Introduction to argparse

The argparse module makes it easy to write user-friendly command-line interfaces even for complex scenarios
The program defines what arguments it requires, and argparse figures out how to parse those out of sys.argv.
Example: simple script that echoes a message a specified number of times

                import argparse

                # Create the parser
                parser = argparse.ArgumentParser(description='Example script demonstrating argparse usage.')

                # define positional required argument:
                parser.add_argument('message', help='the message to be repeated')

                # define optional argument:
                parser.add_argument('--repeat', type=int, default=1, help='How many times to repeat the message (default: %(default)s)')

                # Parse arguments
                args = parser.parse_args()

                # Use arguments
                for _ in range(args.repeat):
                    print(args.message)



                ###Output of: $ python echo.py hello --repeat 3
                # hello
                # hello
                # hello

                ###Output of: $ python echo.py --help
                # usage: echo.py [-h] [--repeat REPEAT] message

                # Example script demonstrating argparse usage.

                # positional arguments:
                #   message          the message to be repeated

                # options:
                #   -h, --help       show this help message and exit
                #   --repeat REPEAT  How many times to repeat the message (default: 1)

                ###Output of: python3 echo.py
                # usage: echo.py [-h] [--repeat REPEAT] message
                # echo.py: error: the following arguments are required: message
            
argparse handles positional arguments (here, message) that are required, and optional arguments (here, --repeat) that the user can specify to alter the script's behavior.
The argparse module automatically generates help and usage messages and issues errors when users give the program invalid arguments.

These slides are based on

customised version of

Hakimel's reveal.js

framework