0

I have a .py file following the normal code structure

def  main( args ): 
        .......
        .......

   if __name__ == "__main__":
      parser = argparse.ArgumentParser(description = “ forecasting example”)
      parser.add_argument("--train-window", default=2160, type=int)
      parser.add_argument("--test-window", default=336, type=int)
      parser.add_argument("--stride", default=168, type=int)
      parser.add_argument("-n", "--num-steps", default=501, type=int)
      parser.add_argument("-lr", "--learning-rate", default=0.05, type=float)
      parser.add_argument("--dct", action="store_true")
      parser.add_argument("--num-samples", default=100, type=int)
     parser.add_argument("--log-every", default=50, type=int)
     parser.add_argument("--seed", default=1234567890, type=int)
     args = parser.parse_args()
     main(args)

I was trying to run this program in Jupyter notebook, but it will get errors such as

usage: ipykernel_launcher.py [-h] [--train-window TRAIN_WINDOW]
                             [--test-window TEST_WINDOW] [--stride STRIDE]
                             [-n NUM_STEPS] [-lr LEARNING_RATE] [--dct]
                             [--num-samples NUM_SAMPLES]
                             [--log-every LOG_EVERY] [--seed SEED]
ipykernel_launcher.py: error: unrecognized arguments: -f C:\Users\AppData\Roaming\jupyter\runtime\kernel-4c744f03-3278-4aaf-af5e-50c96e9e41cd.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

my question is that, what are the right approaches or the modifications I need to make if I want to run a python program, which setup input parameters using argparse type of mechanism, in Jupyter Notebook?

4
  • What command caused the error you showed? Commented Jul 14, 2021 at 18:37
  • 1
    Using commandline arguments in a notebook is, at best, awkward. The sys.argv the notebook sees comes from the kernel, with information on the interfacing javascript file. Arguments provided when starting the server are for the server itself, not for any of the notebooks it might run. Commented Jul 14, 2021 at 18:42
  • The code seems to be incorrectly indented, anyway; if __name__ == "__main__": should be outside def main(): Commented Jul 14, 2021 at 19:08
  • -f C:\Users\AppData\Roaming\jupyter\runtime\kernel-4c744f03-3278-4aaf-af5e-50c96e9e41cd.json is from the server telling this kernel which json file to use. Your parser can't use it. Commented Jul 14, 2021 at 19:24

1 Answer 1

1

Your code should be indented differently so you can import it into your notebook, or into another Python script. The whole point of the if __name__ == "__main__": block is that it gets executed immediately when Python parses the file; the condition is true only when you run the file directly, not when you import it. But the block needs to be indented differently, so that it's not inside any def or class or other block structure.

The way to use this from a notebook, then, is to call main (or whichever other functions from the imported code you want to run) with your desired parameters.

In this case, main has been designed to expect an Argparse object as its argument, which is quite unfortunate. A better design would simply do the argument parsing inside main, and expose a different function or set of functions for reuse as a library.

Assuming your main function's internals look something like

def main(args):
    print(
        real_main(args.train_window, args.test_window,
                  stride=args.stride, num_steps=args.num_steps,
                  learning_rate=args.learning_rate,
                  dct=args.dct, num_samples=args.num_samples,
                  log_every=args.log_every, seed=args.seed))

and supposing you wanted to run the equivalent of

python thatfile.py -n 23 -lr 0.7--dct --num-samples 2300

the equivalent code in your notebook would look like

from thatfile import real_main as that_main

print(that_main(2160, 336, num_steps=23,
                learning_rate=0.7, dct=True,
                num_samples=2300))

where the first two values are simply copied from the argparse defaults, and I obviously had to speculate a great deal about which parameters are required and which are optional keyword parameters, and whether they are named identically to the argparse field names.

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

Comments

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.