π¦ 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.pyHere, 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 | Folder containing modules |
Contains functions, classes, etc. | Organizes related modules |
Example: | Example: |
β Remember
Module β A Python file
Package β A folder containing related Python modules