Python Modules and Packages

What are the modules in Python?

A module in Python is defined as files containing python functions, classes or variables which can be used by other programs. When programs grow large in size, we use modules to break them down and make a useful component that can be reused later.

How do I create a module in Python?

For example, if we make calculator.py that contains helpful functions to perform various calculations, we can import this module in another python file where its module name would be a calculator.

Let us create a module for storing our calculations function definitions so that we can use it later in our main.py file.

""" Calculator Module (calculator.py) """


# Function to add two numbers
def add(x, y):
    return x + y


# Function to subtract two numbers
def subtract(x, y):
    return x - y


# Function to multiply two numbers
def multiply(x, y):
    return x * y


# Function to divide two numbers
def divide(x, y):
    return x / y

Here, we have defined four functions to perform basic mathematical operations inside a module named calculator. These functions take in two numbers and return their sum, difference, multiplication, and division.

How to import modules in Python?

Now as we have defined our module, to import that module we use “import” keyword in python. The syntax of import module is

import module1,module2,…….. module n
In our example above, we can use simply import calculator in our main.py file and use the dot operator to call the functions defined inside it as in below code:-
import calculator

num1 = 10
num2 = 5

print("Addition: ", calculator.add(num1, num2))
print("Subtraction: ", calculator.subtract(num1, num2))
print("Multiplication: ", calculator.multiply(num1, num2))
print("Division: ", calculator.divide(num1, num2))

The output of the above program is:-

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0

What is the from-import statement?

We can import specific functions or classes from the module using from import statement.

from < module-name> import <name 1>, <name 2>..,<name n>
Let us consider the above example using the from import to import add function only from calculator.py
from calculator import add

num1 = 10
num2 = 5

print("Addition: ", add(num1, num2))

The output of the above program is:-

Addition: 15
We can also import all attributes from module using *
from <module> import *

How to rename a module in Python?

We can rename module under different name as per our convineince.

import <module-name> as <specific-name>
Let us consider the above example using a different name for the calculator:-
import calculator as cal

num1 = 10
num2 = 5

print("Addition: ", cal.add(num1, num2))

The output of the above program is:-

Addition: 15

The dir() built-in function

To get all the list of names defined in the passed module, we can use dir() function. Let us see the following example:-

import calculator

print(dir(calculator))

The output of the above program is:-

[‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘add’, ‘divide’, ‘multiply’, ‘subtract’]

The reload() function

For a given session, a module is imported once only in Python. This is a good practice in python as it gives efficiency to the program. But, if the module is dynamically changed during the program execution, we may require it to load again. For that, we use the reload() function. The syntax is

reload(<module-name>)

Python packages

The package hierarchy allows python program to be broken into the various folder to organize the project. The package is a folder that contains sub-packages, modules, and sub-modules. A directory must contain a file named __init__.py in order for Python to consider it as a package. This file can be left empty but we generally place the initialization code for that package in this file.

Let us consider the following directory structure for our Game Package which contains three sub packages:-

  • Sound
  • Image
  • Level

 

Importing module from a package

We import modules from the package using a dot operator

import Game.Level.start
SHARE Python Modules and Packages

You may also like...

Leave a Reply

Your email address will not be published.

Share