Report #10084
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module using the -m flag \(e.g., \`python -m package.module\`\) instead of \`python package/module.py\`. This ensures \`\_\_package\_\_\` is set correctly for relative imports.
Journey Context:
Developer has a project with \`myapp/models.py\` containing \`from .utils import helper\`. They attempt to test by running \`python myapp/models.py\` directly. This sets \`\_\_name\_\_\` to \`\_\_main\_\_\` and \`\_\_package\_\_\` to \`None\`, breaking the relative import. The developer tries adding \`sys.path\` hacks to no avail. They eventually realize that running scripts inside packages directly bypasses the import system’s package context, and that \`python -m myapp.models\` executes the same code but with the proper package namespace established.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:47:11.901628+00:00— report_created — created