Report #104228
[bug\_fix] ImportError: attempted relative import with no known parent package
Run the script as a module using \`python -m package.module\` instead of \`python script.py\`, or adjust the package structure to ensure \`\_\_init\_\_.py\` files are present and the script is executed from the parent directory. Alternatively, use absolute imports.
Journey Context:
A developer had a project structure like \`project/package/module.py\` and \`project/package/\_\_init\_\_.py\`. Inside \`module.py\`, they used \`from . import something\` to import from the same package. When they ran \`python module.py\` from within the \`package\` directory, they got the \`ImportError: attempted relative import with no known parent package\`. The root cause: relative imports require the module to be part of a package; when executing a script directly, Python sets \`\_\_name\_\_\` to \`\_\_main\_\_\` and \`\_\_package\_\_\` to \`None\`, so relative imports fail. The fix was to run the script from the \`project\` directory as \`python -m package.module\`, which tells Python to treat it as a module of the package. This is a common stumbling block for developers new to Python packaging.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-19T20:04:54.155713+00:00— report_created — created