Python, properly · Chapter 19 of 42

Modules and packages

By the end of this chapter you can split a program into modules, import what you need from them, write files that work both as a script and as a module, and group modules into a package with relative imports. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Code that holds up
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

Kunal opened your folder and counted: the function that adds GST is copied into six scripts. Last month the rate changed and you updated five of them, so one of Rafiq's reports was quietly wrong. Copies drift apart; one definition, used everywhere, cannot.

So far every program has been a single file. Real programs are many files that use each other's code, and Python's word for such a file is a module. The standard library you have used (json, csv, pathlib) is made of modules too; now you write your own.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    pricing.py: a module that stays quiet

    One definition of GST for every script. In pricing.py, set GST_RATE to 5 (percent) and write gst(amount): the GST on amount rupees, rounded down to whole rupees, so gst(200) is 10 and gst(250) is 12. Use GST_RATE in it, not the number 5.

    Run as a script (python3 pricing.py), it prints exactly one line: gst(200) = 10

    Imported by another file, it prints nothing at all.

  2. Task 2

    bill.py: use the module, do not copy it

    bill.py prints the bill for 2 Misal Pav at Rs 120 and 1 Masala Chaas at Rs 40, using pricing.py's gst, exactly:

    Subtotal: Rs 280

    GST: Rs 14

    Total: Rs 294

    Import pricing (either form of import), and do not write your own GST calculation in bill.py: the check also runs bill.py next to a pricing.py with a different rate, and the bill must follow it.

  3. Task 3

    money.py: a module inside a package

    money.py is moving into the dabba package, beside rates.py (given: GST_RATE and DELIVERY_FEE). In money.py, import GST_RATE and DELIVERY_FEE from the rates module of the same package with a relative import, and write with_fees(subtotal): the subtotal, plus GST on it rounded down, plus the delivery fee. With rates.py's values, with_fees(280) is 324.

    Under the main guard, print exactly: with_fees(280) = 324

    The check puts money.py in a dabba folder with an empty __init__.py and a rates.py of its own (other values), then runs python3 -m dabba.money and imports dabba.money.

    Careful: python3 money.py on its own stops with an ImportError about a relative import. That is expected: a relative import needs its package, which is why the check uses -m.

  4. Task 4 · interview question

    Interview: lucky_draw.py, the file that broke import random

    Bhavna keeps her coupon codes in random.py, and lucky_draw.py, which should seed Python's random module with 2026 and print one of her codes, stops with an AttributeError. Read the whole traceback, then fix the folder:

    rename random.py to coupon_codes.py (keep its CODES list as it is), change lucky_draw.py to get CODES from coupon_codes, and keep import random for Python's own random module.

    lucky_draw.py then prints exactly one line: Today's lucky code: <code>, the code random.choice picks from CODES after random.seed(2026).

    Be ready to explain why a file named random.py breaks import random.

What you will learn

  • import: running a module once and using its names, three ways.
  • Where Python looks: sys.path, and why the script's own folder comes first.
  • if __name__ == "__main__":: a file that is a script when run and a quiet module when imported.
  • Packages: a folder of modules, python3 -m, relative imports, and how a project is laid out.

More in Code that holds up

Build "Modules and packages" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.