Courseiva
Modules and PackagesmediumMultiple ChoiceObjective-mapped

PCAP Modules and Packages Practice Question

A developer has a module 'config.py' with the following content:

# config.py
import os

DATABASE_URL = os.getenv('DATABASE_URL', 'localhost')

Another module 'app.py' imports config and uses DATABASE_URL. During testing, the environment variable is set correctly, but the import still uses the default value 'localhost'. What is the most likely reason?

⚠ Common exam trap

Python Institute often tests the misconception that `from module import name` creates an independent copy, when in fact it only binds a reference to the same object, and the real issue is Python's module caching and the timing of environment variable reads.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Python caches modules; config.py was imported earlier without the environment variable, and the cached version is reused.

Python caches imported modules in `sys.modules`. If `config.py` was imported earlier in the test session (e.g., during test discovery or another import) before the environment variable `DATABASE_URL` was set, the cached module would retain the default value `'localhost'`. Subsequent imports, even after setting the environment variable, reuse the cached module, so `os.getenv('DATABASE_URL', 'localhost')` is not re-evaluated.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The import statement in app.py is placed inside a function, so it is not executed.

    Why it's wrong here

    An import inside a function body is completely legal and runs every time that function is invoked, not at module definition time. Saying it is 'not executed' is therefore false: it is only deferred until the function call. In this scenario the real issue is not that the import fails to run, but that when it does run, Python finds config.py already cached in sys.modules and reuses it without executing the module-level code again.

  • The module was imported using 'from config import DATABASE_URL' which creates a separate copy.

    Why it's wrong here

    The 'from config import DATABASE_URL' form does not create a separate copy of the module or a duplicate value. It merely binds the local name DATABASE_URL to the same object currently stored as an attribute of the cached config module. Because the module itself is executed only once and then cached in sys.modules, the from-import simply retrieves whatever value was set at that first execution—so if the environment variable was missing then, you receive the old, stale value.

  • The environment variable is only read when the function is called, not at import time.

    Why it's wrong here

    os.getenv() is executed at import time when it appears at the top level of config.py, not when the function in app.py is subsequently called. If the environment variable was not set before the first import, the module-level assignment bakes in the value None permanently. Later function calls cannot change that cached value, because the module code is never re-run; the mistake here is confusing module-level execution with deferred execution inside a function.

  • Python caches modules; config.py was imported earlier without the environment variable, and the cached version is reused.

    Why this is correct

    Python records every imported module in sys.modules, and a later import of the same module simply fetches that cached object instead of re-executing the file. If config.py was imported earlier in the same interpreter session before the environment variable was set, its module-level code—including the os.getenv call—has already run and stored a stale default. All subsequent imports, whether 'import config' or 'from config import DATABASE_URL', see that cached module and its fixed value, so the code is not re-executed.

About these practice questions

One of 169 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.