Friday, December 27, 2024
Google search engine
HomeLanguagesWhat is the difference between Python’s Module, Package and Library?

What is the difference between Python’s Module, Package and Library?

In this article, we will see the difference between Python’s Module, Package, and Library. We will also see some examples of each to things more clear.

What is Module in Python?

The module is a simple Python file that contains collections of functions and global variables and with having a .py extension file. It is an executable file and to organize all the modules we have the concept called Package in Python. 

Examples of modules:

  1. Datetime
  2. Regex
  3. Random etc.

Example: Save the code in a file called demo_module.py 

Python3




def myModule(name):
    print("This is My Module : "+ name)


Import module named demo_module and call the myModule function inside it. 

Python3




import demo_module
 
demo_module.myModule("Math")


Output:

This is My Module : Math

What is Package in Python?

The package is a simple directory having collections of modules. This directory contains Python modules and also having __init__.py file by which the interpreter interprets it as a Package. The package is simply a namespace. The package also contains sub-packages inside it. 

Examples of Packages: 

  1. Numpy 
  2. Pandas

Example:

Student(Package)
| __init__.py (Constructor)
| details.py (Module)
| marks.py (Module)
| collegeDetails.py (Module)

What is Library in Python

The library is having a collection of related functionality of codes that allows you to perform many tasks without writing your code. It is a reusable chunk of code that we can use by importing it into our program, we can just use it by importing that library and calling the method of that library with a period(.). However, it is often assumed that while a package is a collection of modules, a library is a collection of packages.

Examples of Libraries: 

  1. Matplotlib
  2. Pytorch
  3. Pygame
  4. Seaborn etc.

Example: 

Importing pandas library and call read_csv method using an alias of pandas i.e. pd. 

Python3




import pandas as pd
 
df = pd.read_csv("file_name.csv")


RELATED ARTICLES

Most Popular

Recent Comments