Report #42731
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module as a package using 'python -m package.module' instead of 'python package/module.py'. This sets \_\_package\_\_ correctly and allows relative imports to resolve.
Journey Context:
Developer has a package structure with 'project/app/main.py' and 'project/app/utils.py'. main.py contains 'from .utils import helper'. When the developer runs 'python app/main.py', they get the relative import error. They try changing it to 'from utils import helper' which fails with ModuleNotFoundError. They search and learn that running a file directly sets \_\_name\_\_ to '\_\_main\_\_' and \_\_package\_\_ to None. Using 'python -m app.main' from the project root makes Python treat the script as part of the package, enabling relative imports.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T02:11:35.923797+00:00— report_created — created