Report #52322
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module as part of the package using \`python -m package.module\` from the project root \(the directory containing the package folder\), rather than running \`python package/module.py\` directly.
Journey Context:
Developer has a project structure with \`myapp/utils.py\` and \`myapp/\_\_init\_\_.py\`. Inside \`utils.py\`, they use \`from . import config\` \(a relative import\). When they run \`python myapp/utils.py\` from the project root, they get \`ImportError: attempted relative import with no known parent package\`. Developer searches and learns that running a file directly sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\` and \`\_\_package\_\_\` to \`None\`. They try adding \`if \_\_name\_\_ == "\_\_main\_\_": ...\` boilerplate but the import still fails at the top level. Finally, they run \`python -m myapp.utils\` from the project root. This sets \`\_\_package\_\_\` to \`"myapp"\`, making the relative import resolve correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T18:19:04.660222+00:00— report_created — created