Modules and Packages in Python

Modules and Packages in Python

In this article, I am going to discuss Modules and Packages in Python with examples. Please read our previous article where we discussed Decorators and Generators in Python with examples. As part of this article, we are going to discuss the following pointers which are related to Modules and Packages in Python.

  1. What is a module?
  2. Renaming or aliasing a module
  3. dir() function
  4. What is a package?
  5. Advantage
  6. Multiple Programs to understand above concept.
What are Modules in Python?

In python a module means a saved python file. This file can contain a group of classes, methods, functions and variables. Every file with .py or .python extension is called a python file, in turn a module.

Example: Module (addmultiplication.py)

x = 10
def sum(a, b):
   print("Sum of two values: " , (a+b))

def multiplication(a, b):
   print("Multiplication of two values: " , (a*b))

Now addmultiplication.py file is a module. addmultiplication.py module contains one variable and two functions.

What is import keyword in python?

If we want to use other members(variable, function, etc) of a module in your program, then you should import that module by using the import keyword. After importing you can access members by using the name of that module.

Example: demo1.py

import addmultiplication
print(addmultiplication.x)
addmultiplication.sum(1, 2)
addmultiplication.multiplication(2, 3)

Output:

Modules and Packages in Python

Note: Whenever we are using a module in our program, that module’s compiled file will be generated and stored in the hard disk permanently.

Renaming or Aliasing a module in Python:

The syntax for renaming or giving an alias name for the module

Renaming or Aliasing a module in Python

Example: Aliasing module (demo2.py)

import addmultiplication as calculator
print(calculator.x)
calculator.sum(1, 2)
calculator.multiplication(3, 4)

Output:

Aliasing module in Python

from keyword in python:

We can import some specific members of the module by using the from keyword. The main advantage of the from keyword is we can access members directly without using module names.

Example: from keyword (demo3.py)

from add
multiplication import x, sum
print(x)
sum(10,20)

Output:

from keyword in python

Import * in python:

We can import all members of a module as by using import * (symbol).

Example: import * (demo4.py)

from addmultiplication import *
print(x)
sum(10,20)
multiplication(10,20)

Output:

Import * in python

Aliasing members in Python:

We can give alias name to the members of a module as well

Example: Aliasing members of module (demo5.py)

from addmultiplication import x as y, sum as add
print(y)
add(10,20)

Output:

Aliasing members in Python

Once an alias name is given, we should use the alias name only and not the original name.

Example: Aliasing members of module (demo6.py)
from addmultiplication import x as y
print(x)

Output: Modules and Packages in Python

Reloading a module in Python:

By default, a module will be loaded only once even though we are importing multiple times. Let’s consider a module with name module1.

Example: Reloading (module1.py)

print(“This is from module1”)

Output: Reloading a module in Python

Example: Reloading (demo7.py)
import module1
import module1
import module1
import module1
print("This is test module")

Output:

Modules and Packages in Python with examples

The problem in this approach is if a module is updated outside after loading it in our program, then the updated version of the module will not be available to our program. We can solve this problem by reloading modules explicitly based on our requirement wherever needed. We can reload by using the reload() function of the imp module.

Syntax:
import imp
imp.reload(module1)

Example: demo8.py
import module1
import module1
from imp import reload
reload(module1)
reload(module1)
reload(module1)
print("This is test module")

Output:

Reloading Module in Python

In the above program module1 will be loaded 4 times in that 1 time by default and 3 times explicitly. The main advantage of explicit module reloading is we can ensure that updated versions are always available to our program.

dir() function in Python:

We can find members of a module by using the dir() function. For every module at the time of execution, Python interpreter will add some special properties automatically for internal use, example

     __name__
    __package__

This inbuilt function dir() helps us to list out all members, including those which are included by interpreter, of the current module or a specified module.

  1. dir() ===> To list out all members of current module
  2. dir(moduleName) ===> To list out all members of specified module
Example: dir() function (demo9.py)
x=10
y=20
def f1():
   print("Hello")
print(dir())

Output:

dir() function in Python

Example: To display members of specific module (demo10.py)
import module1
import addmultiplication
print(dir(module1))
print(dir(addmultiplication))

Output:

display members of specific module in python

What is a Package in Python?

A package is nothing but a folder or directory which represents a collection of python modules(programs). Any folder or directory, for python to recognize as a package, should contains__init__ .py file.

__init__ .py can be an empty file. A package can contain sub packages also.

Diagrammatic representation of Package in Python:

What is a Package in Python?

Summary of Modules and Packages in Python

  1. Library → A group of packages
  2. Package → A group of modules
  3. Modules → A group of functions or methods and classes.

In the next article, I am going to discuss Lists in Python. Here, in this article, I try to explain Modules and Packages in Python. I hope you enjoy this Modules and Packages in Python article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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