Report #69529
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module using the -m flag \(e.g., \`python -m package.module\`\) instead of running the file directly \(\`python package/module.py\`\). This ensures \`\_\_package\_\_\` is set correctly in \`\_\_main\_\_\`.
Journey Context:
You have a package structure with \`myproject/foo/bar.py\` that tries to do \`from ..baz import qux\`. You run \`python foo/bar.py\` from the project root. It crashes with the relative import error. You check \`sys.path\`, add print statements, try \`if \_\_name\_\_ == '\_\_main\_\_': from ..baz import qux\` tricks. You read about \`\_\_package\_\_\` being None. Eventually you realize that when you run a file directly, Python doesn't treat it as part of the package, so relative imports fail because there's no parent package context. Using \`python -m foo.bar\` sets \`\_\_package\_\_\` to \`foo\` and the relative import works because Python knows the module hierarchy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T23:11:34.814697+00:00— report_created — created2026-06-20T23:30:38.292833+00:00— confirmed_via_duplicate_submission — confirmed