Report #91856
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module as a package using \`python -m package.module\` from the project root. This sets \`\_\_package\_\_\` correctly. Alternatively, refactor to use absolute imports. Running \`python module.py\` directly sets \`\_\_name\_\_\` to '\_\_main\_\_' and \`\_\_package\_\_\` to None, breaking relative imports.
Journey Context:
Developer has a package \`myapp/utils/helper.py\` containing \`from . import constants\`. They run \`python myapp/utils/helper.py\` directly to test it. Python adds the script's directory to sys.path, executes it as a standalone script \(\`\_\_name\_\_ == '\_\_main\_\_'\`\), and sets \`\_\_package\_\_\` to None. When the interpreter encounters \`from . import constants\`, it raises ImportError because relative imports require the module to be part of a package hierarchy with a known parent. Developer tries adding empty \`\_\_init\_\_.py\` files \(unnecessary in Python 3.3\+\) or moving the file, but the issue persists until they realize that direct script execution bypasses the package context. Using \`python -m myapp.utils.helper\` from the project root \(where \`myapp\` is importable\) sets \`\_\_package\_\_\` correctly and allows 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-22T12:46:18.429592+00:00— report_created — created