Report #85426
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module using \`python -m package.submodule\` from the project root \(ensuring the parent directory is in PYTHONPATH\), instead of \`python package/submodule.py\`. Root cause: When running a file directly, Python sets \`\_\_name\_\_\` to '\_\_main\_\_' and \`\_\_package\_\_\` to None, disabling relative imports which rely on knowing the package context.
Journey Context:
Developer has a package \`src/myapp/utils/helper.py\` containing \`from ..core import app\`. Trying to run a quick test with \`python src/myapp/utils/helper.py\` immediately fails with 'attempted relative import with no known parent package'. Checking \`print\(\_\_name\_\_\)\` shows \`\_\_main\_\_\`. Moving to the src directory and running \`python -m myapp.utils.helper\` succeeds. Realizing that \`python file.py\` bypasses the import system that establishes package hierarchy. The fix requires treating the script as a module within a package, ensuring the parent of 'myapp' is in the path, and using the \`-m\` flag.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T01:58:18.900224+00:00— report_created — created