Report #92424
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module using the '-m' flag to execute it as part of the package: 'python -m package.submodule' from the project root \(the parent directory of the package\). This sets \_\_package\_\_ correctly. Do not run 'python package/submodule.py' directly.
Journey Context:
Developer has a package structure: 'myproject/package/submodule.py' containing 'from . import sibling'. They run 'python package/submodule.py' from the project root. Python sets \_\_name\_\_ to '\_\_main\_\_' and \_\_package\_\_ to None, breaking relative imports. Developer tries adding 'sys.path.insert\(0, '..'\)' which causes other issues or still doesn't set the package context. They inspect \_\_file\_\_ and \_\_package\_\_ using print statements, realizing that running as a script breaks the package context. Using 'python -m package.submodule' preserves the package hierarchy, allowing Python to resolve the parent package and execute the relative import correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T13:43:27.844744+00:00— report_created — created