As your Python program becomes lengthier, you might consider dividing it into multiple files to facilitate easier maintenance. Similarly, if you have a useful function that's employed across multiple programs, you can avoid redundant copying by incorporating its definition into each program. Consequently, assembling logically associated code into a module enhances code comprehensibility and reusability. In Python, modules consist of .py files where class definitions, functions, and variables are encapsulated.
How to create a module in Python
There are various methods of writing modules, but the simplest way is to create a file with a .py extension.Example
After writing the above code in a text file and save it as sum.py
Importing a Module
You can use any Python source file (.py extension) as a module by executing an import statement in some other Python source file.Example
In the above program, incorporate the "sum" module by employing the import statement. When the Python interpreter encounters an import statement, it proceeds to import the specified module if it resides within the search path. This search path denotes a compilation of directories explored by the interpreter before module importation. The importation of standard modules follows the same protocol as that of user-defined modules.
The from...import Statement
We can import specific names form a module without importing the module as a whole.Example
Python Module Search Path
Python conducts a search for an imported module within a collection of directories outlined by the Python variable sys.path. This variable initially derives its values from the application's directory and further includes directories designated by the environment variable < PYTHONPATH >, analogous to the structure of the shell variable < PATH >. In instances where < PYTHONPATH > isn't configured or the file remains elusive, the search persists in a predefined default path contingent on the installation.
The dir() function to list the identifiers that a module defines. The identifiers are the functions, classes and variables defined in that module.Example
Python Standard Modules
Python operates as a modular language, importing a multitude of valuable functionalities from its extensive standard library. This library encompasses an expansive array of beneficial modules, an integral component of every standard Python installation. However, it's important to note that not all of these modules are suitable for regular Python developers; certain modules possess specialized utilities tied to Python's internal workings and are primarily tailored for developers engaged in Python's core development.
Compiled Python files
In order to enhance module loading speed, Python employs a cache mechanism, storing compiled versions of modules in the "pycache" directory under the naming convention "module.version.pyc," where the version signifies the format of the compiled file, often reflecting the Python version number. These ".pyc" files store the bytecode derived from Python source files. The interpreter prioritizes loading ".pyc" files over ".py" files, resulting in time savings as it circumvents the need for recompilation. As programs expand, dividing them into multiple files for simplified maintenance becomes advantageous.
The convenience of reusing functions across various programs is achieved by encapsulating them in modules, thereby enhancing code comprehensibility and utility. Python modules are essentially ".py" files that encompass definitions of classes, functions, and variables.