Python Mastery – From Basics to Brilliance / Modules & Packages

Packages

πŸ“¦ Python Packages

A package is a way of organizing related Python modules into a folder.

Think of it like:

πŸ“¦ Package
 β”œβ”€β”€ πŸ“„ module1.py
 β”œβ”€β”€ πŸ“„ module2.py
 └── πŸ“„ module3.py

πŸ—‚οΈ Creating a Package

Example:

my_package/
    __init__.py
    calculator.py
    greeting.py

Here, my_package is the package, while calculator.py and greeting.py are modules.


πŸ“₯ Importing from a Package

from my_package import calculator

calculator.add(10, 5)

Or import something directly:

from my_package.calculator import add

print(add(10, 5))

πŸ“¦ Package vs Module

Module πŸ“„

Package πŸ“¦

Usually a single .py file

Folder containing modules

Contains functions, classes, etc.

Organizes related modules

Example: calculator.py

Example: my_package/

⭐ Remember

  • Module β†’ A Python file

  • Package β†’ A folder containing related Python modules