【问题标题】:Argparse with default file paths running from jupyter notebook使用从 jupyter notebook 运行的默认文件路径的 Argparse
【发布时间】:2019-01-16 15:12:21
【问题描述】:

我是 python 的初学者。

我的 python 脚本中有以下代码,我从命令行运行脚本,但我想从 jupyter notebook 运行,而不是将其路径传递给文档

if __name__ == "__main__":
   parser = argparse.ArgumentParser()
   parser.add_argument('--names-file', required=True, type=str, help="names file")
   parser.add_argument('--class-file', required=True, type=str, help="class file")
   parser.add_argument('--registration-file', required=True, type=str, help="registration file")

   args=parser.parse_args()
   for f in [args.names_file, args.class_file, args.registration_file]:
   if not Path(f).exists():
   print("File doesn't exist")
   sys.exit()

【问题讨论】:

    标签: python jupyter-notebook argparse


    【解决方案1】:

    你可以使用%run jupyter 魔法线函数,或者!command line

    命令行/jupyter魔法线:

    %run full_filename_including_path arguments
    

    或者,使用!作为前缀的命令行:

    !ipython full_filename_including_path arguments
    

    【讨论】:

    • 谢谢,我在我的代码中究竟应该在哪里添加它?
    • 您需要在 jupyter 笔记本单元格中将其作为单行运行
    猜你喜欢
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2017-10-26
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多