How to fix ipykernel_launcher.py: error: unrecognized arguments in jupyter?

A more elegant solution would be:

args, unknown = parser.parse_known_args()

instead of

args = parser.parse_args()

I just ran into this problem today and found a quick, stupid solution is to insert an argument processor for the -f argument that qtconsole/ipython passes though and we did not expect. At end of parser.add_argument I put in:

parser.add_argument("-f", "--fff", help="a dummy argument to fool ipython", default="1")

I don't use the -f parameter, so there's no loss for me.

I'd rather not re-engineer a larger argument processing framework just because of ipython cropping up in workflow on one particular computer...


I got it! the reason why we get the error is because this code is using argparse and this module is used to write user-friendly command-line interfaces, so it seems, it has a conflict with Jupyter Notebook.

I found the solution in this page:

What we have to do is:

Delete or comment these lines:

parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', default=100, type=int, help='batch size')
parser.add_argument('--train_steps', default=1000, type=int,
                    help='number of training steps')

and replace args

args = parser.parse_args(argv[1:])

for a dictionary using the library easydict in this way:

args = easydict.EasyDict({
    "batch_size": 100,
    "train_steps": 1000
})

With easydict we can access dict values as attributes for the arguments.

Update

After all this year diving deeper in python, I found the solution for this question is way more simple (We don't need to use any external library or method). argparse is just one of the many ways to pass arguments to a script in python from the terminal. When I tried to do it in a jupyter notebook obviously that wasn't going to work. We can just replace in the function directly the parameters like:

funtion(batch_size=100, train_steps=1000)

Now, if we have a long list of parameters for our function, we can use *args or **kargs.

*args pass a tuple as parameters in the function, for this case, in particular, it will be:

args = (100, 1000)
function(*args)

**kargs pass a dictionary as arguments to our function:

kargs = {"batch_size": 100,
        "train_steps": 1000}
function(**kargs)

just google it and you will find a really good explanation on how to use them both, here one documentation that I used to study this.