argparse code stops a Jupyter cell with SystemExit: 2
Fix it fast
Most common cause, 1 of 2parse_args() parsed the kernel's launch arguments
Give parse_args an explicit list in the notebook.
args = parser.parse_args([]) # notebook: defaults only
args = parser.parse_args(["--epochs", "5"]) # notebook: chosen values
args, _unknown = parser.parse_known_args() # works in both placesShort answer
SystemExit: 2 with use %tb to see the full traceback means argparse called error() inside a notebook. parse_args() read the kernel's own sys.argv, which holds -f and a connection file, or a required argument got no value. Pass an explicit list such as parser.parse_args([]), or run the file with %run and its arguments. Applies to Python 3.15 and IPython 9.18.
Which cause do you have?
Print what the kernel put in sys.argv, then show the last traceback. Extra kernel flags point to cause 1; an empty argument list with a required-argument message points to cause 2.
import sys; print(sys.argv)
%tbThen find the line you see in this list and open its fix:
What does the error look like?
An exception has occurred, use %tb to see the full traceback.SystemExit: 2ipykernel_launcher.py: error: unrecognized arguments: -f /home/user/.local/share/jupyter/runtime/kernel-1a2b3c.jsonerror: the following arguments are required: --data
Cause 1 of 2
parse_args() parsed the kernel's launch arguments
You seesys.argv contains -f and a kernel-....json path, and the error says unrecognized arguments
Check
With no args, parse_args() takes its input from sys.argv. In a notebook that list belongs to the kernel: ipykernel starts with -m ipykernel_launcher -f {connection_file}. argparse does not know -f, prints unrecognized arguments and exits with status 2, which IPython shows as SystemExit: 2.
print(sys.argv[1:])Fix
Give parse_args an explicit list in the notebook. If the same code must also run from a terminal, parse_known_args() ignores flags it does not know and returns them separately.
args = parser.parse_args([]) # notebook: defaults only
args = parser.parse_args(["--epochs", "5"]) # notebook: chosen values
args, _unknown = parser.parse_known_args() # works in both placesVerify
The cell finishes and print(args) shows a Namespace with the expected values.
print(args)Confirmed by docs.python.org
Cause 2 of 2
A required argument has no value in the notebook
You seeThe usage line ends with the following arguments are required, and sys.argv has no values for your options
Check
Positional arguments and options declared with required=True must be supplied on every call. A notebook never passes them, so argparse reports the missing names and stops with status 2 even after the kernel flags are removed.
%tbFix
Supply the values in the list you pass to parse_args, or keep the code in a .py file and start it with %run followed by the same arguments you would type in a terminal; IPython puts them in sys.argv for that run.
args = parser.parse_args(["--data", "train.csv"])
# or keep the script as a file:
%run train.py --data train.csv --epochs 5Verify
The script runs to the end and no SystemExit is shown.
%run train.py --data train.csv --epochs 5Confirmed by ipython.readthedocs.io
How do I stop it coming back?
- Wrap parsing in a main(argv=None) function that calls parser.parse_args(argv), so a notebook can pass a list.
- Give every option a sensible default unless it truly must come from the caller.
- Keep training and data scripts as .py files and drive them from notebooks with %run.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.