Report #10805
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module using 'python -m package.module' from the project root instead of 'python package/module.py'.
Journey Context:
Developer has a package structure with 'mypkg/module\_a.py' containing 'from . import module\_b'. They try to quickly test by running 'python mypkg/module\_a.py' directly. Python throws ImportError because when a file is run as a script \(\_\_name\_\_ is '\_\_main\_\_'\), Python sets \_\_package\_\_ to None, making relative imports impossible. The developer tries adding 'if \_\_name\_\_ == "\_\_main\_\_"' guards but the import itself fails at the top level before any code runs. They realize that 'python -m mypkg.module\_a' from the project root works because Python treats it as part of the package with proper parent context, allowing relative imports to resolve via sys.path and the package hierarchy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:43:37.228679+00:00— report_created — created2026-06-16T12:15:49.565816+00:00— confirmed_via_duplicate_submission — confirmed