Courseiva
Modules and PackageseasyMultiple ChoiceObjective-mapped

PCAP Modules and Packages Practice Question

A Python script is written to be used both as a standalone program and as an imported module. Which condition should the script use to execute code only when run directly?

⚠ Common exam trap

Python Institute often tests the exact syntax `if __name__ == '__main__':` and distracts candidates with plausible-sounding but incorrect alternatives like `__import__` or `__module__`, exploiting confusion about Python's special attributes and the difference between module-level and execution-level variables.

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__':

Python sets the global variable `__name__` to `'__main__'` when the script is executed directly (e.g., `python script.py`). When the script is imported as a module, `__name__` is set to the module's name. The condition `if __name__ == '__main__':` is the standard Python idiom to guard code that should only run in the direct execution context.

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 __import__ == '__main__':

    Why it's wrong here

    This line compares the built-in function object __import__ to the string '__main__', which is never true. __import__ is a global function used to programmatically import modules, not a variable that holds the name of the executing script. Even if it were evaluated, the comparison would always be False, so the guarded code would never run. This is a common typo for __name__, but the two are entirely different identifiers.

  • if __name__ == '__main__':

    Why this is correct

    This is the canonical Python idiom used to determine whether the current file is being run as the top-level script. When the interpreter executes a script directly, it sets the global variable __name__ to the string '__main__'; when the file is imported as a module, __name__ is set to the module's import name instead. The if block therefore only runs for standalone execution, which is exactly what the script intends. This guard also supports running with python -m, where __name__ is also '__main__'.

  • if __name__ == '__module__':

    Why it's wrong here

    At module level, there is no variable named __module__ that holds the string '__main__'; that attribute exists only on classes and functions, and even then it stores the module name in which the object was defined, not a special sentinel. In a script's global scope, __name__ is either '__main__' or a module name, but never the string '__module__'. Therefore this condition is always False and provides no meaningful runtime check. This mistake likely arises from confusing __module__ with the module system, but it is not a valid replacement for the __name__ check.

  • if __file__ == 'main':

    Why it's wrong here

    The __file__ variable contains the filesystem path to the script, such as 'script.py' or '/home/user/main.py', and is never exactly the string 'main'. In normal execution, even a file named main.py would have __file__ = 'main.py', not 'main', so the comparison fails. Worse, __file__ is not defined in some contexts, such as the interactive interpreter or certain dynamically created modules, making this check unreliable. The standard way to test for standalone execution is to inspect __name__, not the file path.

About these practice questions

Courseiva writes every PCAP question from scratch — 169 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.