What Is the Difference Between a Python Module and a Package?
Understand the difference between a Python module and a package, how __init__.py works, and how imports resolve, with clear examples for interviews.
Expected Interview Answer
A module is a single .py file containing Python code, while a package is a directory of related modules that includes an __init__.py file and can be imported as one unit.
Modules let you split code into reusable files that you import with `import module_name`. A package groups multiple modules under a common namespace using a directory structure, and the presence of __init__.py (even empty in modern Python) signals to the interpreter that the directory should be treated as importable. Packages can nest further into sub-packages, giving large codebases a clear hierarchy such as `package.subpackage.module`.
- Keeps related code organized into files and folders
- Avoids naming collisions via namespacing
- Enables selective imports of just what you need
- Scales from small scripts to large applications
- Matches how third-party libraries are distributed
AI Mentor Explanation
A module is like a single training drill sheet — one focused skill such as batting stance — while a package is the full coaching manual binder that groups batting, bowling, and fielding drill sheets under labelled dividers. The binder's cover page tells the academy this collection is ready to hand out as one kit, just as __init__.py marks a folder as importable.
Step-by-Step Explanation
Step 1
A module is one file
Any .py file is a module and can be imported directly with `import filename`.
Step 2
A package is a directory
A folder of modules becomes a package once it can be imported as a namespace.
Step 3
__init__.py marks the boundary
Its presence (even empty) tells the interpreter to treat the folder as a package; Python 3.3+ also supports implicit namespace packages without it.
Step 4
Dotted access
Sub-modules and sub-packages are reached with dot notation, e.g. `package.subpackage.module`.
Step 5
Import styles
You can `import package.module`, `from package import module`, or `from package.module import name` depending on what you need.
What Interviewer Expects
- Defines a module as a single .py file
- Defines a package as a directory of modules
- Mentions the role of __init__.py
- Knows namespace packages exist without __init__.py in Python 3
- Can demonstrate an import statement for each
Common Mistakes
- Saying a package is just 'a bigger module'
- Forgetting __init__.py's role entirely
- Confusing packages with third-party distributions on PyPI
- Not knowing implicit namespace packages exist
Best Answer (HR Friendly)
“A module is a single Python file, and a package is a folder of related modules grouped together so they can be imported and organized as one unit, much like keeping related documents in labeled folders instead of loose papers.”
Code Example
# module.py
def greet(name):
return f"Hello, {name}!"
# mypackage/
# __init__.py
# greetings.py
# farewells.py
from mypackage import greetings
print(greetings.greet("SkillVeris"))Follow-up Questions
- What is a namespace package and how does it differ from a regular package?
- How does Python resolve an import statement internally (sys.path)?
- What goes inside __init__.py in practice?
- How do relative imports differ from absolute imports?
- What is the difference between a package and a distribution on PyPI?
MCQ Practice
1. What is a Python module?
A module is simply one Python file that can be imported by name.
2. What historically marks a directory as a Python package?
The presence of __init__.py (even empty) traditionally signals that a directory is importable as a package.
3. Which import reaches a module inside a sub-package?
Python uses dot notation to traverse packages and sub-packages down to a specific module.
Flash Cards
What is a module? — A single .py file that can be imported.
What is a package? — A directory of modules importable as one namespace.
What does __init__.py do? — Marks a directory as a regular package (can also run init code).
Can a package exist without __init__.py? — Yes — Python 3.3+ supports implicit namespace packages.