Using module and package in Python

This post is lesson 20 of 54 in the subject Python Programming Language

1. Modules in Python

1.1. What is a Module in Python?

A module in Python is a file containing statements and definitions. For example, a file example.py is a module with the module name example.

In a module, we can define functions, classes, and variables. We use modules to split a large program into smaller files. This helps to manage the program and organize its logical structure better. Furthermore, modules provide the ability to reuse a program’s code.

For example, we have a module example.py as below.

def add(x, y):
    """This program adds two numbers and returns the result"""
    result = x + y
    return result

def subtract(x, y):
    """This program subtracts two numbers and returns the result"""
    result = x - y
    return result

The example.py module consists of the definitions of 2 functions, add() and subtract().

1.2. Importing Modules in Python

We can use the functions, classes, and variables of one module in another module. To do this, we need to import a module into another module. Then, we use the dot operator (.) to use the components in the imported module. For example, we import the example module into the main module (file main.py).

import example

a = 1
b = 2
print("a + b = ", example.add(a, b))
print("a - b = ", example.subtract(a, b))

Output

a + b =  3
a - b =  -1

Note: The imported module must exist and be in the directories that the Python compiler searches for modules, such as the Lib folder in the Python installation folder, the folder containing the Python program being programmed, etc.

Another example of importing modules

import math

a = 16
b = 3.14
print(math.sqrt(a))
print(math.sin(b))
print(math.pi)

Output

4.0
0.0015926529164868282
3.141592653589793

1.3. Importing and Renaming Modules

We can import and rename modules.

import math as m
print("The value of pi is", m.pi)

Output

The value of pi is 3.141592653589793

We only usually rename modules when the module name is too long. Renaming the module only applies to the module that has the import statement.

1.4. The from…import Statement in Python

We can import specific components from a module without having to import the entire module. For example:

from math import pi, e
print("The value of pi is", pi)
print("The value of e is", e)

Output

The value of pi is 3.141592653589793
The value of e is 2.718281828459045

In the above example, when using pi and e, the dot operator (.) is not required. And only pi and e in the math module are used.

1.5. Importing All Components from a Module

We can import all components from a module with the asterisk (*).

from math import *
print("The value of pi is", pi)
print("The value of e is", e)

Output

The value of pi is 3.141592653589793
The value of e is 2.718281828459045

All components in the math module can be used without using the dot operator (.).

1.6. Folders That Python Will Search for Modules to Import

When importing a module, Python will look for that module in some directories in the order:

  • The current directory.
  • PYTHONPATH (an environment variable with a list of directories).
  • The default directory depends on the configuration.

A program that prints out the directories of the environment variable

import sys

# importing sys.path
print(sys.path)

Output

['c:\\python-examples', 'C:\\Python\\Python310\\python310.zip', 'C:\\Python\\Python310\\DLLs', 'C:\\Python\\Python310\\lib', 'C:\\Python\\Python310', 'C:\\Python\\Python310\\lib\\site-packages']

1.7. List of Components in a Module

import example
print(dir(example))

Output

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

2. Packages in Python

Package in Python consists of one or more modules. A package is like a directory. A package can contain other packages.

A directory is considered a package by Python when it contains a file named __init__.py. This file can be empty but usually contains the initialization code for the package.

When a large Python program has many modules, we group modules with common characteristics into a package. From there, we can have many different packages. This makes a large program easier to manage and clearer in terms of structure and program logic.

Example package diagram in Python

an example of packages in python
an example of packages in python

Importing a module from a package

We can import modules from packages by using the dot (.) operator. For example, if we want to import the module outfit as shown in the diagram above, we would write the following:

import Robot.Design.outfit

We can also import the outfit module with the from…import statement as follows:

from Robot.Design import outfit

From here, we can use modules based on the knowledge of modules in Python from Part 1.

5/5 - (1 vote)
Previous and next lesson in subject<< Guide to using decorators in PythonUsing the String data type in Python >>

Leave a Reply

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