'the right way to use argparse module in Jupyter Notebook

Is that possible to use argparse module in JupterNotebook?

For instance, I test the following code segment

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--x', type=int, required=True)
parser.add_argument('--y', type=int, required=True)
args = parser.parse_args()
product = args.x * args.y
print('Product:', product)

And running the above code in JupterNotebook, I got the following error message

enter image description here



Solution 1:[1]

Suppose you have this small code in python:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="increase output verbosity",
                    action="store_true")
parser.add_argument("-v_1", "--verbose_1", help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()

To write this code in Jupyter notebook write this:

import argparse

args = argparse.Namespace(verbose=False, verbose_1=False)

Note: In python, you can pass arguments on runtime but in the Jupyter notebook that will not be the case so be careful with the data types of your arguments.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Arzam Abid