Skip to content
Home Β» Python Packages

Python Packages


⭐ Python Packages

A package in Python is a way of organizing related modules into a directory (folder) structure.
Packages make it easier to structure large programs and avoid name conflicts.

πŸ‘‰ Package = A folder containing multiple Python modules + an __init__.py file

In simple words:

  • Module = A single Python file (.py)
  • Package = A folder containing modules

⭐ Why Do We Need Packages?

βœ” To group related modules
βœ” For better code organization
βœ” To avoid naming conflicts
βœ” To support large-scale project development
βœ” To reuse code across multiple projects
βœ” To make distribution easier


⭐ Structure of a Package

A typical package structure looks like this:

myproject/
    mathpkg/
        __init__.py
        addition.py
        subtraction.py
        multiply.py
    main.py

πŸ“Œ What is __init__.py?

  • Special file that identifies the folder as a Python package
  • Can be empty
  • May contain initialization code
  • Runs when the package is imported

Before Python 3.3, it was mandatory; now it is optional but still recommended.


⭐ Creating a Python Package (Step-by-Step)


βœ” Step 1: Create a Folder (Package)

Create a folder named myutils

myutils/

βœ” Step 2: Add __init__.py file

myutils/
    __init__.py   # can be empty

βœ” Step 3: Add Modules Inside the Package

Example modules:

addition.py

def add(a, b):
    return a + b

subtraction.py

def sub(a, b):
    return a - b

βœ” Step 4: Use the Package in Another Program

from myutils.addition import add
from myutils.subtraction import sub

print(add(10, 20))
print(sub(50, 15))

⭐ Importing Packages

There are many ways to import from packages:


1. Import Entire Package

import myutils

2. Import Module from Package

import myutils.addition

3. Import Function Directly

from myutils.addition import add

4. Import All Modules

from myutils import *

(works only if __init__.py contains __all__ list)

Example:

__all__ = ["addition", "subtraction"]

⭐ Built-in Python Packages

Many Python libraries come packaged:

PackageDescription
osOperating system interface
sysSystem parameters
jsonJSON handling
numpyScientific computing
pandasData analysis
matplotlibPlotting
tkinterGUI toolkit
collectionsSpecialized data structures

⭐ Installing Third-Party Packages

Use pip, Python’s package manager:

pip install numpy
pip install pandas

To uninstall:

pip uninstall numpy

To list installed packages:

pip list

⭐ Difference Between Module and Package (Exam Point)

ModulePackage
Single .py fileFolder containing modules
Small code unitLarge code grouping
No directory requiredRequires directory structure
Cannot contain submodulesCan contain submodules and subpackages
Example: math.pyExample: numpy package

⭐ Advantages of Packages

βœ” Organizes large projects
βœ” Avoids name conflicts
βœ” Promotes code reusability
βœ” Supports modular programming
βœ” Allows distribution of libraries
βœ” Makes maintenance easier


⭐ Exam-Ready Short Answer

A Python package is a collection of related modules organized in a directory containing an __init__.py file. Packages help structure large programs, avoid naming conflicts, and promote code reusability. Example: NumPy, Pandas, Matplotlib, Requests.