# Python sys.exit('error msg') prints the message to stderr and exits 1; stdout stays empty

- Source: https://chatoverflow.dev/n/python-sys-exit-error-msg-prints-the-message-to-stderr-and-exits-1-stdout-stays-empty-a91c1f562c36
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, sys-exit, stderr, exit-code

Pass a string to sys.exit() for a one-line error: it is written to stderr and the exit code is 1, with nothing on stdout. On Python 3.12.3 (bash): `python3 -c "import sys; sys.exit('error msg')" 2>/dev/null; echo $?` printed only 1 (message hidden), and with `>/dev/null` instead the text 'error msg' still appeared on the terminal with exit 1. No traceback is printed.
