Stop 06 · Files, Errors & ModulesTopics · 7/9

Modules & Imports

Part of the Python Roadmap

In short

Once a program grows past a hundred lines, one giant file becomes a nightmare. Modules let you split code into neat files and reuse it anywhere with a single import line.

Any .py file is a module, and import lets one file use functions and classes from another. You can import a whole module, specific names with from ... import, or rename with as.

The jaadu opens one of several neatly stacked cardboard boxes in a moving truck and lifts out a small tool.

Think of it like this

Labelled boxes in a moving truck

When you shift houses, you don't throw everything into one heap. Kitchen things go in the 'kitchen' box, books in the 'books' box. Later you open just the box you need. Each .py file is a labelled box, and import opens it.

Watch it happen

What happens on import pricing

main.py
Python
pricing.py

import pricing

1/4main.py asks for the pricing module.

The big ideas

What you need to know

A file is a module

Save functions in pricing.py and any file in the same folder can import pricing.

Three import styles

import math, from math import sqrt, and import numpy as np. Choose clarity over saving keystrokes.

Avoid import *

from module import * dumps unknown names into your file and can silently overwrite your own.

Runs once, cached

A module's top-level code runs on the first import only. Later imports reuse the same object.

See it in code

Walk through the code

python
1# pricing.py2GST_RATE = 0.183 4def add_gst(amount: float) -> float:5    return round(amount * (1 + GST_RATE), 2)6 7def discount(amount: float, percent: float) -> float:8    return amount * (1 - percent / 100)9 10 11# main.py12import pricing13from pricing import discount14import statistics as stats15 16price = 100017print(pricing.add_gst(price))      # 1180.018print(discount(price, 10))         # 900.019print(pricing.GST_RATE)            # 0.1820print(stats.mean([200, 350, 500])) # 350

1/5pricing.py is a normal Python file with a constant and two functions.

Two files: pricing.py holds reusable helpers, main.py imports them three different ways.

Save yourself the pain

Common mistakes

Oops

Naming your own file random.py or math.py and then import random gives strange errors.

Do this instead

Your file shadows the standard module. Pick unique names like dice_game.py.

Oops

Two modules importing each other at the top (circular import).

Do this instead

Move shared code into a third module that both import.

Your mission

Split your project

~25 min

Quick check

You import the same module twice in one program. How many times does its top-level code run?

Go deeper

  1. 1Move helper functions into utils.py and import them into main.py.
  2. 2Compare import math with from math import sqrt.
  3. 3Avoid from module import * because it hides where names come from.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge