Courseiva
Modules and Packages →hardMultiple Choice

PCAP Modules and Packages Practice Question

An application needs to dynamically load a module whose name is provided at runtime (stored in a variable 'mod_name'). Which function from the importlib module should be used?

⚠ Common exam trap

Python Institute often tests the distinction between `importlib.import_module()` and the non-existent `importlib.import()` or the deprecated `imp.load_module()`, exploiting candidates' tendency to guess based on similar-sounding names rather than precise API knowledge.

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

✓

importlib.import_module(mod_name)

`importlib.import_module(mod_name)` is the standard Python function designed to dynamically import a module given its name as a string at runtime. It returns the module object, allowing the application to load and use modules whose names are not known until execution.

Answer analysis

Option-by-option breakdown

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

  • ✗

    importlib.reload(mod_name)

    Why it's wrong here

    importlib.reload(mod_name) is incorrect because reload() expects an already-imported module object, not a string name, and it re-executes the module's code in place to pick up changes without creating a new module instance. It does not perform a fresh dynamic import by name; in fact the module must already exist in sys.modules, so it cannot load a module that has not yet been imported. For runtime loading by name, you need import_module.

  • ✗

    importlib.load_module(mod_name)

    Why it's wrong here

    importlib.load_module(mod_name) is technically deprecated and was removed in modern Python versions because it bypassed the standard import machinery and directly loaded a module without proper handling of sys.modules, path hooks, and other import system invariants. Historically, it could only load certain kinds of modules and required manual spec construction, making it fragile and unsafe for general dynamic loading. The supported approach is to use importlib.import_module, which delegates to the fully-featured import system.

  • ✗

    importlib.import(mod_name)

    Why it's wrong here

    importlib.import(mod_name) cannot work because importlib has no attribute named import — import is a Python statement keyword, not a function, so this call raises AttributeError. Even if you attempted getattr(importlib, 'import'), the parser would reject the attribute name because 'import' is reserved. The actual function in the importlib module for importing a module by string name is importlib.import_module.

  • ✓

    importlib.import_module(mod_name)

    Why this is correct

    importlib.import_module(mod_name) is the correct way to dynamically load a module when its name is only known at runtime, because it accepts a string and returns the corresponding module object while registering it in sys.modules. It uses the standard import machinery, supports both absolute and relative imports (with a package argument), and is designed exactly for this programmatic use case. This makes it the appropriate replacement for older, deprecated functions like load_module.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.