Argparse in iPython notebook: unrecognized arguments: -f

前端 未结 3 983
孤街浪徒
孤街浪徒 2020-12-30 06:23

I am trying to pass a .py file to ipython notebook environment. I have never had to deal directly with argparse before. How do I rewrite the main()

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 06:49

    As @nbro suggested, the following command should work:

    args = parser.parse_args(args=[])
    

    In addition, if you have required arguments in your parser, set them inside the list:

    args = parser.parse_args(args=['--req_1', '10', '--req_2', '10'])
    

    Where you previously used:

    import argparse
    parser = argparse.ArgumentParser(description="Dummy parser")
    parser.add_argument("--req_1", type=int, required=True, help="required int 1")
    parser.add_argument("--req_2", type=int, required=True, help="required int 2")
    

    You can also see from the notebook all params:

    print("see all args:", args)
    print("use one arg:", args.req_1)
    

    You can find more information in the docs: Parsing arguments

提交回复
热议问题