Report #50758
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module as a package using \`python -m package.module\` from the project root, instead of \`python package/module.py\`. Root cause: When executing a file directly, Python sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\` and \`\_\_package\_\_\` to \`None\`, breaking relative imports which require the module to be part of a package hierarchy.
Journey Context:
Developer has a project \`foo/bar.py\` containing \`from . import baz\`. They run \`python foo/bar.py\` from the project root. The script crashes with \`ImportError: attempted relative import with no known parent package\`. Developer tries setting \`PYTHONPATH=$PWD\`, same error. Tries converting to absolute imports, which works but breaks the package structure. Eventually searches StackOverflow, learns about \`python -m foo.bar\`. Tries it from the project root, and it works. Realizes the fundamental difference between running a script and importing a module.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T15:40:47.256182+00:00— report_created — created