Python Basics — Day 19 Modules & Packages (import, from, __main__)
Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey
Building skills in Python and full-stack development, with a focus on web apps and system design
Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering
From Python basics to full-stack projects, sharing my progress as I grow into a developer
Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development
Starting from this post , I’ve changed the format form explaining concepts to presenting problems and solving them. Personally, I’ve found that I learn best when I face a problem and work through the process of fixing it.
Learning Goal
Learn how to split a Python project into multiple files using modules and packages,
and understand how import, from, and __main__ work in a real-world context.
01. Problem Scenario
Your calculator script is getting too messy.
All your functions are in one file, making it hard to maintain.
Let’s refactor the project by separating logic into modules, organizing them into packages,
and controlling code execution using __main__.
02. Step 1 – Creating a Module
In Python, a single .py file is a module.
We’ll create a new file calc.py to store the calculator’s logic.
calc.py
def add(a, b):
return a + b
def sub(a, b):
return a - b
def mul(a, b):
return a * b
def div(a, b):
return a / b
main.py
import calc
print("3 + 5 =", calc.add(3, 5))
print("10 - 7 =", calc.sub(10, 7))
Now main.py only handles program execution,
while calc.py manages the logic, making the code cleaner and easier to reuse.
03. Step 2 – Different Ways to Import
# Import the entire module
import math
print(math.sqrt(16))
# Import specific functions
from math import sqrt, pi
print(sqrt(25), pi)
# Use an alias
import math as m
print(m.pow(2, 3))
Using from and as can make your code shorter and more readable.
04. Step 3 – Building a Package
As your project grows, you can organize related modules inside a folder, called a package.
my_package/
__init__.py
math_ops.py
string_ops.py
main.py
math_ops.py
def add(a, b):
return a + b
main.py
from my_package import math_ops
print(math_ops.add(2, 3)) # 5
The presence of __init__.py tells Python that this folder should be treated as a package.
05. Step 4 – Understanding __name__ and __main__
Python can tell whether a file is being executed directly or imported as a module.
You can control behavior with this structure:
# test.py
def hello():
print("Hello!")
if __name__ == "__main__":
hello()
Explanation:
When a file runs directly:
__name__ == "__main__"When imported:
__name__ == "test"
$ python test.py # Output: Hello!
>>> import test # No output
This prevents test code from running automatically when a module is imported.
06. Step 5 – Installing External Libraries
You can install third-party modules from the Python Package Index (PyPI) using pip.
pip install requests
import requests
res = requests.get("https://api.github.com")
print(res.status_code)
The requests library is commonly used for handling HTTP requests and APIs.
07. Step 6 – Extended Practice Ideas
Add new functions to
calc.pypower(a, b)→ exponentiationsqrt(n)→ square root
Import only what you need in
main.pyfrom calc import add, subAdd a startup message in
main.pyif __name__ == "__main__": print("Calculator App Starting...")
This message should appear only when the script runs directly,
not when imported from another file.
Key Takeaways
| Concept | Description |
| Module | A single .py file used to organize related code |
| Package | A folder containing multiple modules (__init__.py required) |
| import / from | Used to include modules and their functions |
| name / main | Distinguishes direct execution from import |
| pip install | Installs external Python libraries |