Modules and Packages Flashcards
7 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Modules and Packages flashcards as text
What does a relative import like `from . import utils` do?
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.
Where does `pip install` place packages by default?
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.
What is the role of `setup.py` in a Python package?
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.
Which module provides the `find_spec()` function for inspecting how a module would be imported?
Answer: importlib.util
`importlib.util.find_spec('module_name')` returns a `ModuleSpec` describing how the module would be located and loaded.
What does `from ..sibling import helper` mean in a package?
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.
Which tool is the modern standard for declaring package dependencies and metadata, replacing `setup.py`?
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`.
What happens when you run `python -m mypackage`?
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`.