Report #88711
[bug\_fix] ImportError: attempted relative import with no known parent package
Execute the module using \`python -m package.submodule\` from the project root \(the directory containing the package folder\), rather than \`python package/submodule.py\`. This sets \`\_\_package\_\_\` correctly. Alternatively, restructure to use absolute imports only. The root cause is that running a file directly sets \`\_\_name\_\_\` to \`'\_\_main\_\_'\` and \`\_\_package\_\_\` to \`None\`, so relative imports \(which rely on \`\_\_package\_\_\` to know the parent\) cannot resolve.
Journey Context:
You have a project structure with \`myapp/utils/helper.py\` and \`myapp/main.py\`. In \`main.py\`, you have \`from .utils import helper\`. You try to run it with \`python myapp/main.py\` and immediately get \`ImportError: attempted relative import with no known parent package\`. You try adding \`sys.path.insert\(0, os.path.dirname\(os.path.abspath\(\_\_file\_\_\)\)\)\` but that doesn't fix it because the issue is not path searching but the import system's package context. You research and learn that relative imports are package-context aware. When you use \`python -m myapp.main\` from the parent directory, Python treats \`myapp\` as a package, sets \`\_\_package\_\_\` to \`'myapp'\`, and the relative import \`from .utils import helper\` correctly resolves to \`myapp.utils.helper\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T07:29:19.066488+00:00— report_created — created