Report #87326
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the script as a module using \`python -m package.module\` instead of \`python package/module.py\`, or refactor to absolute imports.
Journey Context:
Developer has a package structure with \`myapp/utils/helper.py\` containing \`from ..config import settings\`. They attempt to test it by running \`python myapp/utils/helper.py\` directly. Python sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\` and crucially \`\_\_package\_\_\` to \`None\`. Relative imports require \`\_\_package\_\_\` to be set to the actual package name to resolve parent directories \(dots\). Because it is \`None\`, Python cannot determine the package context and raises the ImportError. The developer might try adding the parent directory to \`sys.path\`, which allows absolute imports but does not fix the relative import logic. The fix works because running with \`-m\` executes the module within the package context, setting \`\_\_package\_\_\` correctly and allowing the relative import to resolve against the package root.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T05:09:55.369895+00:00— report_created — created