Bird
Raised Fist0

Given this package structure:

medium📝 Debug Q7 of Q15
Python - Modules and Code Organization
Given this package structure:
service/
  __init__.py
  api.py
  helpers.py
In api.py, the code is:
from helpers import process
process()

Running api.py directly causes ModuleNotFoundError. How to fix?
AAdd helpers.py to __init__.py
BRename helpers.py to helpers.pyc
CChange import to <code>from .helpers import process</code>
DRun api.py from outside the service folder
Step-by-Step Solution
Solution:
  1. Step 1: Understand relative imports in packages

    Inside a package, importing sibling modules requires relative import syntax with dot.
  2. Step 2: Fix import statement

    Changing to from .helpers import process tells Python to import from the same package folder.
  3. Final Answer:

    Change import to from .helpers import process -> Option C
  4. Quick Check:

    Use relative imports inside packages [OK]
Quick Trick: Use dot prefix for relative imports inside packages [OK]
Common Mistakes:
MISTAKES
  • Using absolute import without package prefix
  • Modifying __init__.py unnecessarily
  • Renaming files incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes