【问题标题】:Command-line options to IPython *scripts*?IPython *脚本*的命令行选项?
【发布时间】:2010-11-09 20:18:19
【问题描述】:

我经常被要求调试其他人编写的 Python 脚本。我想将这些脚本发送到 IPython,以便在脚本失败时将其放入 IPython shell。

很遗憾,我找不到发送脚本所需的(必需)命令行选项的方法。

当我将脚本及其选项传递为:

ipython <script_name> <script_options>

是否有解决方案或变通方法?

【问题讨论】:

    标签: python ipython


    【解决方案1】:
    ipython -- sometest.py 1 2 3 4
    

    【讨论】:

    • 它有效,但我在文档中找不到这个,你能指出这是记录在哪里吗?
    • 还要保留在交互式 shell 中,使用 ipython -i -- sometest.py 1 2 3 4 语法。
    • 注意'--'是系统shell解释器的一个特性,并不是ipython shell特有的
    • 如果你喜欢用 ipdb 调试,同样可以使用ipython --pdb -- somtest.py 1 2 3 4。
    【解决方案2】:
    ipython -i -c "%run test.py 1 2 3 4"
    

    【讨论】:

    • 非常好!不幸的是,当它出现异常时,它会让我回到操作系统命令行,而不是 IPython 提示符。有什么建议吗?
    • 你可以启动 ipython 然后执行 %run test.py 1 2 3 4
    • 实际上添加 -i 使它留在 shell 中。
    • @jimbob 如果您不介意我问,您在哪里找到“-i”选项?我在“ipython -help”和“ipython.scipy.org/doc/stable/html/interactive/reference.html”中找不到它
    • @JS 我只在 man python (not ipython) 中找到它:“-i 当脚本作为第一个参数传递或使用 -c 选项时,执行脚本后进入交互模式或命令。它不读取 $PYTHONSTARTUP 文件。当脚本引发异常时,这对于检查全局变量或堆栈跟踪很有用。有点令人沮丧,它不在 man ipython 中,但试了一下,它奏效了。
    【解决方案3】:

    我知道有一个已经被接受的解决方案,但是在最新版本的 ipython 中这不起作用。这是我使用 --autoreload 运行龙卷风测试的命令的剪切和粘贴

    ipython --c="%run test.py --autoreload"
    

    这是使用 ipython .11。

    【讨论】:

      【解决方案4】:

      简单示例here。

      script.py

      from sys import argv
      
      script, first, second, third = argv
      
      print "The script is called:", script
      print "Your first variable is:", first
      print "Your second variable is:", second
      print "Your third variable is:", third
      

      外壳:

      $ ipython script.py stuff things that
      The script is called: ex13.py
      Your first variable is: stuff
      Your second variable is: things
      Your third variable is: that
      

      【讨论】:

        【解决方案5】:

        可以通过用户的 IPython 配置文件中的设置来控制 IPython 行为的许多方面,这些文件通常位于 ~/.ipython/ 中。一个用户可以创建多个配置文件,每个配置文件都有不同的配置参数设置。每个配置文件的设置都位于 .ipython 文件夹中的单独文件夹中。默认配置文件位于profile_default,其中用于自定义行为的主文件是ipython_config.py。默认情况下,它几乎完全被注释,注释行显示配置变量及其默认设置。取消注释或插入行以改变行为。

        要更改 IPython 在脚本运行结束时的行为方式,请使用:

        c.TerminalIPythonApp.force_interact = True
        

        然后当脚本结束(或引发异常)时,IPython 将继续运行并为您提供提示。这与ipython -i 的行为相同。

        我在我的默认配置文件中使用此设置,因为这是我始终希望 IPython 的行为方式。如果您不是这种情况,您可以创建具有此行为的配置文件,仅在您需要此行为时使用。或者继续使用(显然没有记录)-i 选项。

        IPython 配置文档在此处提供:Introduction to IPython configuration — IPython documentation,force_interact 选项在此处描述:Terminal IPython options — IPython documentation。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-26
          • 1970-01-01
          • 2017-08-05
          • 2021-07-10
          • 1970-01-01
          相关资源
          最近更新 更多