Report #45635
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module using the package context with \`python -m package.module\` rather than running the file directly with \`python package/module.py\`, ensuring \`\_\_package\_\_\` is set correctly for relative imports to resolve.
Journey Context:
A developer structures a project with \`src/mypackage/module.py\` containing \`from . import sibling\`. They run \`python src/mypackage/module.py\`. Python sets \`\_\_name\_\_\` to \`'\_\_main\_\_'\` and \`\_\_package\_\_\` to \`None\` because the file is executed as a script, not as part of a package. The relative import fails because Python cannot determine the parent package. The developer tries \`sys.path\` hacks and moving files, but the real issue is the execution context. Using \`python -m mypackage.module\` \(after ensuring the parent of \`mypackage\` is in \`PYTHONPATH\`\) executes the code with \`\_\_package\_\_='mypackage'\`, allowing relative imports to resolve correctly via the parent package namespace.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T07:04:29.494150+00:00— report_created — created