Python Modules and Code Reuse

This tutorial is adapted from Web Age course Introduction to Python Development.

1.1 Code Organization in Python

Several organizational terms are used when referring to Python code:
Module – A file with some Python code in it
Library – A collection of modules
Package – A directory that can include an individual module, a library of modules, an __init__.py file or sub-package(s).

1.2 Python Modules

A Module is a file containing Python code.  Modules that are meant to be executed are sometimes called Scripts. Modules meant to be imported and used by other modules may be referred to as Libraries. Module filenames have the extension .py
Python module code can include variables, functions, classes and runnable code (code not in a function)

1.3 Python Module Example

The following file is an example of a Python module:
# my_utils.py
def get_mod_name():
return __name__

def halved(a):
return a / 2

def doubled(a):
return 2 * a

def squared(a):
return a * a

1.4 Using Modules

Modules must be importing before they can be used:
import module_name

Functions are called with the following syntax:
module_name.function_name()

Here’s a script that uses our util module:
# my_app.py
import my_utils
print(my_utils.get_mod_name())
print(my_utils.doubled(7))
print(my_utils.squared(5))

The above script outputs
my_utils
14
25

1.5 Import Statements

Import statements can be used to import a module or individual function. Imports can allow access via an alias’.

Import Allow Use of
import mod1 mod1.func_name()
import mod1 as m m.func_name()
import dir1.mod1 dir1.mod1.func_name()
import dir1.mod1 as dm1 dm1.func_name()
from mod1 import func_name func_name()

1.6 Using Modules in Multiple Projects

To use a Module in multiple projects:
Create the module in its own project.
Place a copy of the module in a location where Python can access it.
Import the module into the code where it is used.

1.7 How Does Python Find Modules?

Upon reading the import statement below Python looks in various directories for my_util.py and an error is thrown if the module is not found.
import my_util

The directories Python looks in are defined in its sys.path variable. The sys.path variable can be accessed from the Python console like this:
>>> import sys
>>> print(sys.path)
[”,
‘/usr/lib/python36.zip’,
‘/usr/lib/python3.6’,
‘/usr/lib/python3.6/lib-dynload’,
‘/usr/local/lib/python3.6/dist-packages’,
‘/usr/lib/python3/dist-packages’]

Placing your module in any of the listed directories will allow python to find it when it processes the import statement.

1.8 Adding Directories to Sys.Path

If the directory where you are keeping your module is not one of the directories in sys.path you have two options.
Copy your module file to a directory listed in sys.path.
Add the directory to the sys.path.

You can add directories to sys.path by :

  • Setting the PYTHONPATH environment variable before running your app (or before running the shell if you are working in there).
    export PYTHONPATH=”$PWD/the_module_dir”.
  • Adding the following code in your script (before the module import statement):

import sys
sys.path.append(‘the_module_dir’)
import module_name
module_name.some_function()

1.9 Packages in Python

A Python package is an organized collection of individual python files (modules).
To create a Python package:

  • Create a directory for the package.
  • Add python module files to the package directory.
  • Create a python file named __init__.py in the package directory.

Code in Python packages can be accessed using the same type of import statements that are used for modules.

1.10 Example Package

This package directory will be named: util
The util directory will hold two modules – each with two functions plus an empty __init_.py file:
module01.py

  • halved()
  • doubled()

module02.py

  • squared()
  • cubed()

__init__.py

1.11 Accessing the Package Functions

The following code will access the packages’ functions
# my_app.py
import util.module01
import util.module02
print(util.module01.doubled(2))
print(util.module02.squared(5))

This code is not optimal in that:
It requires two import statements
All function names must be fully qualified

1.12 Using the Package’s Initialization File

We can improve our code by adding the following to the __init__.py file:
from .module01 import doubled
from .module01 import halved
from .module02 import squared
from .module02 import cubed

This allows us to write our application like this:
# my_app.py
import util
print(util.doubled(2))
print(util.squared(5))

1.13 Summary

In this tutorial, we covered:
Code Organization in Python
Python Modules
Python Packages
Import Statements
The Package Initialization File

Leave a Reply

Your email address will not be published. Required fields are marked *