Report #79199
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module using the \`-m\` flag so Python treats it as part of the package \(\`python -m package.module\`\) rather than as a standalone script. This sets \`\_\_name\_\_\` to the fully qualified name and \`\_\_package\_\_\` correctly, enabling relative imports by establishing the parent package context.
Journey Context:
You have a project structure with \`myproject/package/submodule.py\` containing \`from . import sibling\`. Running \`python package/submodule.py\` fails immediately with the relative import error. You check \`sys.path\` and see the directory containing the script is added, but Python sees \`\_\_name\_\_\` as \`"\_\_main\_\_"\` and \`\_\_package\_\_\` as \`None\`. You try setting \`\_\_package\_\_\` manually, which works but is brittle. Realizing that running a file as a script breaks the package context, you switch to \`python -m package.submodule\` from the project root. The error disappears because Python now recognizes the parent package and resolves the relative import correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T15:32:06.214888+00:00— report_created — created