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 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 file.

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 file and use the dot operator to call the functions defined inside it as in below code:-

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

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:-

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:-

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


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 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. Required fields are marked *