โ† All Python Flashcard Decks

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
  1. What is an 'editable install' (`pip install -e .`)?

    Answer: An install where the package source directory is linked so code changes take effect immediately without reinstalling

    Editable installs (development mode) add the source directory to `sys.path` via a `.pth` file so changes to the source are reflected instantly.

  2. What is the purpose of `PYTHONPATH` environment variable?

    Answer: It adds directories to `sys.path` at interpreter startup

    Directories listed in `PYTHONPATH` (colon-separated on Unix) are prepended to `sys.path` when the interpreter starts.

  3. What does `importlib.import_module('os.path')` return?

    Answer: The `os.path` module object

    `importlib.import_module()` performs the import and returns the module object, equivalent to `import os.path; return os.path`.

  4. What is the effect of deleting a module from `sys.modules` and then reimporting it?

    Answer: The module's source file is re-executed and a fresh module object is created

    Removing a module from `sys.modules` causes the next import to treat it as never loaded, re-executing its code and producing a new module object.

  5. Which attribute on a package module object contains the list of paths used to find its submodules?

    Answer: __path__

    `__path__` is set on package modules to the list of directories searched for submodules; regular (non-package) modules do not have this attribute.

  6. What problem does a circular import cause in Python?

    Answer: One module may see an incomplete version of the other because it is still being initialized

    During a circular import, the partially initialized module is placed in `sys.modules`, so the second module may access attributes that haven't been defined yet.

  7. What is the correct way to make a package expose `MyClass` directly (e.g., `from mypackage import MyClass`) when `MyClass` is defined in `mypackage/core.py`?

    Answer: Add `from .core import MyClass` inside `mypackage/__init__.py`

    Importing `MyClass` in `__init__.py` with `from .core import MyClass` makes it available directly on the package namespace.