How to fix ipykernel_launcher.py error unrecognized arguments in jupyter?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The error "ipykernel_launcher.py: error: unrecognized arguments" occurs when code using argparse (or similar argument parsers) runs in a Jupyter notebook. The Jupyter kernel process passes its own command-line arguments (like -f kernel-xxxx.json), which argparse does not recognize and treats as errors. The fix is to use parse_known_args() instead of parse_args(), which ignores unrecognized arguments. This article covers the cause, multiple fixes, and how to handle argument parsing in notebooks properly.
The Error
Jupyter passes -f <connection_file> to the kernel process. parse_args() fails because it does not know about -f.
Why This Happens
When you run a Jupyter notebook, the kernel is launched as:
sys.argv in a notebook cell looks like:
argparse.parse_args() reads sys.argv[1:] by default. Since it does not know about -f, it raises an error.
Fix 1: Use parse_known_args() (Recommended)
parse_known_args() parses recognized arguments and returns unrecognized ones separately instead of raising an error. This is the simplest fix and works in both scripts and notebooks.
Fix 2: Pass an Empty List
This is useful when you want to hardcode arguments in a notebook for experimentation.
Fix 3: Check if Running in Jupyter
This lets the same code work as both a script and a notebook.
Fix 4: Clear sys.argv
This is a quick hack but can break other libraries that read sys.argv.
Fix 5: Use a Configuration Dict Instead
For notebook-heavy workflows, replace argparse with a simple config dictionary:
For more structure, use a dataclass:
Handling Third-Party Libraries
Some libraries (like absl-py, tensorflow) use their own flag parsing that causes the same issue:
Common Pitfalls
- Using
parse_args()in code that runs in both scripts and notebooks:parse_args()always readssys.argv, which contains kernel arguments in Jupyter. Useparse_known_args()as a universal solution that works in both environments. - Clearing
sys.argvglobally: Settingsys.argv = [sys.argv[0]]affects all code in the session. Other libraries or notebook extensions that depend onsys.argvmay break. Preferparse_known_args()or passing an explicit empty list. - Not handling
unknownargs fromparse_known_args():parse_known_args()returns a tuple(args, unknown). If you only capture one return value, you get the tuple, not just the args. Always unpack both:args, _ = parser.parse_known_args(). - Importing argparse-using modules at the top level: If a module calls
parse_args()during import (in module-level code), it fails immediately when imported in a notebook. Move argument parsing insideif __name__ == "__main__":in library modules. - Forgetting to test in both environments: Code using
parse_known_args()silently ignores unknown arguments. If you accidentally typo a flag name (--epohcsinstead of--epochs), it silently uses the default. Add validation after parsing to catch this.
Summary
- The error occurs because Jupyter's kernel passes
-f kernel.jsontosys.argv, whichargparsedoes not recognize - Use
parse_known_args()instead ofparse_args()— the simplest and most portable fix - Pass an explicit empty list
parse_args([])to use defaults in notebooks - Use
is_notebook()detection for code that must work as both a script and a notebook - For notebook-first workflows, replace argparse with config dictionaries or dataclasses
- Always guard module-level argument parsing with
if __name__ == "__main__":to prevent import-time errors
Related reading
- How to fix ''jupyter'' is not recognized as an internal or external command, operable program or batch file when running Jupyter on Windows?
- How to fix ROC curve with points below diagonal?
- how to fix There is at least 1 reference to internal data in the interpreter in the form of a numpy array or slice and run inference on tf.lite
- How to flatten a hierarchical index in columns
- HOW TO FIX IT? AttributeError module 'keras.preprocessing.image' has no attribute 'load_img
- How to fix MatMul Op has type float64 that does not match type float32 TypeError?
- How to fix issue of 'Unable to connect to the server EOF' Kubernetes - Kubectl
- How to fix java.io.NotSerializableException org.apache.kafka.clients.consumer.ConsumerRecord in Spark Streaming Kafka Consumer?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.