PCAP Modules and Packages Practice Question
A Python project has the following directory structure:
project/ __init__.py main.py subpackage/ __init__.py module.py
Inside 'module.py', there is a function 'func' that needs to be imported in 'main.py'. The team wants to import 'func' using a relative import from 'main.py'. However, when they run 'python main.py' from the project root, they get an ImportError. What is the most likely reason?
⚠ Common exam trap
Python Institute often tests the distinction between running a script directly (`python main.py`) versus running it as a module (`python -m package.main`), and candidates mistakenly think that adding `__init__.py` files or using absolute imports will fix the relative import error.
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
✓
Relative imports cannot be used in scripts executed directly as __main__.
When a Python script is executed directly (e.g., `python main.py`), its `__name__` is set to `"__main__"`, not the package name. Relative imports (e.g., `from .subpackage.module import func`) rely on the `__package__` attribute being set to the package hierarchy, which only happens when the module is imported as part of a package. Since `main.py` is run as the top-level script, it has no package context, causing an `ImportError`.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The 'project' directory does not contain an __init__.py file.
Why it's wrong here
The directory tree shown for the project includes an `__init__.py` file inside the `project` directory, so this statement directly contradicts the given structure. An `__init__.py` file is what marks a directory as a Python package, and its presence is precisely what would allow `project` to be imported as a package. However, even with `__init__.py` present, relative imports still fail when the file is executed directly as a script, because the package context is lost. Thus, the absence of `__init__.py` is not the cause of the error here.
- ✗
The function 'func' is not defined as a public function.
Why it's wrong here
Python does not enforce true public/private access modifiers for functions; the underscore prefix is merely a naming convention. The function `func` has no leading underscore, so it is conventionally considered public and is fully importable via `from project.module import func` or `import project.module`. If `func` were named `_func`, it would still be importable, only flagged by linters as a private name. Therefore, the failure of the relative import is unrelated to any notion of function visibility.
- ✓
Relative imports cannot be used in scripts executed directly as __main__.
Why this is correct
When a file is run directly as a script, e.g., `python project/module.py`, Python sets `__name__` to `'__main__'` and leaves `__package__` empty or `None`. A relative import such as `from .module import func` relies on `__package__` to determine the package the module belongs to; without that context, Python raises `ImportError: attempted relative import with no known parent package`. This is the exact error described in the question. Relative imports only work when the module is imported as part of a package, for example when the project is run with `python -m project.module` from the parent directory.
- ✗
The module name 'module.py' contains a hyphen.
Why it's wrong here
A hyphen cannot appear in a Python module filename because hyphens are not valid in Python identifiers, and the import system expects valid identifier names for modules. The given directory structure explicitly lists the file as `module.py`, which contains no hyphen. Even if a hyphen were present, the error from a relative import would be a syntax or import error about invalid module names, not the 'no known parent package' error described. Thus, this option misidentifies the cause and is factually incorrect based on the provided structure.
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.