Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A package is a collection of related modules organized into a directory. A package can contain sub-packages, which can store further sub-packages and modules. A package is simply a way of organizing associated modules together in a way that makes sense.
In this article, we will understand modules and packages in Python, including what is the difference between modules and packages in Python. We will see uses and examples of modules and packages. We will also see the difference between modules and packages.
What is a Module in Python?
In Python, a module is a file that contains Python definitions and statements. The file name is the module name with the ".py" extension at the end. Modules can be imported into other Python codes and provide functions, classes, and other functional codes that can be reused in multiple places.
What is Package in Python?
Whereas, on the other hand, a package is a collection of modules organized in a directory. They are used to create a namespace for modules that work in the same manner. A package is a directory that contains an init.py file, which can be empty or have an initialization code for the package.
Packages can contain sub-packages that are nested directories containing their init.py files and other Python modules. When a package is imported, all sub-packages are also automatically imported.
Uses of Modules and Packages in Python
Below are some of the primary uses of modules and packages in Python:
Code reusability: Modules and packages allow developers to reuse code in multiple parts of their applications or across different applications. Rather than rewriting the same code repeatedly, coders can write it once in a module or package and then import to use it in other parts of the application.
Encapsulation: It provides a way to encapsulate code, which means that it can be wrapped the data and code in a single entity. This helps to improve the overall structure of the codebase.
Namespace management: It allows developers to manage namespaces, i.e., avoiding naming conflicts between different application parts. Namespaces help to ensure that variables, functions, and classes have unique names within their respective modules or packages.
Code organization: It provides a way to organize code into logical groups based on functionality or purpose. This can make it easier to navigate and understand a large codebase.
Third-party libraries: Many third-party libraries are provided in the form of modules or packages. This means the developers can easily add new functionality to their applications without writing everything from scratch.
Examples of Modules and Packages in Python
A module is a file containing Python definitions and statements. In comparison, a package is a hierarchical structure. It defines a single Python application environment that includes modules and sub-packages.
Here are some examples of built-in modules and packages in Python:
Modules:
1. math - provides mathematical functions like sqrt()(for finding square root of a number), sin()(for finding sin of an angle), cos()(for finding cos of an angle), etc. Let us see how it works:
import math
x = math.sqrt(16)
print("Square root of 16 is:",x)
You can also try this code with Online Python Compiler
The output of above code will print the current date and time in Year-month-date Hour-min-sec order:
4. os - provides a way of using operating system-dependent functions like read or write to the file system. Let us see how it works:
import os
file_path = "/path/to/file.txt"
if os.path.exists(file_path):
os.remove(file_path)
print("File deleted.")
else:
print("File does not exist.")
You can also try this code with Online Python Compiler
In the above code, after importing the os module, we set a variable path. Check if that variable exists then delete the file and print file deleted. Otherwise state that the file doesn't exist
5. csv - provides classes for reading and writing to CSV files. Let us see how the code runs:
import csv
file_path = "/path/to/file.csv"
You can also try this code with Online Python Compiler
With the csv module we can access the csv files and use the data in them. As there exists no csv file, therefore, it would give an error.
Packages:
1. numpy - It provides working on large, multi-dimensional arrays and matrices, with a large library of mathematical functions to operate on these arrays. Let us see how it works:
2. pandas - It provides tools for data manipulation and analysis. It also includes reading and writing to various file formats, cleaning data, and performing data aggregation. Let us see how its code works:
The above code will print the data types of all the variables of the csv file. But here as the file doesn't exist therefore, it will not give an error. You can change the file_path to your csv file address and can run the code.
3. matplotlib - It provides tools for creating visualizations and graph plots, for example, line plots, scatter plots, bar plots, and more. Let us see how it works:
Examples of Python modules include os, sys, math, datetime, random, and collections.
What are modules and packages in Python interview questions?
Modules are single Python files; packages are directories containing multiple modules and an __init__.py file.
How many modules are in Python?
Python has over 200 standard library modules, with thousands more available via third-party libraries.
What is the difference between module and class in Python?
A module is a file containing Python code; a class is a blueprint for creating objects within that module.
Conclusion
In this article, we aimed to understand the modules and packages in Python, including what is the difference between modules and packages in Python. We discussed its uses, examples, and differences between them.
Live masterclass
Amazon PowerBI & AI Essentials: Data Visualization Tips
by Abhishek Soni
10 Jul, 2025
01:30 PM
Must-have GenAI skills as developer to not get replaced
by Shantanu Shubham
07 Jul, 2025
08:30 AM
Amazon Data Analyst: Advanced Excel & AI Interview Tips
by Megna Roy
08 Jul, 2025
01:30 PM
Build ChatGPT-like Search Using RAG - Live with Google SDE3
by Saurav Prateek
09 Jul, 2025
01:30 PM
Amazon PowerBI & AI Essentials: Data Visualization Tips
by Abhishek Soni
10 Jul, 2025
01:30 PM
Must-have GenAI skills as developer to not get replaced