Report #97634
[bug\_fix] ImportError: attempted relative import with no known parent package
Convert relative imports to absolute imports \(e.g., from mypackage import module\) or run the script as a module using \`python -m mypackage.script\` instead of \`python script.py\`. Ensure the package directory contains an \`\_\_init\_\_.py\`.
Journey Context:
A developer was building a small CLI tool with a package structure. They had a file \`main.py\` that tried \`from ..utils import helper\`. Running \`python main.py\` gave the ImportError. They tried adding \`\_\_init\_\_.py\` to parent directories but the error persisted. Reading Python docs, they understood that relative imports rely on the module's \`\_\_package\_\_\` attribute, which is empty when a script is run directly. The fix was to run the script as a module: \`python -m mypackage.main\`. They refactored the code to use absolute imports as a more robust solution.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T15:46:21.226028+00:00— report_created — created