EasyExperience level: 0-2 years · 3-5 years1 min read

What are modules and packages, and how does import work?

Reviewed by Gurusankar M.

Asked inInfosysTCSWiproCognizant
#module#package#import#__name__#__init__.py
Report issue

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

python
# 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__ block
Output
15

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

  1. Absolute vs relative imports?
  2. What causes a circular import and how do you fix it?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.