Skip to content
Learn Netverks

Lesson

Step 11/36 31% through track

modules-imports

Modules and imports

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Modules and imports: the syntax, patterns, and safety habits you need before advancing in Python.

Teams still ship Modules and imports in Python codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Modules and imports in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

A module is a .py file; packages are directories with __init__.py (Python 3.3+ namespace packages differ). Use import to reuse code—like ES modules in JavaScript or packages in Java.

Import styles

import math
from datetime import date
import json as js

Prefer explicit imports over from module import *—it pollutes namespaces and hides origins.

__name__ == "__main__"

def main():
    print("running as script")

if __name__ == "__main__":
    main()

Code under this guard runs only when executed directly, not when imported—essential for reusable modules.

Important interview questions and answers

  1. Q: import vs from import?
    A: import math requires math.sqrt; from math import sqrt binds sqrt directly.
  2. Q: What does __name__ hold when imported?
    A: The module's dotted name (e.g., mypackage.utils), not __main__.

Self-check

  1. What guard runs code only as a script?
  2. Why avoid import *?

Tip: Prefer explicit imports—import json beats from json import * for readable codebases.

Interview prep

import vs from import?

import math needs math.sqrt; from math import sqrt binds sqrt directly.

__name__ when imported?

Module dotted path—not __main__—so guarded code does not run on import.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • import vs from?
  • Circular import?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump