Search
Custom Modules

Pre-requisites

  • functions
  • types
  • collections
  • packages

Modules

Instead of creating one large piece of code that can often be unwieldy we can break it up into smaller chunks, or modules. Modules can be accessed through installed packages or they can written by the user.

Custom Modules

Below is an example of a custom module (i.e. one we have written ourselves) called print_things.py:

def print_names(list_of_names):
    for name in list_of_names:
        print(name)

def print_dictionary(input_dictionary):
    for key, value in input_dictionary.items():
        print("Key: " + key + ", Value: " + str(value))

Here, print_things.py contains the objects:

  • print_names()
  • print_dictionary()

which are both functions.

Great! We have made our module, but how can we go about using it?

Python makes this really simple with the import statement.

Importing Modules

In order to use our newly created module we need to import it into our main script:

import print_things

Now, we can access the objects in the print_things module:

import print_things


element_names = ["Hydrogen", "Helium", "Lithium"]
element_protons = {"Hydrogen": 1, "Helium": 2, "Lithium": 3}

print_things.print_names(element_names)

print_things.print_dictionary(element_protons)

An alternative to using import is to use the from statement. This allows us to select certain objects from our module rather than importing all of them in one go:

from print_things import print_names


element_names = ["Hydrogen", "Helium", "Lithium"]

print_things.print_names(element_names)

However, if we do want to import all objects in our print_things module with the from statement we can do so like this:

from print_things import *

Using this style of import, if there are any modules that we don't want to be imported (i.e. that we only want to be used within the module itself), we can prefix them with an underscore (_).

def print_names(list_of_names):
    for name in list_of_names:
        print(name)

def print_dictionary(input_dictionary):
    for key, value in input_dictionary.items():
        print("Key: " + key + ", Value: " + str(value))

def _private_function():
    print("I'm a private function!")

Here, _private_function() can only be used within the print_things.py module. We can test this by importing the print_things.py module and trying to use it:

#  Import all of the functions from the print_things.py module
from print_things import *

element_names = ["Hydrogen", "Helium", "Lithium"]

#  Use the print_names() function 
print_names(element_names)

#  Use the _private_function()
_private_function()

This can be avoided by importing the module in the normal way:

#  Import all of the functions from the print_things.py module
import print_things

element_names = ["Hydrogen", "Helium", "Lithium"]

#  Use the print_names() function 
print_things.print_names(element_names)

#  Use the _private_function()
print_things._private_function()

Another useful way to use both the import / from statements is to import modules and use an alternative name:

from print_things import print_names as pn #  Import print_names from the print_things module 
import print_things as pt #  Import the whole print_things module

Organising Modules: Packages

As a project grows, the number of individual modules may increase and can often be hard to manage. This is where Python packages come in.

Packages are collections of modules contained within a directory. However, in order to make sure this directory is recognised as a package it needs to contain a file named __init__.py, this allows it to be imported (just like the print_things.py module we created earlier).

For example if we had a package called pkg that contained two modules we will name module2.py and module1.py we could import them as:

import pkg.module1, pkg.module2

Location

An important point that applies to packages (and by extension modules) is their location. Most of the time a package manager will sort out the location of installed packages so they are useable. Additionally, Python will also look in your current working directory for any custom packages.