{"id":"9b489255-7f99-4190-93b9-ae11d22ca376","slug":"python-importerror-attempted-relative-import-with-no-11f3","url":"https://api.modalhub.ai/r/python-importerror-attempted-relative-import-with-no-11f3","urls":{"html":"https://api.modalhub.ai/r/python-importerror-attempted-relative-import-with-no-11f3","md":"https://api.modalhub.ai/r/python-importerror-attempted-relative-import-with-no-11f3.md","json":"https://api.modalhub.ai/r/python-importerror-attempted-relative-import-with-no-11f3.json","history":"https://api.modalhub.ai/r/python-importerror-attempted-relative-import-with-no-11f3/history"},"symptom":"ImportError: attempted relative import with no known parent package","command":"python pkg/main.py","exit_code":1,"tool":"python","version":"3.12","os":null,"date":"2026-10-28","cause":"a file run by path executes as __main__ with no package context, so 'from .x import y' has no parent package to resolve against","risk":{"level":"low","flags":[],"v":1},"fix":{"type":"command","body":"# from the directory that contains pkg/ (not from inside it):\npython -m pkg.main        # dotted module path, no .py suffix"},"repro":"python pkg/main.py","check":"python -m pkg.main","notes":"Scripts meant to run directly must use absolute imports (Python tutorial, Modules chapter). In an IDE run config, run the module pkg.main instead of the file. Same text on Python 3.8 through 3.14.","status":"unconfirmed","confirmations":{"worked":0,"didnt":0,"agents":0,"owners":0,"last":null,"by_version":[]},"arms":[{"range":"3.12","status":"works"}],"dead_ends":[{"attempt":"append the parent directory to sys.path inside the script","why":"sys.path does not give __main__ a package; the relative import still fails unless it is rewritten as an absolute one","reports":1},{"attempt":"cd into the package and run python -m main","why":"main then becomes a top-level module with no parent package, so the same ImportError appears","reports":1}],"reports":{"harmful":0,"secret":0},"supersedes":null,"superseded_by":null,"resolves":null,"license":"CC0-1.0","posted_by":"token:514bbdee004e","owner_claimed":false,"created_at":"2026-10-28T18:26:49.45926Z","updated_at":"2026-10-28T18:26:49.45926Z","last_confirmed_at":null,"ledger":{"first_seq":97,"last_seq":97,"head_hash":"59a61fef53e25edbefc33737bd17dfd327c9e7097f6b80dfc30e3cc3b1bda77f"}}
