Python Modules and Packages


Table of contents

Python Modules

Modules are python files that contain python statements and definitions. Modules can be used to split large programs into small files. The modules are named the same as a single file such as myprogram.py and in this case, the module name would be myprogram. Python modules allow us to logically group related code which makes the program easier to understand.

Python provides different built-in modules that we will discuss in future tutorials. Another important feature of modules is that they provide code reusability. We can write frequently used functions, classes, and variables as a separate module and import it into different programs. Similarly, the built-in modules contain functions that can be imported and called into our programs.

Creating Modules in Python

For example:

# Python module example myprogram.py

def subtract(a, b):
    """ this function subtracts b from a and returns the result """
    return b - a

def add(a, b:)
    """ this function adds a and b, and returns the result """
    return a + b

In the above example, functions subtract() and add() are written in a file myprogram.py and saved as a separate file. Now, we can just import the above module myprogram.py and call these functions if we need to perform subtraction or addition in our programs instead of writing the same functions in all programs.

Importing Modules Using import Statement in Python

Once a module is created, you use it in other programs simply by importing it using an import statement.

For example, we can import the module myprogram.py that we created earlier as:

import myprogram

Remember that we should not include .py extension in the import statement. The built-in or standard python modules can be imported in the same way. Now, to use the subtract function defined in the myprogram.py module, we can use the dot operator (.) as:

For example:

import myprogram

c = myprogram.subtract(10, 5)
print(‘The result is:’, c)

Output:

The result is: 5

Python Module Name Renaming in the import Statement

As you can see in the above example, to use functions that are in the module, we need to use a module name with the dot operator. We can use ‘as’ keyword to rename the module name.

For example:

import myprogram as mp

c = mp.subtract(10, 5)
print(‘The result is:’, c)

Python from…import Statement

Sometimes, you might just want to import specific functions or attributes from the module instead of everything. Python allows you to do just that with from…import statement.

For example:

from myprogram import subtract

c = subtract(10, 5)
print(‘The result is:’, c)

In the above example, you can see that when we use from…import statement, we do not need to use a dot operator to access the functions. Here, from…import statement will only import subtract function, not the add function.

To import all the attributes using from…import statement, we can use * operator.

For example:

from myprogram import *

This will import all the attributes from the module myprogram, however, this is not a good programming practice so I suggest avoiding using it often.

Python Module Search Path

While importing a module, the python interpreter searches for the module in the following order:

  • The current working directory where the main program file is located.
  • If the module is not found in the current directory, python tries to locate the module in each directory in the shell variable PYTHONPATH.
  • If a module is not found in each directory in the PYTHONPATH, it searches the installation-dependent default, that is the default path.

Python Module Reloading Using reload() function

The code in the module is imported and executed only once per session when it is imported into another file. However, if we change our module after importing it into another program during the same session, we have to load it again. One way to do this is to restart the interpreter.

However, python provides another way of doing this with the use of the built-in function reload(). This function is defined in a standard module called imp.

For example:

import importlib as il
import myprogram
il.reload(myprogram)

Finding Names in a Module Using dir() Function

Python allows you to display all the names that a module contains with the use of built-in function dir(). This function returns a sorted list of names that are defined in a module.

For example:

import myprogram as mp

a = dir(mp)
print(a)

Output:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'add', 'subtract']

In the output above, you can see there are names starting with a double underscore (__) other than the names we defined (add, subtract). Those names are default python attributes that are associated with the module.

Python Packages

A real-world program contains lots of files and modules. Having all the files in a single directory can make it difficult to understand the program. Python provides a hierarchical file directory structure called package to store files that defines a single Python application environment.

A package consists of all the files, modules, and sub-packages related to a program. We can have multiple packages for a single program. Each package will contain all the similar files and modules. Packages make a program easy to manage and maintain.

One important thing to keep in mind is that each directory must have a file named __init__.py for the python interpreter to interpret it as a package. This file contains the initialization code for the respective package.

Suppose we are creating a program to store student records. The program may contain packages and files as follows:

  • Main
    • __init__.py
  • Grades
    • __init__.py
    • display.py
    • add.py
  • Attendance
    • __init__.py
    • display.py
    • add.py
  • Addresses
    • __init__.py
    • display.py
    • add.py

Importing Module From a Python Package

A package can contain multiple modules and they can be imported to other files with the use of a dot (.) operator or from…import statement.

For example:

import Main.Grades.display # importing display module from Grades package
from Main.Grades import display # this is equivalent to the first line and both can be used interchangebly

The above code will import display module from the Grades package from the above example.