Python Modules and Packages 3 — Questions and Answers
Question 1: What does a relative import like `from . import utils` do?
- Imports `utils` from the same package as the current module (Correct answer)
- Imports `utils` from the Python standard library
- Imports `utils` from the parent directory of the project
- Imports `utils` relative to `sys.path[0]`
Correct answer: Imports `utils` from the same package as the current module
A single leading dot (`.`) means the import is relative to the current package, so `from . import utils` loads a sibling module.
Question 2: Where does `pip install` place packages by default?
- In the `site-packages` directory of the active Python environment (Correct answer)
- In the current working directory
- In `/usr/local/lib/`
- In a `packages/` folder next to the script
Correct answer: In the `site-packages` directory of the active Python environment
`pip install` places packages in the `site-packages` directory of the currently active Python interpreter or virtual environment.
Question 3: What is the role of `setup.py` in a Python package?
- It defines package metadata and build instructions for distribution (Correct answer)
- It initializes the package namespace on import
- It lists runtime dependencies in a machine-readable format
- It configures the Python interpreter settings
Correct answer: It defines package metadata and build instructions for distribution
`setup.py` uses `setuptools.setup()` to declare package metadata, dependencies, and entry points for building and distributing a package.
Question 4: Which module provides the `find_spec()` function for inspecting how a module would be imported?
- importlib.util (Correct answer)
- sys
- pkgutil
- inspect
Correct answer: importlib.util
`importlib.util.find_spec('module_name')` returns a `ModuleSpec` describing how the module would be located and loaded.
Question 5: What does `from ..sibling import helper` mean in a package?
- Import `helper` from a module in the parent package's `sibling` subpackage (Correct answer)
- Import `helper` from two levels above the project root
- Import `helper` from the current package's `sibling` directory
- Import `helper` using an absolute path two steps up
Correct answer: Import `helper` from a module in the parent package's `sibling` subpackage
Two dots (`..`) navigate to the parent package, so `from ..sibling import helper` imports from a subpackage that is a sibling of the current package.
Question 6: Which tool is the modern standard for declaring package dependencies and metadata, replacing `setup.py`?
- pyproject.toml (Correct answer)
- requirements.txt
- Pipfile
- MANIFEST.in
Correct answer: pyproject.toml
PEP 517/518 standardized `pyproject.toml` as the build-system configuration file, and tools like `setuptools`, `flit`, and `poetry` use it instead of `setup.py`.
Question 7: What happens when you run `python -m mypackage`?
- Python executes `mypackage/__main__.py` if it exists (Correct answer)
- Python prints the package's `__init__.py` source
- Python lists all modules inside `mypackage`
- Python installs `mypackage` into `site-packages`
Correct answer: Python executes `mypackage/__main__.py` if it exists
The `-m` flag runs a module or package as a script; for a package it looks for and executes `__main__.py`.
What does a relative import like `from . import utils` do?