Courseiva
Modules and PackageseasyMultiple ChoiceObjective-mapped

PCAP Modules and Packages Practice Question

A Python script imports the module 'my_module'. The developer wants to ensure that when the script is run directly, it executes a specific function, but when imported as a module, that function is not executed. Which code snippet achieves this?

⚠ Common exam trap

Python Institute often tests the distinction between `__name__` and `sys.argv` or environment variables, trapping candidates who confuse the script's filename with the module's name or who think an external flag is needed to control execution.

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

if __name__ == '__main__': run()

Both options A and B are correct because they are identical and represent the standard Python idiom `if __name__ == '__main__': run()`. When the script is run directly, Python sets `__name__` to `'__main__'`, triggering the function. When imported, `__name__` is the module name, so the function is not executed. Options C and D are incorrect: C relies on an environment variable that is not standard, and D checks `sys.argv[0]` which is the script path, not the module name.

Answer analysis

Option-by-option breakdown

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

  • if __name__ == '__main__': run()

    Why this is correct

    This is the canonical Python idiom for conditional execution. The interpreter assigns the special variable __name__ the value '__main__' only when the source file is run directly as the main program (e.g., `python my_module.py`). When the file is imported as a module, __name__ becomes the module's fully qualified name, so the equality check fails and run() is not invoked, allowing safe import without side effects.

  • if __name__ == '__main__': run()

    Why this is correct

    This option is exactly equivalent to the standard guard in option A, and it is just as correct. Comparing `__name__` to the string literal '__main__' is the recognized, PEP 8-compliant way to determine whether a file has been executed directly rather than imported. The identical code appearing twice in the options does not change its validity; both instances pass because the interpreter sets __name__ consistently regardless of how many times the idiom appears.

  • if os.environ.get('RUN_MAIN'): run()

    Why it's wrong here

    This is incorrect because it relies on an environment variable that Python does not set or guarantee. The os.environ.get('RUN_MAIN') call only returns a value if the user or some external tool has manually defined RUN_MAIN in the environment; it has no built-in connection to Python's execution model. For example, Django's development server sets an internal RUN_MAIN variable for its autoreloader, but that is framework-specific and not a general mechanism for detecting direct execution in a plain Python script.

  • if sys.argv[0] == 'my_module': run()

    Why it's wrong here

    This fails because sys.argv[0] is the first element of the command-line argument list; it holds the path of the script being executed, not the module name. When you run `python my_module.py`, sys.argv[0] is 'my_module.py' (or an absolute/relative path), and when the module is imported, sys.argv[0] refers to the importing program's script, not 'my_module'. Thus this string comparison is unreliable and will not detect direct execution in general, especially with `python -m my_module`.

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.