Report #98673
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module as part of its package using \`python -m package.module\` from the project root, not \`python package/module.py\`. If the file must also be runnable as a script, replace relative imports with absolute imports or guard them behind \`if \_\_name\_\_ == "\_\_main\_\_":\` and set \`\_\_package\_\_\` explicitly.
Journey Context:
You have a package \`myapp/\` with \`\_\_init\_\_.py\`, \`cli.py\`, and \`utils.py\`, and \`cli.py\` starts with \`from .utils import helper\`. You run \`python myapp/cli.py\` and Python raises \`ImportError: attempted relative import with no known parent package\`. You try \`cd myapp && python cli.py\` and get the same error. The confusion is that running a file directly makes it \`\_\_main\_\_\` with \`\_\_package\_\_\` set to \`None\`, so relative dots have no parent. Python only knows the parent package when the module is imported through the normal package machinery. Using \`python -m myapp.cli\` from the repository root loads \`myapp/\_\_init\_\_.py\` first, sets \`\_\_package\_\_\` correctly, and the relative import resolves.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-28T04:34:40.124168+00:00— report_created — created