←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Python Packages: Pre-defined, User-defined & Package vs Folder

Study material and explanations for Unit 3

Python Packages: Pre-defined, User-defined & Package vs Folder

As a Python project grows, a single program may end up having dozens of .py modules. Keeping 40 .py files dumped inside one directory becomes messy. To organize related modules cleanly, Python uses Packages.


1. What is a Package in Python?

A Package is a directory (folder) that contains multiple related Python modules (.py files) along with a special initialization file named __init__.py (and optionally sub-packages).

Simple Analogy:

  • Function: A single chapter or recipe.
  • Module (.py file): A book containing related chapters (functions & classes).
  • Package (Directory with __init__.py): A bookshelf/library section that organizes multiple related books (modules).
Organizing a Python Project into a Package (college_pkg)
📁 my_project/
├── 📄 main.py                  # Entry point of the application
└── 📦 college_pkg/            # Python Package
    ├── 📄 __init__.py        # Marks this folder as a Python Package
    ├── 📄 students.py        # Module 1: Student admission & records
    ├── 📄 faculty.py         # Module 2: Teacher details
    └── 📄 exams.py           # Module 3: Marks & result calculation

Role of __init__.py:

  • The __init__.py file tells Python that the directory should be treated as an importable Package.
  • It can be completely empty, or it can contain package initialization code and specify which modules to expose when someone imports the package.

2. Types of Packages in Python

Just like modules, Python packages are classified into two types:

A. Pre-Defined (Built-in & Third-Party) Packages

Packages created by the Python community or core developers that provide ready-made functionality:

  1. Standard Library Packages (Pre-installed with Python):
    • tkinter (GUI development)
    • urllib (Working with URLs and HTTP requests)
    • xml and json (Data parsing)
    • unittest (Automated software testing)
  2. Third-Party Packages (Installed via pip install <package> from PyPI):
    • NumPy: Scientific computing & multi-dimensional arrays.
    • Pandas: Data analysis & tabular DataFrames.
    • Matplotlib: Plotting 2D graphs and charts.
    • Django / Flask: Web application development.

B. User-Defined Packages

Custom packages created by a programmer to structure their own application into logical folders (e.g., ecommerce/cart.py, ecommerce/payment.py).


3. Step-by-Step: Creating and Using a User-Defined Package

Let’s create the college_pkg package shown above and use it inside main.py.

Step 1: Create folder college_pkg and add __init__.py

Inside college_pkg/__init__.py:

# File: college_pkg/__init__.py
print("college_pkg initialized!")

Step 2: Create modules inside college_pkg/

Inside college_pkg/students.py:

# File: college_pkg/students.py
def register_student(name, course):
    return f"Student {name} enrolled in {course}."

Inside college_pkg/exams.py:

# File: college_pkg/exams.py
def check_result(marks):
    return "PASS" if marks >= 40 else "FAIL"

Step 3: Import from the Package inside main.py

# File: main.py
from college_pkg import students
from college_pkg.exams import check_result

print(students.register_student("Aarav", "BCA"))
print("Exam Status:", check_result(76))

4. Package vs Folder (Directory)

Students often ask: “Is every folder on my computer a Python package?” No! Here is the exact difference:

Feature Python Package Normal Folder (Directory)
Definition A namespace directory containing Python .py modules and an __init__.py file A general Operating System container used to store any type of files (images, docs, videos, code)
Role of __init__.py Contains __init__.py to initialize package-level state Does not contain __init__.py
Purpose Organizes Python modules so they can be imported using dot notation (import pkg.mod) Organizes files on disk for the user/OS
Importability Can be imported in Python scripts using import Cannot be imported as a Python module unless it contains Python code

5. Quick Comparison: Module vs Package

Feature Module Package
What is it? A single Python file with a .py extension A directory (folder) containing multiple .py modules and __init__.py
Scope Groups related functions, classes, and variables Groups multiple related modules and sub-packages
Example math.py, random.py, students.py numpy, pandas, matplotlib, college_pkg/