Modules, packages, the import system, pip, and the script-vs-import execution guard.
Question 1: What is a module?
Ans
A module is a Python file containing definitions and executable statements that can be imported by other code.
Example
python
# math_utils.py
def add(a, b): return a + b
# another file
from math_utils import add
print(add(2, 3))
Important Point
Keep module-level side effects small; use an `if __name__ == "__main__"` guard for script-only execution.
Question 2: What is a package?
Ans
A package is a structured collection of Python modules, normally represented by directories and importable package namespaces.
Example
text
shop/
__init__.py
orders.py
users.py
Important Point
Modern Python can support namespace packages without an `__init__.py`, but regular packages commonly include one.
Question 3: What is `__name__` == '`__main__`'?
Ans
When a Python file is run directly, its __name__ is set to __main__. When imported, its __name__ is the module name. The guard separates script execution from importable definitions.
Example
python
def main():
print("Running app")
if __name__ == "__main__":
main()
Important Point
This prevents the guarded code from running merely because another module imports the file.
Question 4: What is pip?
Ans
pip is the standard package installer commonly used to install Python packages from package indexes and other sources.
Example
bash
python -m pip install requests
Important Point
Using `python -m pip` helps ensure pip belongs to the intended Python interpreter/environment.
Question 5: What is a virtual environment?
Ans
A virtual environment isolates a project's Python interpreter context and installed packages from other projects.
Example
bash
python -m venv .venv
# activate it according to the operating system
python -m pip install requests
Important Point
Create separate environments for projects with different dependency requirements.
Question 6: What is requirements.txt?
Ans
A requirements file records project dependencies so another environment can install compatible packages with pip.
Example
text
requests==2.32.3
pytest==8.3.2
Important Point
Pinning every dependency can improve reproducibility, but teams may use modern dependency-management tools instead.
Question 7: What is import?
Ans
import loads a module and binds a module name in the importing namespace. from module import name binds a specific attribute directly.
Example
python
import math
print(math.sqrt(25))
Important Point
Circular imports can cause partially initialized modules; organize dependencies to avoid unnecessary cycles.
Question 8: What is circular import?
Ans
A circular import occurs when module A imports B while B directly or indirectly imports A. It can produce partially initialized modules and import errors.
Example
text
A -> imports B
B -> imports A
Important Point
Move shared definitions into a third module or redesign dependencies rather than relying on import timing tricks.
Question 9: What is `__all__`?
Ans
__all__ defines names intended for wildcard imports such as from module import *. It also documents a module's public export list for that use case.
Example
python
__all__ = ["add", "multiply"]
Important Point
Explicit imports are usually clearer than wildcard imports.