1

I'm trying to run some code with a jupyter notebook, but from the beginning, I have issues.

Indeed, it looks like I can't use these commands:

parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')

usage: ipykernel_launcher.py [-h] [--lr LR] [--resume] and ipykernel_launcher.py gives error: unrecognized arguments: -f

And:

args = parser.parse_args()

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py in parse_args(self, args, namespace)
   1750         if argv:
   1751             msg = _('unrecognized arguments: %s')
-> 1752             self.error(msg % ' '.join(argv))
   1753         return args
   1754 

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py in error(self, message)
   2499         self.print_usage(_sys.stderr)
   2500         args = {'prog': self.prog, 'message': message}
-> 2501         self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/argparse.py in exit(self, status, message)
   2486         if message:
   2487             self._print_message(message, _sys.stderr)
-> 2488         _sys.exit(status)
   2489 
   2490     def error(self, message):

SystemExit: 2

I've seen that some people already had this problem when trying to use jupyter and arg_parse, but I can't find an easy solution.

Thank you for your help!

2
  • Tell us what you learned from other people's problems. Are you expecting us to give you a solution that they didn't receive? Commented Mar 19, 2019 at 16:51
  • The thing is I didn’t understand the other explanations, and that’s why I had to open a new question :/ Commented Mar 19, 2019 at 18:00

1 Answer 1

1

When I launch a blank notebook and execute

import sys
sys.argv

I get

['/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py',
 '-f',
 '/run/user/1000/jupyter/kernel-b2d72478-6aaa-4206-9bf3-38fc1a0fd303.json']

parse_args() uses the sys.argv list that 'normally' comes from the shell that's calling your script. It is a list of the commandline strings.

The sys.argv that your script sees includes this -f argument, created by jupyter.

You could use parse_known_args() so that your parser doesn't raise this error. But you that won't help you get --lr or -r arguments - because jupyter's own parser will reject them.

The essence of the previous SO questions is that you can't provide commandline arguments to a notebooks.

Sign up to request clarification or add additional context in comments.

5 Comments

So there is no solutions in my case?? Maybe I can just say that lr = 0.1to replace the first add_argument, but for the second add_argument, I don't know what is it: '--resume', '-r', action='store_true', help='resume from checkpoint') so I don't know how to replace it !!
Just to be clear, are you running a Python script ( a .py file) or a notebook (.ipynb)?
Thank you for your answer: A .ipynb
Can't you just control all of that from within the notebook - once it's started? The purpose the jupyter notebook command is to start the server, not to talk to or control the notebook. The server could be running several different notebooks.
I'm sorry but I don't really understand what you want we to do exactly??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.