Python Modules vs Packages
We offer you a brighter future with placement-ready courses - Start Now!!
In this article, we’ll look at the distinctions between Python modules vs packages. Let’s take a quick look at what they’re both about before we get into their differences. Let’s start with modules in Python.
Modules in Python
A module is a file with the extension.py that contains Python or C executable code. A module is made up of a number of Python statements and expressions. Modules allow us to use pre-defined variables, functions, and classes. This cuts down on development time and allows code to be reused. The majority of modules are intended to tackle specific difficulties for developers and are designed to be succinct and straightforward.
Creating a Module in Python
We can create a module by writing some code in a file and saving that file in a .py extension.
Let us create a module named pythongeeks.py.
Example of module in Python
def display():
print("PythonGeeks")
if __name__ == "__main__":
display()
Output
Importing a Module in Python
We can import a module by using the import keyword
Example of importing a module in Python
import pythongeeks pythongeeks.display()
Output
We can import every object in a module by using the asterisk * operator.
Example of importing a module in Python
from pythongeeks import * display()
Output
Likewise, we can import a specific function from a module.
Example of importing a module in Python
from pythongeeks import display display()
Output
We can also alias a module while importing
Example of importing a module in Python
import pythongeeks as pg pg.display()
Output
dir() in Python
Python has a built-in function called dir(). It accepts a module name as an input and returns a sorted list of all attributes and functions contained in the provided module.
Example of dir() in Python
import pythongeeks print(dir(pythongeeks))
Output
How Does Python Import a Module
To import a module, Python first searches in the current directory. If the module doesn’t exist in the current directory, it then searches in all the directories present in the PYTHONPATH variable. If it still can’t find the directory, it then proceeds to the default directory. It raises a ModuleNotFoundError if it doesn’t find the module in any of the mentioned directories. We can see all the directories in the PYTHONPATH variable by using the following code.
Example of PYTHONPATH variable in Python
from os import environ, pathsep print(environ['PYTHONPATH'].split(pathsep))
Output
Packages in Python
To provide an application development environment, a python package establishes a hierarchical directory structure with several modules and sub-packages. They are nothing more than a bundle of modules and sub-packages.
Creating and Importing a Package
To create a Python package, we need to create a directory with a __init__.py file and a module. Suppose we have created a package named website with the previously created module pythongeeks.py in it. We can import the website package by using the import keyword and a dot operator.
Example of importing a package in Python
import website.pythongeeks pythongeeks.display()
Output
Similar to modules, we can also alias while importing.
Example of importing a package in Python
import website.pythongeeks as pg pg.display()
Output
Python Modules vs Packages
The following are some of the distinctions between Modules and Packages:
- A Package is a directory containing numerous modules and sub-packages, whereas a Module is a.py file containing Python code.
- An __init__ .py file is required to create a package. There is no such necessity when it comes to creating modules.
- We can import all objects in a module at once by using the asterisk (*) operator but we can’t import all modules in a package at once.
Packages and Modules and Functions
The principle is the same for functions, modules, and packages. They want to make it as simple as possible for code to be reused.
Their core idea is the same, despite the fact that they appear and operate differently.
Multiple Python statements and expressions make up a function. Multiple Python functions make up a module and multiple Python modules make up a Package. The below image illustrates this precisely.
Python Interview Questions on Python Modules vs Packages
Q1. Write a program to import the math module and print the pi value.
Ans 1. Complete code is as follows:
import math print(math.pi)
Output
Q2. Write a program to import all the objects in the random module and print a random integer between 20 and 30 (including).
Ans 2. Complete code is as follows:
from random import * print(randint(20, 30))
Output
Q3. Write a program to import the numpy package and print the log value of 10.
Ans 3. Complete code is as follows:
import numpy print(numpy.log(10))
Output
Q4. Write a program to import the sub-package plotting from the package pandas and print the first 3 elements of the list returned by dir() function.
Ans 4. Complete code is as follows:
import pandas.plotting print(dir(pandas.plotting)[:3])
Output
Q5. Create a package human with a module man.py. The module contains a function walk() that prints the string “Man is walking”. Create a main.py and run the function after importing the module from the package.
Ans 5. Complete code is as follows:
File: human/man.py
Code
def walk():
print("Man is walking")
File: main.py
Code
from human.man import walk walk()
Output
Conclusion
In this article, we learned the differences between python modules and python packages. We also discussed the similarities between them. If you have any further queries, please post them in the comments area.
