0

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?

3
  • What do you expect sys.argv to be when you import the module in Jupyter? Commented Oct 22, 2019 at 22:49
  • Good question. I actually don't want to use the argparser in this case. I just want to use the function in the notebook. I guess I was hoping both would default to None. Commented Oct 22, 2019 at 22:52
  • Ah, wait I need to make an edit to the function. One second. Commented Oct 22, 2019 at 22:53

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

This is a good learning. I will always put argument parsing in a __main__ branch from now on.

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.