Report #102858
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the script as a module with \`python -m mypackage.submodule\` instead of \`python submodule.py\`, or convert relative imports to absolute imports using the full package path.
Journey Context:
A developer organized a project with \`utils/helpers.py\` and \`main.py\`. In \`helpers.py\` they wrote \`from ..config import settings\` to import from a parent package. Running \`python utils/helpers.py\` gave the ImportError. They thought relative imports would always work. After reading PEP 328, they learned that relative imports only work when the script is part of a package and executed via \`-m\` or imported from another module. They refactored to use absolute imports \(\`from myproject.config import settings\`\). Alternatively, they added \`\_\_init\_\_.py\` files and ran \`python -m myproject.utils.helpers\`. The -m approach worked because Python correctly set up the package hierarchy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T15:47:08.896732+00:00— report_created — created