What are Python modules and packages?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Python topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
A module is a single Python file (.py) that can contain functions, classes, and variables. Import a module: import math or from math import sqrt, pi or import math as m. A package is a directory containing an __init__.py file (can be empty) and multiple modules: from mypackage.utils import helper. The __init__.py is executed when the package is imported. Python's standard library is a collection of built-in modules: os, sys, json, datetime, collections, itertools, functools, pathlib, re, logging. Third-party packages are installed with pip: pip install requests. Namespaces prevent name conflicts between modules. The __name__ == "__main__" guard lets a module run standalone or be imported without executing top-level code.
Pro Tip
This topic has Python-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.