Report #104430
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the script as a module using \`python -m mypackage.mymodule\` instead of \`python mymodule.py\`, or restructure the package to avoid relative imports in top-level scripts. Alternatively, modify \`sys.path\` \(not recommended\).
Journey Context:
Developer created a package with submodules and used relative imports \(e.g., \`from . import sibling\`\). When running one of the files directly with \`python submodule.py\`, they got the error. They tried adding \`\_\_init\_\_.py\` files and checking \`sys.path\`, but the error persisted. After reading Python's import system documentation, they understood that relative imports require the module to be part of a package, which is only true when the module is loaded via \`-m\` or imported from another module. The fix: they changed their workflow to run the script as \`python -m mypackage.submodule\` from the parent directory, which correctly sets the \`\_\_package\_\_\` attribute.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-16T20:06:55.180208+00:00— report_created — created