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'?
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.
Why this answer
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'.
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.
How to eliminate wrong answers
Option A is wrong because `__name__` does not include the '.py' extension; it is the module's bare name. Option B is wrong because `__name__` is only set to `__main__` when the module itself is executed as the top-level script, not when it is imported. Option D is wrong because `__name__` is not the full file path; the path is used for locating the module but is not stored in `__name__`.