โ† All Python Flashcard Decks

Modules and Packages Flashcards

6 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 6 Modules and Packages flashcards as text
  1. A developer is creating a large application with a directory structure for utilities like `my_app/utils/string_helpers.py`. To make `utils` a package and allow `string_helpers` to be imported from `my_app/main.py` using `from utils import string_helpers`, what crucial, often empty, file must exist inside the `my_app/utils/` directory?

    Answer: __init__.py

    For the Python interpreter to treat a directory as a package, it must contain a file named `__init__.py`. This file can be empty, or it can contain initialization code for the package. Without it, the directory is just a directory, and you cannot import modules from it in this manner.

  2. What is the primary purpose of the `if __name__ == '__main__':` construct in a Python script?

    Answer: To allow code to run only when the script is executed directly, but not when it is imported as a module.

    The Python interpreter sets the special variable `__name__` to `'__main__'` when a file is run directly. If the file is imported into another module, `__name__` is set to the module's name. Therefore, the `if __name__ == '__main__':` block is used to contain code that should only execute when the file is the main program, preventing it from running on a simple import.

  3. A data scientist is working on a script and needs to use both the `numpy` and `pandas` libraries, which are conventionally imported with short aliases. Which of the following code snippets correctly imports these modules using their standard aliases?

    Answer: import numpy as np import pandas as pd

    The correct syntax for importing a module and assigning it an alias is `import module_name as alias_name`. The standard, community-accepted aliases for numpy and pandas are 'np' and 'pd' respectively. This practice improves code readability while keeping the module's namespace separate.

  4. When you execute `from math import sqrt`, what exactly happens in the current script's namespace?

    Answer: The entire `math` module is loaded into memory, and a reference to the `sqrt` function is added directly to the current namespace.

    Even when using `from...import`, Python loads the entire module (`math` in this case) into memory (specifically, into `sys.modules`). However, the `from...import` statement only places a reference to the specified name (`sqrt`) directly into the current script's namespace, not the module name itself.

  5. Which of the following is the LAST place the Python interpreter will look when trying to resolve an `import my_module` statement?

    Answer: Installation-dependent default directories, such as `site-packages`.

    Python's module search path, accessible via `sys.path`, has a defined order. It first checks the directory of the currently running script, then the directories in the `PYTHONPATH` environment variable, and finally, it checks the standard library and installation-dependent default locations like `site-packages`. A random directory is never checked.

  6. A developer is building a package and wants to control which functions are imported when a user performs a wildcard import (`from my_package import *`). Which special variable should be defined within the package's `__init__.py` file to achieve this?

    Answer: __all__

    The `__all__` variable is a list of strings that defines the public API of a module. When `__all__` is defined in a package's `__init__.py`, a user who runs `from my_package import *` will only import the names listed in `__all__`, giving the package author explicit control over the namespace.