Report #76125
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module as a package using \`python -m package.module\` \(e.g., \`python -m mypkg.cli\`\) from the project root, rather than \`python mypkg/cli.py\`. Alternatively, convert to absolute imports or set \`\_\_package\_\_\` manually if running as script.
Journey Context:
You have a project structure with \`mypkg/\_\_init\_\_.py\` and \`mypkg/cli.py\`. Inside \`cli.py\` you use \`from .utils import helper\` \(a relative import\). When you execute \`python mypkg/cli.py\` directly, you get 'attempted relative import with no known parent package'. You try moving the import to the bottom of the file, but it still fails because the fundamental issue is that running a file as a script sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\` and \`\_\_package\_\_\` to \`None\`, so Python doesn't recognize it as part of a package. The relative import has no parent context. The correct fix is to run it as a module: \`python -m mypkg.cli\` from the parent directory. This preserves the package context, sets \`\_\_package\_\_\` correctly, and allows relative imports to resolve. This is the standard way to run executable modules within packages.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T10:21:54.882537+00:00— report_created — created