Report #12525
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module using the \`python -m package.module\` form \(preserving the package context\) instead of \`python package/module.py\`, which sets \`\_\_name\_\_\` to \`\_\_main\_\_\` and \`\_\_package\_\_\` to \`None\`.
Journey Context:
Developer has a project structure \`myapp/utils/helper.py\` containing \`from . import config\`. They navigate to the project root and run \`python myapp/utils/helper.py\` directly. Python adds the script's directory to \`sys.path\`, but because the script is executed as a standalone file, its \`\_\_package\_\_\` is \`None\`. The relative import fails because Python cannot determine the parent package. Developer tries adding \`sys.path\` manipulations or empty \`\_\_init\_\_.py\` files \(which are not the issue\). They eventually realize that running \`python -m myapp.utils.helper\` from the project root \(or package root\) executes the same code but with \`\_\_package\_\_\` correctly set to \`myapp.utils\`, allowing the relative import to resolve.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T16:15:34.657069+00:00— report_created — created