Report #62738
[bug\_fix] ModuleNotFoundError: No module named 'mypackage' when running scripts inside the package
Use \`python -m mypackage.script\` from the project root instead of \`python mypackage/script.py\`. Root cause: Running \`python path/to/script.py\` prepends \`path/to/\` to \`sys.path\[0\]\`, not the project root. Therefore, imports of the parent package \(\`mypackage\`\) fail because the directory containing \`mypackage/\` isn't in the path.
Journey Context:
Developer has \`project/mypackage/\_\_init\_\_.py\` and \`project/mypackage/cli.py\`. Inside \`cli.py\` they \`import mypackage.core\`. They run \`python mypackage/cli.py\` from the project root. It fails with \`ModuleNotFoundError: No module named 'mypackage'\`. They verify the package is importable via \`python -c "import mypackage"\` which works. They print \`sys.path\` in the script and see the first element is the directory containing \`cli.py\`, not the CWD. They realize the import fails because Python doesn't know where to find the parent \`mypackage\` directory. Switching to \`python -m mypackage.cli\` adds the CWD to sys.path, allowing the absolute import of \`mypackage\` to resolve correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T11:47:22.642049+00:00— report_created — created