Python Modules and Packages 4 — Questions and Answers
Question 1: What is `sys.modules`?
- A dictionary caching all currently imported modules (Correct answer)
- A list of module search paths
- A set of built-in module names
- A file listing installed packages
Correct answer: A dictionary caching all currently imported modules
`sys.modules` is a dict mapping module names to module objects; Python checks it first on every import to avoid re-executing module code.
Question 2: What does `pkgutil.iter_modules()` return?
- An iterator of `(finder, name, ispkg)` tuples for modules in given paths (Correct answer)
- A list of all installed package names
- A generator of module source code strings
- A dict of module names to file paths
Correct answer: An iterator of `(finder, name, ispkg)` tuples for modules in given paths
`pkgutil.iter_modules(path)` yields named tuples describing each module or subpackage found in the given list of directories.
Question 3: Which attribute of a module object stores the file path where the module was loaded from?
- __file__ (Correct answer)
- __path__
- __spec__
- __loader__
Correct answer: __file__
`module.__file__` is the absolute path to the `.py` (or `.pyc`) file that was executed to create the module.
Question 4: When is `__init__.py` code executed?
- The first time the package or any of its submodules is imported (Correct answer)
- Every time a submodule of the package is imported
- Only when the package directory is listed in sys.path
- Only when the package is run with `python -m`
Correct answer: The first time the package or any of its submodules is imported
`__init__.py` runs once on the first import of the package; subsequent imports use the cached module from `sys.modules`.
Question 5: What is the difference between a regular package and a namespace package?
- A regular package has `__init__.py`; a namespace package does not and can span multiple directories (Correct answer)
- A regular package can only be imported once; a namespace package can be reloaded
- A namespace package is installed via pip; a regular package is local only
- A regular package is a `.zip` archive; a namespace package is a directory
Correct answer: A regular package has `__init__.py`; a namespace package does not and can span multiple directories
Regular packages require `__init__.py`; namespace packages (PEP 420) lack it and allow merging directories across multiple locations into one logical package.
Question 6: What does setting `sys.path.insert(0, '/my/path')` accomplish?
- Makes Python search `/my/path` before all other directories when importing (Correct answer)
- Appends `/my/path` to the end of the module search path
- Permanently modifies the PYTHONPATH environment variable
- Removes the default site-packages from the search path
Correct answer: Makes Python search `/my/path` before all other directories when importing
Inserting at index 0 places the path at the front of `sys.path`, so Python finds modules there before any other location.
Question 7: Which built-in function returns a list of names defined in a module?
- dir() (Correct answer)
- vars()
- globals()
- help()
Correct answer: dir()
`dir(module)` returns a sorted list of names in the module's namespace, including functions, classes, and variables.