Lesson 08
Creating Python Packages — Folder, __init__.py, and Modules
Learn how to build your own package using a folder, __init__.py, and multiple modules — with a complete bankkit example.
A package is simply a folder that contains Python modules. Instead of keeping all your code in one file, packages organize related modules into a reusable library.
Use the playground and quiz to build and import a bankkit package.
What you will learn
- Create a package in 3 simple steps
- Understand the purpose of
__init__.py - Build a complete bankkit package
- Import modules using dot notation
The 3 steps
| Step | Action |
|---|---|
| 1 | Create a folder — the folder name becomes your package name |
| 2 | Add __init__.py — tells Python the folder is a package (can be empty) |
| 3 | Place your .py files inside — each Python file becomes one module |
Visual 1 — Package structure
This is what your project should look like:
Step 1 — Create the package folder
project/
│
├── main.py
│
└── bankkit/The folder name bankkit becomes the package name you will import later.
Step 2 — Add __init__.py
Inside the package, create this file:
# bankkit/__init__.py
# Can be completely emptyWhy is it needed? It marks the folder as a Python package and allows imports like
import bankkit.bankinfo.
Step 3 — Add your modules
bankinfo.py
# bankkit/bankinfo.py
BNAME = "BOI"
ADDR = "HYD"
def simple_interest(p, t, r):
si = (p * t * r) / 100
print("SI:", si)
print("Total:", p + si)arithops.py
# bankkit/arithops.py
def sumop(a, b):
print("sum =", a + b)
def subop(a, b):
print("sub =", a - b)greetmod.py
# bankkit/greetmod.py
def welcome(name):
print("Welcome to bankkit,", name)Visual 2 — How imports work
The package is the top-level namespace, and modules live inside it.
Importing from a package
Method 1 — Import the module
import bankkit.bankinfo
print(bankkit.bankinfo.BNAME)
bankkit.bankinfo.simple_interest(100000, 2, 10)Method 2 — Import with alias
import bankkit.arithops as ao
ao.sumop(10, 20)Method 3 — Import specific names
from bankkit.bankinfo import BNAME, simple_interest
print(BNAME)
simple_interest(50000, 2, 8)Rule: Package imports use dot notation — packagename.modulename.feature.
Complete project layout
project/
│
├── main.py
│
└── bankkit/
├── __init__.py
├── bankinfo.py
├── arithops.py
└── greetmod.pyWhen main.py and bankkit/ sit in the same folder, Python finds the package automatically.
Common mistakes
| Mistake | Correct |
|---|---|
import bankinfo |
import bankkit.bankinfo |
Missing __init__.py |
Create __init__.py |
| Modules outside the folder | Keep all modules inside bankkit/ |
bankinfo.BNAME without importing |
Import the package first |
Interview cheat sheet
| Question | Answer |
|---|---|
| What is a package? | A folder containing multiple modules |
| Package name comes from? | Folder name |
| Module name comes from? | .py filename |
Why __init__.py? |
Marks the folder as a package |
| Access syntax | package.module.feature |
Key takeaways
- Folder = package
.pyfile = module__init__.pymakes the folder importable- Import packages using dot notation:
bankkit.bankinfo - Next: Reusing Packages —
sys.path.appendandPYTHONPATH
Loading playground…
Check your understanding
Creating packages quiz
Step 1 to create a package is:
1 / 10