Report #14771
[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\`, or refactor to absolute imports. This ensures \`\_\_package\_\_\` is correctly set.
Journey Context:
Developer creates a package \`myapp/\` with \`\_\_init\_\_.py\` and \`utils.py\`, where \`utils.py\` uses \`from . import config\`. They navigate into \`myapp/\` and run \`python utils.py\` directly. Python sets \`\_\_name\_\_\` to \`"\_\_main\_\_"\` but leaves \`\_\_package\_\_\` as \`None\`, causing the relative import to fail because Python cannot determine the parent package context. The traceback points to the import line. Developer tries adding \`if \_\_name\_\_ == "\_\_main\_\_"\` guards but it doesn't help because the import happens at module level. Realization occurs that relative imports are only valid when the module is imported as part of a package. Changing the working directory to the project root and running \`python -m myapp.utils\` causes Python to treat \`myapp\` as the package, set \`\_\_package\_\_\` correctly, and resolve the relative import through the package namespace.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:22:36.845671+00:00— report_created — created