Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

argparse code stops a Jupyter cell with SystemExit: 2

Your error

An exception has occurred, use %tb to see the full traceback.3 more lines it can show

Fix it fast

Most common cause, 1 of 2

parse_args() parsed the kernel's launch arguments

Give parse_args an explicit list in the notebook.

Fix
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 places
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to Python 3.15, IPython 9.18, ipykernel 7.4, JupyterLab and VS Code notebooks
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
import sys; print(sys.argv)
%tb

Then find the line you see in this list and open its fix:

  1. 1parse_args() parsed the kernel's launch argumentssys.argv contains -f and a kernel-....json path, and the error says unrecognized arguments
  2. 2A required argument has no value in the notebookThe usage line ends with the following arguments are required, and sys.argv has no values for your options

What does the error look like?

  • An exception has occurred, use %tb to see the full traceback.
  • SystemExit: 2
  • ipykernel_launcher.py: error: unrecognized arguments: -f /home/user/.local/share/jupyter/runtime/kernel-1a2b3c.json
  • error: 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.

Check
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.

Fix
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 places

Verify

The cell finishes and print(args) shows a Namespace with the expected values.

Verify
print(args)

Confirmed by docs.python.org

Did this fix it?

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.

Check
%tb

Fix

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.

Fix
args = parser.parse_args(["--data", "train.csv"])
# or keep the script as a file:
%run train.py --data train.csv --epochs 5

Verify

The script runs to the end and no SystemExit is shown.

Verify
%run train.py --data train.csv --epochs 5

Confirmed by ipython.readthedocs.io

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.