Bona Akubue | Software Developer

python modules

Python Modules and Packages

Python modules and packages are powerful ways of structuring programs, making it easy for code reuse and minimizing the chances of name collisions across programs.

A Module in Python is simply a file containing statements and variables. It is a file with a file extension of  .py containing codes written in Python. A typical module is made up of definitions and statements in the form of variables, functions and classes.

Modules make it easy to organize program components in such a way that they can be reused by other programs. Instead of writing all your codes in a single file, you can break them down into smaller chunks. It’s best that you organize related codes in a module.

The beauty of this approach in programming is writing simple, reusable and maintainable programs.

Creating Python Modules

To create a Python module, you have to create a file with a text editor like Notepad and save the file with a file extension of .py. With this, you have created a Python module. Subsequently, you can type in your codes in the file and save them.

You are free to name your module whatever you wish, as long as it adheres to the rules for Python variable names.

However, it’s best that the name you choose be simple, describing concisely what the module does.

All names defined at the top level of a module become attributes of the imported module object. Here is an example of a module file with the name greeting.py.

name = 'Andrew'

def greet():
    print('Hello' + ' ' + name)

In the above example, the name of the module is greeting. The variable name and the function greet() are attributes of the module. These attributes are accessible by other programs, but to do this, you would have to import them.

Import Modules

In order to use an already existing module in a program or access its attributes, you have to import the module. Importing a module simply means loading the contents of the module file into another program or script.

Now, let’s look at some of the ways you can import a module into a program.

Import Statement

To import an entire module into a program, you can use the keyword import followed by the name of the module that you want to import as shown below:


import greeting

The above statement is a way of telling Python to load the contents of a module named greeting.py into a program. An import statement instructs Python to include the code in the module file in the currently running program.

By importing a module, the program has access to all the attributes in the module through the name of the module.

The module greeting has two attributes – name and greet().

By importing the module greeting, you have access to these attributes by writing the name of the module – greet, followed by a dot (.) and the name of the attribute.


import greeting
print(greeting.name)
#output
'Andrew'
greetings.greet()
#output
#Hello Andrew

from module import name

Using the import statement, you automatically include all the namespaces contained in a module.

What if you’re only interested in some of the attributes and not all?

To do this, instead of importing the entire module, you can select the one you are interested in using from module import attribute.


from greeting import name

print(name)

#output
#Andrew

The from import copies specific names from a module to another program. Hence, allowing copied names to be used directly without going through the modules. It also allows names to be copied to be listed out as shown in the example below.


from greeting import name, greet

print(name)

#output
#Andrew

greet()
#output
#Hello Andrew

If the function name you are importing conflicts with an existing name in your program, you can use an alias ‘as‘.


from greeting import name as person

print(person.name)

#output
#Andrew

from module import *

Instead of listing out the attributes when using the from import, you can use the wild card * to copy out all the attributes defined at the top level of the imported module.


from greeting import *
print(name)
#output
#Andrew

Keep in mind that this is not the best practice and should be avoided because it can lead to confusion with names in your file, leading to errors that are difficult to diagnose.

Reloading modules

Importing modules is an expensive process. Hence, you cannot import modules more than once in Python. However, there are times when it becomes extremely necessary to do so. You can use the reload() function of the importlib module.

import greeting
import importlib

#reload module greeting
importlib.reload(greeting)

Standard Library Module

Python automatically comes with a large collection of modules known as the standard library. Modules like math, os, datetime, unittest and so on are modules in Python’s standard library.

You can import these modules the same way that you import the ones that you created.

import maths

#find the square root of a number
num = math.sqrt(4)

print(num)

#output
#2.0

Dir() Function

The dir() function is used to determine the names that are defined in a module. If you are unsure of the namespaces or attributes of a module, you can quickly check this out using this function.

import math
dir(math)

Module search path

This has to do with the places where Python searches for a module file when it encounters an import statement.

When you import a module in a script or another module. Python searches for the module file in the current working directory.

If the module is not there, it then proceeds to search for it in the directories available in the Python path and then in the directories for the standard library.

If no such module exists, then it will raise an exception – ModuleNotFoundError.

Packages

A package is simply a directory containing modules. While modules are used for organizing code components into files, packages are used for organizing module files into directories.

To create a Python package, all that is required is to create a directory and put module file(s) into it.

In the past, a directory is required to contain a Python file with the name __init__.py to be considered a Python module, but it’s no longer compulsory.

Package import

Indeed, packages make it easy to organize your modules. Now, let’s look at how you can import the modules or access the attributes of modules contained in packages.

There are two ways of performing package imports and they include:

  • Relative import
  • Absolute import

Relative Import

In relative imports, you specify the module you want to import by providing its path relative to the current working directory.

Let’s say that you have a directory named pkg that contains another directory named pkg1. The directory pkg1 contains a module file with the name mod1.py.

Imagine you are to create a new module named mod in the pkg directory and want to import the module mod1, which is in the pkg directory, you can use easily do so using the relative import.


#relative import
from pkg1 import mod1

Since the package – pkg1 and the module mod are at the same level in the directory, you can simply refer to the package with just the name.

Now, let’s say that pkg1 contains another module named mod2.py and you want to import mod1 in mod2. Since mod1 and mod2 are in the same package, they are considered siblings. Hence, you can use a leading dot (.) to indicate a package import as shown below:

 
#To import mod1 in mod2
from . import mod1 

The . specifies that the module named mod1 is in the same package as the mod2.

Now, let’s say the parent directory pkg contains another package named pkg2, which contains a module – mod3.

This means that packages – pkg1 and pkg2 are siblings. To import mod3 into mod1, you will have to use two leading dots (..).


#import mod3 in mod1
from .. import mod3

Absolute Import

An absolute path contains the full path of the package being imported starting from the root directory to the module that you are interested in.

Let’s say that you want to import the module mod which is contained in the package pkg from the mod1 module.

 
#Absolute import
from pkg import mod 

 

Leave a Comment

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