Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate40 min
Creating Python packages hero Module 14 Lesson 8

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

Three steps create folder add init py place module files
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:

bankkit package structure init bankinfo arithops greetmod each py is one module

Step 1 — Create the package folder

text
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:

python
# bankkit/__init__.py

# Can be completely empty

Why 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

python
# 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

python
# bankkit/arithops.py

def sumop(a, b):
    print("sum =", a + b)

def subop(a, b):
    print("sub =", a - b)

greetmod.py

python
# 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.

main.py imports bankkit package bankinfo arithops greetmod modules dot notation

Importing from a package

Method 1 — Import the module

python
import bankkit.bankinfo

print(bankkit.bankinfo.BNAME)
bankkit.bankinfo.simple_interest(100000, 2, 10)

Method 2 — Import with alias

python
import bankkit.arithops as ao

ao.sumop(10, 20)

Method 3 — Import specific names

python
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

Complete project layout main py and bankkit package folder
text
project/
│
├── main.py
│
└── bankkit/
    ├── __init__.py
    ├── bankinfo.py
    ├── arithops.py
    └── greetmod.py

When 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
  • .py file = module
  • __init__.py makes the folder importable
  • Import packages using dot notation: bankkit.bankinfo
  • Next: Reusing Packages — sys.path.append and PYTHONPATH

Loading playground…

Check your understanding

Creating packages quiz

Step 1 to create a package is:

1 / 10