Report #38646
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module using the \`-m\` flag \(e.g., \`python -m package.module\`\) instead of running the file directly as a script, or refactor relative imports to absolute imports.
Journey Context:
A developer has a package structure \`myproject/utils/helpers.py\` containing \`from ..core import config\`. They attempt to run the helper directly via \`python myproject/utils/helpers.py\`. Python executes the file with \`\_\_name\_\_\` set to \`"\_\_main\_\_"\` and \`\_\_package\_\_\` set to \`None\`, so the relative import \`..core\` has no parent package to resolve against. The developer checks \`sys.path\` and sees the project root, but the import still fails. After debugging with \`print\(\_\_package\_\_\)\`, they realize the execution context is the issue. Using \`python -m myproject.utils.helpers\` preserves the package hierarchy, setting \`\_\_package\_\_\` to \`"myproject.utils"\`, allowing the relative import to resolve correctly against the parent package \`myproject\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:20:23.158874+00:00— report_created — created