Report #76546
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the module as a package using \`python -m package.module\` instead of \`python package/module.py\`, or switch to absolute imports. This preserves the \`\_\_package\_\_\` context required for relative imports.
Journey Context:
You are refactoring a script into a proper package with internal modules using \`from . import utils\`. You try to test one submodule by running \`python mypkg/submod.py\` directly. It crashes with the relative import error. You try adding \`if \_\_name\_\_ == "\_\_main\_\_"\` guards or manually setting \`\_\_package\_\_\`, but the imports keep failing in confusing ways. After checking \`sys.path\`, you realize running a file as a script sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\` and \`\_\_package\_\_\` to \`None\`, breaking the relative import machinery. By switching to \`python -m mypkg.submod\`, Python treats it as part of the package, \`\_\_package\_\_\` is correctly set to \`"mypkg"\`, and relative imports resolve.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T11:04:24.424888+00:00— report_created — created