Python Programming

Python Modules

As your Python program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you've written in several programs without copying its definition into each program. So, grouping related code into a module makes the code easier to understand and use. Modules in Python are simply Python files with the .py extension , where classes, functions and variables are defined.

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
#creating module sum.py def add(num1,num2): val = num1+num2 return val

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
#importing module sum.py import sum x=10 y=20 result = sum.add(x,y) print(result)
output
30
In the above program, import the "sum" module using the import statement. When the Python interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the Python interpreter searches before importing a module . Standard modules can be imported the same way as we import our user-defined modules.

The from...import Statement

We can import specific names form a module without importing the module as a whole.

Example
from math import sqrt print("Square root of 9 is", sqrt(9))

Python Module Search Path

Python searches for an imported module in the list of directories given by the Python variable sys.path. This variable is initialized from the directory containing the application, and in the list of directories specified by the environment variable < PYTHONPATH >, which uses the same syntax as the shell variable < PATH >, that is, a list of directory names. If you have not set < PYTHONPATH >, or if the file is not found, the search continues in an installation-dependent default path.

dir() function

The dir() function to list the identifiers that a module defines. The identifiers are the functions, classes and variables defined in that module. Example
import sys print(dir(sys))

Python Standard Modules

Python is a modular language that imports most useful operations from the standard library. The Python Standard Library contains a huge number of useful modules and is part of every standard Python installation. Not all of these modules are recommended for use by the typical Python developer , many have specialized uses associated with the Python internal modules and are intended mainly for use by developers working on Python itself.

Compiled Python files

To speed up loading modules, Python caches the compiled version of each module in the __pycache__ directory under the name module.version.pyc, where the version encodes the format of the compiled file; it generally contains the Python version number. The .pyc contain the compiled bytecode of Python source files. The Python interpreter loads .pyc files before .py files, so if they're present, it can save some time by not having to re-compile the Python source code.As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you've written in several programs without copying its definition into each program. So, grouping related code into a module makes the code easier to understand and use. Modules in Python are simply Python files with the .py extension, where classes, functions and variables are defined.