Report #44039
[bug\_fix] ImportError: attempted relative import with no known parent package when running script directly
Execute the script as a module using \`python -m package.module\` from the project root so Python sets \`\_\_package\_\_\` correctly, or refactor to absolute imports. Do not run scripts inside packages via \`python path/to/script.py\`.
Journey Context:
You have a project with \`mypackage/utils/helper.py\` containing \`from . import constants\`. You run \`python mypackage/utils/helper.py\` and get ImportError. You check that \`\_\_init\_\_.py\` files exist. You add prints to see \`\_\_name\_\_\` is \`\_\_main\_\_\` and \`\_\_package\_\_\` is None. You realize that when running a file directly, Python doesn't treat it as part of a package, breaking relative imports. You change your run command to \`python -m mypackage.utils.helper\` from the parent directory of \`mypackage\`, and the relative import succeeds because Python now knows the package context.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T04:23:23.939995+00:00— report_created — created