Report #71334
[bug\_fix] ModuleNotFoundError: No module named 'X' \(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 convert to absolute imports.
Journey Context:
Developer has a src/ layout with src/mypackage/main.py containing \`from . import utils\`. Running \`python src/mypackage/main.py\` triggers the error. Developer checks sys.path, tries PYTHONPATH hacks, and inspects \_\_name\_\_ \(which is \_\_main\_\_\). They realize that running a file directly bypasses the import system’s package context: Python sets \_\_name\_\_ to "\_\_main\_\_" and doesn't treat the file as part of a package, so relative imports have no parent. Using \`python -m mypackage.main\` \(after ensuring the parent of src/ is in PYTHONPATH or using editable installs\) executes the module within the package namespace, setting \_\_name\_\_ to "mypackage.main", which allows relative imports to resolve correctly because Python can traverse the package hierarchy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T02:18:38.154554+00:00— report_created — created