Courseiva
Modules and PackagesmediumMultiple ChoiceObjective-mapped

Why Relative Imports Fail in Directly Executed Scripts

A Python script placed in /opt/myapp/script.py fails with ImportError when run from a cron job with the command: python /opt/myapp/script.py. The script works when run manually from the /opt/myapp/ directory. The script contains the line: from . import config. The config module is located in /opt/myapp/lib/config.py with an __init__.py in /opt/myapp/lib/. What is the most likely cause of the failure?

Quick Answer

The correct answer is that relative imports fail in directly executed scripts because the script’s `__name__` is set to `'__main__'` instead of a package name. When you run `python /opt/myapp/script.py`, Python treats the file as the top-level entry point, and relative imports like `from . import config` require the importing module to be part of a package with a proper `__name__` reflecting the package hierarchy—without that, the interpreter cannot resolve the dot. This is a classic Certified Associate Python Programmer PCAP exam trap: candidates often assume the script works because it runs from the correct directory, but the real issue is that relative imports are only valid inside a package loaded with `-m` or imported from another module. The exam tests your understanding of Python’s module system and the `__name__` variable. Memory tip: “Main means no dot—when `__name__` is `'__main__'`, relative imports are in vain.”

⚠ Common exam trap

Python Institute often tests the distinction between running a script directly (`python script.py`) versus running it as a module (`python -m package.script`), and the trap here is that candidates mistakenly blame `sys.path` or `__init__.py` contents instead of recognizing that relative imports are fundamentally incompatible with direct script execution.

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 are not allowed in a script that is executed directly because its __name__ is not set to a package name.

When a Python script is executed directly (e.g., `python /opt/myapp/script.py`), its `__name__` is set to `'__main__'`, not to a package name. Relative imports (like `from . import config`) require the importing module to be part of a package with a proper `__name__` reflecting the package hierarchy. Since the script is run as the top-level entry point, the relative import fails with an `ImportError`. This explains why the script works when run manually from `/opt/myapp/` (if the working directory is set appropriately, but the relative import still fails unless the script is run as a module with `-m`), but fails from cron where the working directory is typically the user's home directory.

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 __init__.py file in the lib directory is empty and should contain imports.

    Why it's wrong here

    An empty __init__.py works to mark a directory as a package.

  • The lib directory is not in sys.path when the script is run from cron.

    Why it's wrong here

    The script's directory is added to sys.path, so lib as a subdirectory is accessible.

  • Relative imports are not allowed in a script that is executed directly because its __name__ is not set to a package name.

    Why this is correct

    When a script is run directly, it is treated as __main__, not as part of a package, so relative imports fail.

  • The cron job uses a different Python interpreter that does not have the required standard library.

    Why it's wrong here

    This would affect standard libraries, not custom modules.

About these practice questions

This PCAP question is part of Courseiva's 169-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

Same concept, more angles

2 more ways this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Refer to the exhibit. Given the project structure, which of the following import statements in main.py would cause an ImportError?

medium
  • A.from utils import strings
  • B.from ..utils import helpers
  • C.from utils import helpers
  • D.from utils.strings import format

Why B: Uses a relative import with '..' which is only valid inside a package (i.e., when the module is loaded as part of a package and has a __package__ attribute set). In a flat project structure where main.py is a top-level script, '..' attempts to go above the top-level package, which is not allowed and raises an ImportError. Python's import system requires that relative imports be used only within a package hierarchy.

Variation 2. Refer to the exhibit. Which import statement in app.py will successfully import and use the greet function from helpers.py?

easy
  • A.import utils.helpers; then greet()
  • B.from utils.helpers import greet
  • C.from .utils.helpers import greet
  • D.from my_package.utils.helpers import greet

Why C: The package structure shown in the exhibit places app.py inside a package with utils as a subpackage. The relative import `from .utils.helpers import greet` correctly resolves `utils` relative to the current package. Option A fails because it does not bind the `greet` name. Option B fails because `utils` is not a top-level package in the exhibit. Option D fails because it uses `my_package` as the top-level package name, which is not the actual package name shown in the exhibit.

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.