Report #94036
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module using the \`-m\` flag as a package: \`python -m mypackage.mymodule\` from the project root, ensuring Python sets \`\_\_package\_\_\` correctly.
Journey Context:
A developer structures a project with \`mypkg/utils.py\` and \`mypkg/main.py\`, where \`main.py\` uses \`from . import utils\`. Attempting to execute \`python mypkg/main.py\` triggers the error. The developer debugs by printing \`\_\_name\_\_\` and \`\_\_package\_\_\`, discovering that running a file directly sets \`\_\_package\_\_\` to \`None\`, breaking relative imports. Research reveals that Python treats the file as a standalone script, not a package member. By switching to \`python -m mypkg.main\` from the parent directory, Python imports the module within the package context, populating \`\_\_package\_\_\` and allowing the relative import to resolve correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T16:25:39.884427+00:00— report_created — created