我经常被要求调试其他人编写的 Python 脚本。我想将这些脚本发送到 IPython,以便在脚本失败时将其放入 IPython shell。
不幸的是,我找不到发送脚本所需的(必需的)命令行选项的方法。
当我将脚本及其选项传递为:
ipython <script_name> <script_options>
有解决方案或解决方法吗?
ipython -- sometest.py 1 2 3 4
ipython -i -c "%run test.py 1 2 3 4"
I know there's an already accepted solution, but in the most recent version of ipython this won't work. Here's a cut and paste of the command I use to run tornado tests with --autoreload
ipython --c="%run test.py --autoreload"
This is using ipython .11.
简单的例子在这里。
脚本.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 配置文档可在此处获得: IPython 配置简介 — IPython 文档,以及force_interact
此处描述的选项:终端 IPython 选项 — IPython 文档。