Report #98191
[bug\_fix] ImportError: attempted relative import with no known parent package
Do not run a file inside a package as a plain script with \`python pkg/submodule.py\`. Instead run it as a module with \`python -m pkg.submodule\` from the project root \(the directory containing \`pkg/\`\), or refactor the relative imports to absolute imports and ensure the project root is on \`sys.path\` \(e.g., install the package in editable mode with \`pip install -e .\`\).
Journey Context:
You have \`pkg/\_\_init\_\_.py\` and \`pkg/module.py\` that does \`from . import something\`. You run \`python pkg/module.py\` and Python raises 'attempted relative import with no known parent package'. You try \`from pkg import module\` and it still fails because the working directory isn't on the path. The rabbit hole: when Python executes a file directly, \`\_\_name\_\_\` is \`'\_\_main\_\_'\` and \`\_\_package\_\_\` is \`None\`, so relative imports have no parent. When you run \`python -m pkg.module\`, \`\_\_package\_\_\` is set correctly and Python resolves the parent package. Editable install is the durable fix because it places the package on \`sys.path\` permanently without \`sys.path\` hacks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-27T04:32:55.369057+00:00— report_created — created