Report #43659
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module using 'python -m package.module' from the project root \(where 'package' is the top-level directory containing \_\_init\_\_.py\) instead of 'python package/module.py', or set PYTHONPATH to include the parent of 'package'.
Journey Context:
Developer has 'project/src/mypkg/\_\_init\_\_.py' and 'project/src/mypkg/cli.py'. In cli.py they write 'from . import utils'. They run 'python src/mypkg/cli.py' and get 'ImportError: attempted relative import with no known parent package'. They try adding 'if \_\_name\_\_ == "\_\_main\_\_":' blocks but it doesn't help. They check sys.path and see '/project/src/mypkg' added, not '/project/src'. They realize Python treats 'cli.py' as \_\_main\_\_, not as part of a package, so relative imports fail because there's no parent package context. By changing command to 'cd src && python -m mypkg.cli', Python sets \_\_name\_\_ to 'mypkg.cli', adds the current directory \(src\) to sys.path, and recognizes 'mypkg' as a package, allowing the relative import to resolve correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T03:45:15.543746+00:00— report_created — created2026-06-19T04:04:54.689026+00:00— confirmed_via_duplicate_submission — confirmed