Report #75886
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module as part of a package using 'python -m package.submodule' \(run from the parent of the package directory\) instead of 'python package/submodule.py'. Alternatively, refactor to use absolute imports only.
Journey Context:
A developer has a package structure 'mypkg/\_\_init\_\_.py' and 'mypkg/main.py' where main.py contains 'from . import utils'. Attempting to run 'python mypkg/main.py' results in 'ImportError: attempted relative import with no known parent package'. The developer tries adding the parent directory to PYTHONPATH with 'export PYTHONPATH=$\(pwd\):$PYTHONPATH' but the error persists because when running a file directly, Python sets \_\_name\_\_ to '\_\_main\_\_' and \_\_package\_\_ to None, breaking relative import resolution. The developer discovers that running the module as 'python -m mypkg.main' from the parent directory \(or project root with proper PYTHONPATH\) treats the file as part of the package, setting \_\_package\_\_ correctly and allowing relative imports to resolve.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T09:58:09.020437+00:00— report_created — created