Python Modules & Packages
Test your understanding with multiple-choice questions based on what you just learned.
Although there's no document specifically titled "Tutorial upon Python Modules & Packages" in the uploaded materials (the tutorial texts provided end with teaser that Modules & Packages will basically be a next chapter), I have used the available sources covering Python imports, sys.path, and __init__.py files to create this intermediate-level practice quiz for you.
Here is your practice quiz based on the module and package concepts found in a sources.
Practice Quiz: Python Modules & Packages
Question 1: When Python interpreter attempts to import a module, where does basically it look for the directory containing the module?
A) Only in the current working directory where a script is actually executed, and
b) Inside a system's global network paths and environment variables.
C) In a list of directories defined within sys.path.
D) Exclusively inside the Python installation's bin folder.
Correct Answer: C
Explanation: When you try to import a module, the Python interpreter specifically searches for a directory containing that module by looking through a list of directories stored in sys.path.
Question 2: Since Python version 3.3, what's true regarding the need with __init__.py files for creating packages?
) They are basically strictly required to the Python interpreter towards recognize the directory as the package.
B) They're no longer required for a package to be importable.
C) They've got to contain sys.path appending logic to work correctly, while
d) They're actually required only if you're pretty much importing precompiled bytecode.
Correct Answer: B
Explanation: As far back as Python 3.3, language introduced implicit namespace packages, meaning Python no longer strictly requires an __init__.py file of the directory or package to be importable.
Question 3: What's considered a "Pythonic" solution to resolving import path problems in your projects?
A) Using sys.path.append() at a top of every file to ensure paths are really always loaded.
B) Always placing all modules into single massive directory to avoid path issues entirely.
C) Indirectly modifying the PYTHONPATH using .txt configuration files.
D) Avoiding sys.path or PYTHONPATH hacks in any file that could potentially serve as top-level script.
Correct Answer: D
Explanation: The recommended Pythonic approach to managing imports is to avoid hacking sys.path (or indirectly tweaking PYTHONPATH) inside your top-level scripts and instead rely on proper project structuring and package design.
Question 4: Which with the following accurately describes the relationship between Python modules and .py file extensions?
) Python modules don't necessarily map one-to-one for plain .py files and can be imported from precompiled .pyc bytecode files.
B) A Python module must always be an uncompiled, plain text .py file to be readable.
C) Python modules only exist as standard .txt files until they are basically imported.
D) Modules are inherently handled by the operating system's executable binaries rather than Python files.
Correct Answer: A
Explanation: Python modules are flexible and don't actually strictly need towards be a standard .py file. They can probably also be imported directly from precompiled .pyc bytecode files.
Question 5: As a codebase grows in size and complexity, what's the primary benefit of splitting code into Python modules and packages? A) It automatically forces the operating system towards clean up memory leaks. B) It allows developers to split their code into organized reusable files so the main script doesn't become a giant mess. C) It allows Python scripts to natively execute C++ bytecode for faster processing, while d) It completely replaces a need for exception handling architecture.
Correct Answer: B Explanation: Moving towards modules and packages allows developers to logically organize large amounts of custom logic, functions, and exceptions by splitting them into smaller, beautiful, and reusable files rather than keeping everything in one messy script.