Report #78559
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the script as a module using \`python -m package.module\` instead of \`python package/module.py\`. This ensures \`\_\_name\_\_\` is set to the fully qualified module name and \`\_\_package\_\_\` is correctly populated, allowing the relative import machinery to resolve parent packages.
Journey Context:
A developer is refactoring a monolithic script into a package structure. They create \`myapp/utils/helpers.py\` containing \`from ..core import config\`. They attempt to run a quick test with \`python myapp/utils/helpers.py\` and are immediately hit with the relative import error. They check \`sys.path\`, add \`\_\_init\_\_.py\` files, and even try manipulating \`sys.modules\` manually. After extensive searching, they realize that when a file is run as a script \(\`\_\_name\_\_ == '\_\_main\_\_'\`\), Python does not treat it as part of a package, so relative imports have no parent context. Switching to \`python -m myapp.utils.helpers\` from the project root makes Python treat the target as a module within the \`myapp\` package, resolving the parent package chain correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T14:27:29.875855+00:00— report_created — created