Report #45318
[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, enabling the relative import algorithm to resolve parent packages. Alternatively, refactor to absolute imports if the package structure allows.
Journey Context:
Developer is working on a package with nested modules. They run \`python mypackage/submodule.py\` directly to test a quick change. Immediately hits ImportError on \`from . import sibling\` or \`from ..parent import thing\`. They try adding \`\_\_init\_\_.py\` files \(if on Python 3.2\+ where they're not always needed\), they try modifying PYTHONPATH, they try absolute imports. The error persists because running a file directly makes Python set \`\_\_name\_\_\` to \`\_\_main\_\_\` and \`\_\_package\_\_\` to None, breaking the relative import resolution algorithm defined in PEP 366. The developer realizes they must run the module as part of the package using \`python -m mypackage.submodule\` from the project root, which sets \`\_\_package\_\_\` correctly and allows the relative import to resolve through the package hierarchy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:32:29.922842+00:00— report_created — created