【发布时间】:2010-11-09 20:18:19
【问题描述】:
我经常被要求调试其他人编写的 Python 脚本。我想将这些脚本发送到 IPython,以便在脚本失败时将其放入 IPython shell。
很遗憾,我找不到发送脚本所需的(必需)命令行选项的方法。
当我将脚本及其选项传递为:
ipython <script_name> <script_options>
是否有解决方案或变通方法?
【问题讨论】:
我经常被要求调试其他人编写的 Python 脚本。我想将这些脚本发送到 IPython,以便在脚本失败时将其放入 IPython shell。
很遗憾,我找不到发送脚本所需的(必需)命令行选项的方法。
当我将脚本及其选项传递为:
ipython <script_name> <script_options>
是否有解决方案或变通方法?
【问题讨论】:
ipython -- sometest.py 1 2 3 4
【讨论】:
ipython -i -- sometest.py 1 2 3 4 语法。
ipython --pdb -- somtest.py 1 2 3 4。
ipython -i -c "%run test.py 1 2 3 4"
【讨论】:
我知道有一个已经被接受的解决方案,但是在最新版本的 ipython 中这不起作用。这是我使用 --autoreload 运行龙卷风测试的命令的剪切和粘贴
ipython --c="%run test.py --autoreload"
这是使用 ipython .11。
【讨论】:
简单示例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
【讨论】:
可以通过用户的 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。
【讨论】: