Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google-oauth inside Jupyter Notebook

I am using Google oauth2client and the code from sample is:

flags = argparser.parse_args()
credentials = run_flow(flow, storage, flags)

All works in Python interactive or IDE but if I am trying to use the code from Jupiter Notebook I got an exception.

Inside Jupiter Noteboo I am trying simple:

    from oauth2client.tools import argparser
    argparser.parse_args()

and got:

usage: __main__.py [--auth_host_name AUTH_HOST_NAME]
                   [--noauth_local_webserver]
                   [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
                       [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
    __main__.py: error: unrecognized arguments: -f /run/user/1000/jupyter/kernel-c9aa5199-fcea-4884-8e5f-a004c66a733e.json

SystemExit                                Traceback (most recent call last)
<ipython-input-3-d141fc7aebe0> in <module>()
----> 1 argparser.parse_args()

/usr/lib/python3.5/argparse.py in parse_args(self, args, namespace)
   1736         if argv:
   1737             msg = _('unrecognized arguments: %s')
-> 1738             self.error(msg % ' '.join(argv))
   1739         return args
   1740 

/usr/lib/python3.5/argparse.py in error(self, message)
   2392         self.print_usage(_sys.stderr)
   2393         args = {'prog': self.prog, 'message': message}
-> 2394         self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

/usr/lib/python3.5/argparse.py in exit(self, status, message)
   2379         if message:
   2380             self._print_message(message, _sys.stderr)
-> 2381         _sys.exit(status)
   2382 
   2383     def error(self, message):

SystemExit: 2
like image 995
Brans Ds Avatar asked Jan 31 '17 15:01

Brans Ds


1 Answers

From the documentation for ArgumentParser.parse_args(), "By default, the argument strings are taken from sys.argv [...]". In this case, the sys.argv command-line arguments are those that created the Jupyter notebook server process. As a workaround, assuming you don't need to use any flags, you can replace

flags = argparser.parse_args()

with

flags = argparser.parse_args([])

You can add flags as strings in the provided list like this:

flags = argparser.parse_args(['--auth_host_name=example.org', '--auth_host_port=1234'])
like image 104
argentpepper Avatar answered Nov 08 '22 08:11

argentpepper