EasyπŸ‘€ 0-2 yearsπŸ‘€ 3-5 years 1 min read

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

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.

β˜•Coffee Chat Question

Concept Made Simple

β€œWhat are modules and packages, and how does import work?”

🧠Mind Map Answer

Remember It Faster

Module→one .py file
Package→folder of modules (__init__.py)
import→runs once, cached in sys.modules
__name__β†’'__main__' when run directly

⌨️Hands-on Keyboard

Learn by Doing

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

πŸ”₯What If?

Think Beyond the Expected

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

  • 1Absolute vs relative imports?
  • 2What causes a circular import and how do you fix it?
  • 3How does Python find modules (sys.path)?

🧩Related Technologies

sys.modulespip__init__.pyimportlib

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission β€” at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions