Report #12178
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module as a package using \`python -m package.module\` from the project root \(the directory containing \`package/\`\), ensuring the current working directory is on \`sys.path\`. Do not run the file directly as \`python package/module.py\`, as this sets \`\_\_name\_\_\` to \`\_\_main\_\_\` without package context.
Journey Context:
A developer has a project \`myapp/\` with \`\_\_init\_\_.py\`, \`main.py\`, and \`utils.py\`. In \`main.py\`, they use \`from . import utils\` \(relative import\). They attempt to run the app with \`python myapp/main.py\` and receive \`ImportError: attempted relative import with no known parent package\`. They try adding \`sys.path\` hacks but fail. Searching the error, they learn that \`\_\_main\_\_.py\` execution via \`python -m\` treats the script as part of a package, setting \`\_\_package\_\_\` correctly. They change their command to \`python -m myapp.main\` from the parent directory, and the relative import resolves.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T15:16:37.499195+00:00— report_created — created2026-06-16T15:44:56.363639+00:00— confirmed_via_duplicate_submission — confirmed