What are modules and packages, and how does import work?
Reviewed by Gurusankar M.
Short Answer
A module is a single .py file; a package is a folder of modules (traditionally with __init__.py). `import` runs the module once and caches it in sys.modules, so re-imports are cheap. Use `if __name__ == '__main__':` to separate 'run as script' code from 'imported as a module' code.
On this page
Concept Made Simple
Coffee Chat Question
“What are modules and packages, and how does import work?”
Remember It Faster
Mind Map Answer
- Module
- one .py file
- Package
- folder of modules (__init__.py)
- import
- runs once, cached in sys.modules
- __name__
- '__main__' when run directly
Learn by Doing
Hands-on Keyboard
# mymath.py
def add(a, b):
return a + b
if __name__ == "__main__":
print(add(2, 3)) # only when run directly
# other.py
from mymath import add
print(add(10, 5)) # import doesn't run the __main__ block15
Think Beyond the Expected
What If?
What is the point of `if __name__ == '__main__':`?
It lets a file work both as a runnable script and an importable module. When run directly, __name__ is '__main__' and the block executes; when imported, __name__ is the module name, so that block is skipped — preventing your test/CLI code from firing on import.
Real World
Every project is organized into modules/packages; the `__main__` guard is why importing a utility file doesn't accidentally run its demo code — a detail interviewers check that beginners often miss.
Interviewer's Expectation
Keywords they're listening for:
- module=file, package=folder
- import runs once & caches
- __name__=='__main__' guard
- absolute vs relative imports
Common Mistakes
- Naming a file the same as a stdlib module (shadowing)
- Wildcard `from x import *`
- Creating circular imports
Best Practices
- Guard script code with `if __name__ == '__main__'`
- Prefer explicit imports over `import *`
- Keep packages cohesive; avoid circular deps
Follow-up Questions
- Absolute vs relative imports?
- What causes a circular import and how do you fix it?
- How does Python find modules (sys.path)?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Modules (Python) Interview question: "What are modules and packages, and how does import work?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair