Reputation: 1745
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!
Upvotes: 1
Views: 1780
Reputation: 231665
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.
Upvotes: 1