Report #38868
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module as a package using \`python -m package.module\` from the directory containing the package root, rather than \`python package/module.py\`. This sets \`\_\_package\_\_\` correctly, allowing relative imports to resolve. Restructuring the entry point outside the package and using absolute imports is a robust alternative.
Journey Context:
A developer clones a repo with \`src/mypkg/cli.py\` containing \`from . import utils\`. They navigate to \`src/mypkg\` and run \`python cli.py\` for a quick test. Python sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\` and \`\_\_package\_\_\` to \`None\`. The relative import fails because Python cannot determine the parent package. The developer tries \`sys.path\` hacks which fail because the issue is \`\_\_package\_\_\` context, not path. They realize the module must be imported as part of a package to establish the parent context.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:43:01.699809+00:00— report_created — created