Report #29693
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module as part of the package using the \`-m\` flag: \`python -m mypkg.cli\` \(run from the project root where \`mypkg\` is importable\). Do not run the file directly with \`python mypkg/cli.py\`.
Journey Context:
Developer has a package structure with \`mypkg/utils.py\` and \`mypkg/cli.py\`. Inside \`cli.py\`, they use \`from .utils import helper\` to perform a relative import. They navigate to the project root in their terminal and run \`python mypkg/cli.py\`. Immediately, they get \`ImportError: attempted relative import with no known parent package\`. They search online and find suggestions to add \`sys.path.append\('.'\)\`, which they try unsuccessfully. They then read that relative imports require the module to know its package context, which only happens when imported. They change their command to \`python -m mypkg.cli\` from the project root. The script executes successfully because Python treats \`mypkg\` as a package and \`cli\` as a module within it, establishing the parent package context needed for the relative import.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T04:13:50.139512+00:00— report_created — created