Courseiva
Modules and PackageshardMultiple ChoiceObjective-mapped

PCAP Modules and Packages Practice Question

A developer notices that a custom package 'mypackage' is not being found when importing, even though it is installed in the site-packages directory. The developer suspects a conflict with another package of the same name. Which command should the developer run to diagnose the location from which Python is importing the package?

⚠ Common exam trap

Test-takers frequently confuse `__file__` (which gives the current script's path) with `module.__file__` (which gives the imported module's path), or they assume `print(mypackage)` will show the path directly, when in fact it may only show a module representation without the full path in all contexts.

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

print(mypackage.__file__)

`mypackage.__file__` returns the filesystem path from which the module was loaded, allowing the developer to see exactly which `mypackage` Python is using. This directly reveals if the wrong package (e.g., from a different location or a conflicting installation) is being imported instead of the intended one.

Answer analysis

Option-by-option breakdown

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

  • print(mypackage)

    Why it's wrong here

    Calling print(mypackage) outputs the module's repr, typically `<module 'mypackage' from '/abs/path/to/mypackage/__init__.py'>`. While this string does happen to display the package path, it is a human-readable representation, not a plain path string that can be used by code. The question asks for the module's location as a usable value, and this option merely prints an object description, so it does not solve the requirement.

  • print(__file__)

    Why it's wrong here

    In a regular script, `__file__` is automatically defined as the path of the script file currently being executed, not the imported package. If the code is run in an interactive prompt, `__file__` may be undefined and raise a NameError. Even when defined, it refers to the main module's file, so it cannot reveal where `mypackage` lives; it is completely disconnected from the package's location.

  • print(mypackage.__file__)

    Why this is correct

    For an imported module, the `__file__` attribute stores the filesystem path of the source file from which the module was loaded. When `mypackage` is a package, its `__file__` points to the package's `__init__.py` file, which is exactly the location of the package on disk in most ordinary cases. This is the standard, programmatic way to determine where a package or module resides, making this option correct.

  • import os; print(os.getcwd())

    Why it's wrong here

    `os.getcwd()` returns the current working directory of the Python process, which is the directory from which the interpreter was launched or to which it has been changed. The location of an imported module has no inherent connection to the CWD; packages can be imported from any directory in `sys.path`, environment-specific paths, or even zipped archives. Therefore, printing `getcwd()` will almost always give a path unrelated to `mypackage`.

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.