PCAP Modules and Packages Practice Question
Which TWO of the following are valid uses for the '__name__' variable in a Python module?
⚠ Common exam trap
It's easy for candidates to confuse '__name__' with '__file__' (for file paths) or '__all__' (for export control), and may incorrectly think '__name__' can identify the importing module, which Python does not directly support.
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
✓
To check if the module is being run as the main program.
The '__name__' variable is set to the string '__main__' when the module is executed directly as the main program (e.g., via 'python module.py'). This allows a module to include code that runs only when it is the entry point, not when it is imported by another module. The check is typically done with 'if __name__ == "__main__":'.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
To get the file path of the module.
Why it's wrong here
The special attribute __name__ is a string identifier that tells you how the module is referenced, not where its code lives on disk. The filesystem location of a module is exposed via __file__, which contains the absolute or relative path to the source file. Relying on __name__ to find a path would fail because it only holds the module's logical name, and for a script invoked directly it is simply the literal string '__main__'.
- ✗
To control which names are exported when using 'from module import *'.
Why it's wrong here
Star-import behavior is governed by the module-level __all__ attribute, which is an explicit list of names to be exported, not by __name__. If __all__ is absent, 'from module import *' imports all names that do not start with an underscore. __name__ merely records how the module is named in the import system and has no effect on the name-resolution rules that star imports follow.
- ✗
To determine which module imported it.
Why it's wrong here
The __name__ attribute always refers to the module's own identity; it contains either the literal string '__main__' or the module's dotted import path, and it never reveals which other module triggered its import. Determining the importer would require inspecting the call stack or using import hooks, but __name__ offers no such information. The module's name is set by the interpreter at load time, not by the importing code.
- ✓
To check if the module is being run as the main program.
Why this is correct
When a module is executed directly as a script, Python assigns the literal string '__main__' to its __name__ attribute, whereas an imported module receives its normal dotted name. Comparing __name__ to '__main__' thus allows you to detect whether the current file is the entry point of the program. This pattern is the standard way to protect executable code so it runs only when the file is launched directly, not when it is imported by another module.
- ✓
To get the fully qualified name of the module (e.g., 'package.module').
Why this is correct
For an imported module, Python sets __name__ to the fully qualified name that reflects the module's location within the package hierarchy, such as 'package.module' or 'package.subpackage.module'. This dotted path is built from the package name and the module's own name, making it easy to identify the module in logging or introspection. In contrast, a directly executed script always receives '__main__' as its name, even if it is inside a package.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
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.