Courseiva
Modules and Packages →hardMultiple Choice

PCAP Modules and Packages Practice Question

A developer runs the following code in a script that is not part of a package:

import sys

sys.path.insert(0, '/custom/path')

import mymodule
print(mymodule.__name__)

What is the output if 'mymodule' is found at '/custom/path/mymodule.py'?

⚠ Common exam trap

Python Institute often tests the distinction between `__name__` and the file path or extension, hoping candidates confuse the module's name with its filename or full path.

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

✓

mymodule

When a module is imported, Python assigns the `__name__` attribute to the module's name as a string (e.g., 'mymodule'), not the filename with extension. Since the script is not part of a package and the module is found at a custom path, `mymodule.__name__` is simply 'mymodule'.

Answer analysis

Option-by-option breakdown

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

  • ✗

    mymodule.py

    Why it's wrong here

    Including the `.py` extension is incorrect because `__name__` is assigned a Python module identifier, not a filesystem filename. When a module is imported, Python strips the `.py` suffix and sets `__name__` to the name used in the import statement. For example, importing `mymodule.py` from disk populates `mymodule.__name__` with the string `'mymodule'`, never `'mymodule.py'`. The extension exists only for the loader to find the file, not for runtime module identification.

  • ✗

    __main__

    Why it's wrong here

    The value `__main__` is reserved strictly for the top-level execution environment—the script, interactive session, or module launched with `python -m`. When another script imports `mymodule`, the interpreter executes the module code but does not promote it to the top-level scope; instead, `__main__` remains associated with the original entry point. Assigning `__main__` to an imported module would incorrectly imply that the module itself is the program being run, which defeats the purpose of the `if __name__ == '__main__'` guard.

  • ✓

    mymodule

    Why this is correct

    This is the correct answer because, for an imported module, `__name__` is set to the module's dotted import name—exactly the identifier used in the `import` statement, with no trailing `.py`. For a file `mymodule.py` imported as `import mymodule`, Python sets `mymodule.__name__` to `'mymodule'`, allowing the module to know its own name within the system. If the module is part of a package, the value would include the package path, such as `'package.mymodule'`, but the base case is simply the module name.

  • ✗

    /custom/path/mymodule.py

    Why it's wrong here

    The absolute filesystem path `/custom/path/mymodule.py` corresponds to the module's `__file__` attribute, not to `__name__`. `__file__` records the location from which the module was loaded, which is useful for resource resolution or reloading, but it is irrelevant to the module's logical identity. Python deliberately separates these two values: `__name__` is the namespace key used for imports and conditional guards, while `__file__` is an implementation detail of the import system. Expecting `__name__` to equal the full path conflates the module object's identity with its physical storage location.

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 →

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.