Report #70059
[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 \(ensuring \`\_\_package\_\_\` is set correctly\), rather than running the file directly with \`python package/module.py\`. Alternatively, restructure to absolute imports.
Journey Context:
Developer clones a repo with a \`src/mypackage/\` structure. They cd into \`src/mypackage\` and run \`python submodule.py\` which contains \`from . import sibling\`. Python sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\` and \`\_\_package\_\_\` to \`None\`, so relative imports fail because Python cannot determine the parent package. Developer tries adding \`if \_\_name\_\_ == "\_\_main\_\_"\` guards, tries manipulating \`sys.path\` to include parent directories, and tries moving files. Eventually, they realize the script must be invoked as part of the package namespace via \`python -m mypackage.submodule\` from the project root, which sets \`\_\_package\_\_\` correctly and allows the relative import to resolve via the import system's parent package tracking.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T00:11:00.088675+00:00— report_created — created