Report #67612
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module using \`python -m package.module\` from the project root instead of \`python package/module.py\`, or convert relative imports to absolute imports. The root cause is that running a file as a script sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\`, so Python does not treat it as part of a package hierarchy, making relative imports impossible because no parent package context exists.
Journey Context:
You have a project with \`myapp/utils/helpers.py\` that contains \`from ..config import settings\`. You try to run it directly with \`python myapp/utils/helpers.py\` to test a function. It crashes immediately with \`ImportError: attempted relative import with no known parent package\`. You add \`\_\_init\_\_.py\` files to every directory, but the error persists. You add prints to see \`\_\_name\_\_\` and realize it is \`"\_\_main\_\_"\`, not \`"myapp.utils.helpers"\`. Searching reveals that Python's import system treats scripts run directly as top-level modules, breaking relative imports. You change your workflow to run \`python -m myapp.utils.helpers\` from the repository root \(ensuring the root is in \`PYTHONPATH\` or using an editable install\), and the import succeeds because Python now recognizes the package structure.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T19:58:15.226289+00:00— report_created — created