Report #94273
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module using \`python -m package.module\` from the project root instead of \`python package/module.py\`. This sets \`\_\_package\_\_\` correctly, allowing Python to resolve relative imports by establishing the module's package context.
Journey Context:
Developer has a project structure with \`myapp/utils/helpers.py\` containing \`from . import config\`. They execute \`python myapp/utils/helpers.py\` directly. Python sets \`\_\_name\_\_\` to \`\_\_main\_\_\` and \`\_\_package\_\_\` to \`None\`, breaking relative imports. Developer adds \`sys.path\` hacks or moves files fruitlessly. They eventually debug \`print\(\_\_package\_\_\)\` and find it's \`None\`, realizing the import system cannot determine the parent package when run as a script. The 'aha' moment comes when they try \`python -m myapp.utils.helpers\` and it works, as \`-m\` correctly initializes the package hierarchy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T16:49:20.206456+00:00— report_created — created