DEV Community

Cover image for Python basics - Day 19
Sabin Sim
Sabin Sim

Posted on

Python basics - Day 19

Day 19 – Modules & Packages (import, from, main)

Project: Build a modular “Math & Greeting Toolkit” that imports and reuses your own functions.


01. Learning Goal

By the end of this lesson, you will be able to:

  • Understand what modules and packages are
  • Import both built-in and custom Python modules
  • Use from-import, aliasing, and main effectively
  • Organize your code for reusability and clarity

02. Problem Scenario

As your projects grow, you’ll want to organize your code into smaller, reusable files.

Instead of putting everything into one Python file, you’ll learn to use modules and packages to make your project scalable and clean.


03. Step 1 – What is a Module?

A module is simply a Python file (.py) that contains functions, classes, or variables.

You can import it into another file to reuse its code.

Example: using Python’s built-in math module

import math

print(math.sqrt(16))   # 4.0
print(math.pi)         # 3.141592...
Enter fullscreen mode Exit fullscreen mode

04. Step 2 – Different Ways to Import

You can import modules in multiple ways depending on your use case.

# Import the entire module
import math
print(math.sqrt(25))

# Import specific functions
from math import sqrt, pi
print(sqrt(25), pi)

# Use an alias for convenience
import math as m
print(m.sqrt(36))
Enter fullscreen mode Exit fullscreen mode

05. Step 3 – Creating Your Own Module

You can create your own Python file and reuse it as a module.

calc.py

def add(a, b):
    return a + b

def sub(a, b):
    return a - b
Enter fullscreen mode Exit fullscreen mode

main.py

import calc

print(calc.add(3, 5))   # 8
print(calc.sub(10, 7))  # 3
Enter fullscreen mode Exit fullscreen mode

When you import calc, Python automatically runs that file once and gives access to its functions.


06. Step 4 – What is a Package?

A package is a folder that contains multiple modules.
It must include an __init__.py file to be recognized as a package.

my_package/
    __init__.py
    math_ops.py
    string_ops.py
Enter fullscreen mode Exit fullscreen mode
from my_package import math_ops
print(math_ops.add(2, 3))
Enter fullscreen mode Exit fullscreen mode

Packages make it easy to group related modules together for large-scale projects.


07. Step 5 – The __name__ and __main__ Check

Every Python file can act as either:

  1. A script (run directly)
  2. A module (imported into another file)

You can distinguish them using if __name__ == "__main__":

# test.py
def hello():
    print("Hello!")

if __name__ == "__main__":
    hello()
Enter fullscreen mode Exit fullscreen mode

When executed directly:

$ python test.py
Hello!
Enter fullscreen mode Exit fullscreen mode

When imported:

import test
# Does not print automatically
Enter fullscreen mode Exit fullscreen mode

This allows you to include test code or demos that run only when executed directly.


08. Step 6 – Installing External Libraries

You can install third-party libraries using pip.

pip install requests
Enter fullscreen mode Exit fullscreen mode

Then, use it in your project:

import requests

res = requests.get("https://api.github.com")
print(res.status_code)
Enter fullscreen mode Exit fullscreen mode

09. Step 7 – Practice Examples

Example 1: Standard Module

import random

print(random.randint(1, 6))   # Roll a dice
Enter fullscreen mode Exit fullscreen mode

Example 2: Custom Module

greet.py

def say_hello(name):
    return f"Hello, {name}!"
Enter fullscreen mode Exit fullscreen mode

main.py

import greet
print(greet.say_hello("Sabin"))
Enter fullscreen mode Exit fullscreen mode

10. Step 8 – Mini Project: Math & Greeting Toolkit

Let’s build a simple package that combines math and greeting modules.

project structure

toolkit/
    __init__.py
    math_tools.py
    greet_tools.py
main.py
Enter fullscreen mode Exit fullscreen mode

math_tools.py

def add(a, b):
    return a + b

def multiply(a, b):
    return a * b
Enter fullscreen mode Exit fullscreen mode

greet_tools.py

def greet(name):
    return f"Welcome, {name}!"
Enter fullscreen mode Exit fullscreen mode

main.py

from toolkit import math_tools, greet_tools

print(math_tools.add(10, 5))
print(greet_tools.greet("Sabin"))
Enter fullscreen mode Exit fullscreen mode

Output:

15
Welcome, Sabin!
Enter fullscreen mode Exit fullscreen mode

11. Reflection

You have learned how to:

  • Import built-in and custom modules
  • Organize multiple files into packages
  • Use __main__ to separate test code from imports
  • Build your own Math & Greeting Toolkit for reusable code

Next → Day 20 – Python Standard Library
Explore the most useful built-in modules like math, random, datetime, and os.

Top comments (0)