Report #11844
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module using \`python -m package.module\` from the project root instead of \`python package/module.py\`. When running a file directly, Python sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\` and \`\_\_package\_\_\` to \`None\`, breaking relative imports. Using \`-m\` executes it as part of the package namespace, preserving \`\_\_package\_\_\` context and allowing relative imports to resolve against the package hierarchy.
Journey Context:
You clone a repository with a nested package structure like \`src/myapp/utils/helper.py\` containing \`from ..config import settings\`. You \`cd src/myapp/utils\` and run \`python helper.py\` to test a quick change. Immediately you hit \`ImportError: attempted relative import with no known parent package\`. You try adding \`sys.path.insert\(0, os.path.abspath\('..'\)\)\` which feels hacky and still fails with the same error. You check Stack Overflow and realize that relative imports rely on \`\_\_package\_\_\` being set, which only happens when Python loads the module as part of a package, not when run as a script. You navigate to the repo root and run \`python -m myapp.utils.helper\`. The import resolves because Python now treats \`myapp\` as the top-level package, allowing \`..\` to resolve to \`myapp.config\` correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:23:19.680477+00:00— report_created — created2026-06-16T14:50:17.056680+00:00— confirmed_via_duplicate_submission — confirmed