简体   繁体   中英

Unrecognized arguments exception when importing from Python script in Jupyter notebook

I'm not sure if this is a bug or if I'm doing something wrong.

I have a stand-alone python script that uses argparse for launching from the command-line.

I dumbed-it down a bit but this is essentially the script:

test.py

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-i", "--inputfile", type=str, default=None,
                    help="filename containing input data")
parser.add_argument("-o", "--outputfile", type=str, default=None,
                    help="filename to save output data to")
args = parser.parse_args()


def step_test_experiment(inputs_filename=None, outputs_filename=None):
    print(inputs_filename, outputs_filename)

if __name__ == '__main__':

    if args.outputfile is None:
        outputs_filename = "tclab_dyn_data.csv"
    else:
        outputs_filename = args.outputfile
    step_test_experiment(args.inputfile, outputs_filename)

When I try the following in a Jupyter notebook:

from test import step_test_experiment

I get:

usage: ipykernel_launcher.py [-h] [-i INPUTFILE] [-o OUTPUTFILE]
ipykernel_launcher.py: error: unrecognized arguments: -f /Users/billtubbs/Library/Jupyter/runtime/kernel-df168fd5-a7f9-4258-b106-0e4582601e79.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2


//anaconda3/envs/torch/lib/python3.7/site-packages/IPython/core/interactiveshell.py:3334: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

%tb produces:

---------------------------------------------------------------------------
SystemExit                                Traceback (most recent call last)
<ipython-input-1-14c395429157> in <module>
----> 1 from test import step_test_experiment

~/dd-temp-control/test.py in <module>
      6 parser.add_argument("-o", "--outputfile", type=str, default=None,
      7                     help="filename to save output data to")
----> 8 args = parser.parse_args()
      9 
     10 

//anaconda3/envs/torch/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 

//anaconda3/envs/torch/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)

//anaconda3/envs/torch/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

Looks like Jupyter kernel is adding an '-f' argument when it loads the script.

Any ideas what is causing this?

If, as you said in the comments,

I actually don't want to use the argparser in this case

the solution is to move the entire argument-parsing block into the if __name__ == "__main__": block, resulting in:

def step_test_experiment(inputs_filename=None, outputs_filename=None):
    print(inputs_filename, outputs_filename)

if __name__ == '__main__':

    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--inputfile", type=str, default=None,
                    help="filename containing input data")
    parser.add_argument("-o", "--outputfile", type=str, default=None,
                    help="filename to save output data to")
    args = parser.parse_args()
    if args.outputfile is None:
        outputs_filename = "tclab_dyn_data.csv"
    else:
        outputs_filename = args.outputfile
    step_test_experiment(args.inputfile, outputs_filename)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM