Learning Python Part-20: Python Modules and Python Package

Python Modules:

We use modules to break down large programs into small manageable and organised files. We cannot keep on writing large programs in one python file. This approach may pose challenges like maintaining the code, debugging and few others. Using only one file for entire program may work for small programs and when only one individual is working on a small project. 

Most of the times, it is not the case, as when it comes to programming at your office environment, you are working mostly on large projects and as part of team and hence collaboration will be required as tasks will be distributed. This is where modules help in achieving the final goal.
A Module is basically a file containing Python statements and definitions. A file containing Python code, for e.g.: example.py, is called a module and its module name would be example. 

Furthermore, modules provide reusability of code. We can define our most used functions in a module and import it, instead of writing their definitions into different programs multiple times.

Steps to create user defined module:
  • Create a .py file. In this example, we will use name example.py 
  • Write python code like functions or classes as in example below and save it.
  • Now whenever, you need to perform addition of two numbers, simply import this module.

Python Module example 

def add(a, b):

“””This program adds two numbers and return the result”””

result = a + b return result

How to import modules in Python?

We can import the definitions inside a module to another module or the interactive interpreter in Python. We use the import keyword to do this. To import our previously defined module example we type the following in the Python prompt.

import example 

Using the module names we can access the function using the dot . operator.
For example:

example.add(4, 5.5)


Standard modules of python can be imported the same way as we import user-defined modules.

Few more exmaples of ways to import and using modules:

  • Python import statement

import math

print(“The value of pi is”, math.pi)

  • Import with renaming
import math as m

print(“The value of pi is”, m.pi)

  • Python from…import statement
from math import pi

print(“The value of pi is”, pi)

  • Import all names
from math import *

print(“The value of pi is”, pi)

Python Module Search Path:

  • While importing a module, Python looks at several places. 
  • Interpreter first looks for a built-in module then (if not found) into a list of directories defined in sys.path. 
  • The search is in this order.
    • The current directory.
    • PYTHONPATH (an environment variable with a list of directory).
    • The installation-dependent default directory.
  • We can add or modify this list to add our own path.

>>> import sys
>>> sys.path

Reloading a module:

  • The Python interpreter imports a module only once during a session.
  • Now if our module changed during the course of the program, we would have to reload it.
  • One way to do this is to restart the interpreter. But this does not help much.
  • Python provides a neat way of doing this. We can use the reload() function inside the imp module to reload a module. 

>>> import my_module
>>> imp.reload(my_module) 

Python Package:

  • We usually store all of our files in computer in well organised hierarchy of directories for easier access.
  • Similar files are kept in the same directory, for example, we may keep all the songs in one directory. 
  • Similar to this, Python has packages for directories and modules for files.
  • As program grows larger in size with a lot of modules, we place similar modules in one package and different modules in different packages.
  • Similar, to that of directory which contains sub-directories and files, a Python package can have sub-packages and modules.
  • A directory must contain a file named __init__.py in order for Python to consider it as a package.
  • This file can be left empty but we generally place the initialization code for that package in this file.

Here is an example. Suppose we are developing a game, one possible organisation of packages and modules could be as shown in the figure below.

Image: Programmiz

Importing module from a package:

  • We can import modules from packages using the dot (.) operator.
  • To import the start module in the above example, it is done as follows.

>>> import Game.Level.start

Leave a Reply