Report #82727
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module using \`python -m package.module\` from the project root instead of \`python package/module.py\`, converting the entry-point to use absolute imports only, or guarding relative imports behind \`if \_\_name\_\_ \!= "\_\_main\_\_"\` blocks.
Journey Context:
Developer runs \`python src/mypkg/cli.py\` directly while prototyping. The file contains \`from . import utils\`. Python raises \`ImportError: attempted relative import with no known parent package\`. Developer adds \`\_\_init\_\_.py\` files \(unnecessary in Python 3\), then tries manipulating \`sys.path\` with \`sys.path.insert\(0, os.path.dirname\(os.path.dirname\(os.path.abspath\(\_\_file\_\_\)\)\)\)\`, creating fragile path hacks. After reading the traceback, they realize that when a file is run as a script \(\`\_\_name\_\_ == "\_\_main\_\_"\`\), Python does not treat it as part of a package, so the relative import has no package context. Switching to \`python -m mypkg.cli\` from the repository root preserves the package hierarchy, allowing the relative import to resolve correctly against the parent package.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T21:26:37.774064+00:00— report_created — created