我经常被要求调试别人写的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"
我知道已经有一个被接受的解决方案,但在最近版本的ipython中,这种方法不起作用。以下是我使用的命令来运行带有--autoreload的tornado测试的剪切和粘贴。
ipython --c="%run test.py --autoreload"
这是使用 ipython .11。
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
shell:
$ 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/
中。用户可以创建多个配置文件(profiles),每个配置文件具有不同的配置参数设置。每个配置文件都有单独的文件夹来存储其设置在.ipython
文件夹中。默认配置文件在profile_default
中,用于自定义行为的主要文件是ipython_config.py
。默认情况下,它几乎全部被注释掉,注释行显示了配置变量及其默认设置。取消注释或插入行以更改行为。
要更改IPython在运行脚本结束时的行为,请使用:
c.TerminalIPythonApp.force_interact = True
然后当脚本结束(或引发异常)时,IPython将继续运行并呈现给您提示符。这与ipython -i
的行为相同。
我在我的默认配置文件中使用此设置,因为这是我希望IPython始终如一的行为。如果对于您来说不是这种情况,则可以创建具有此行为的配置文件,仅在需要此行为时使用。或者继续使用(显然未记录的)-i
选项。
IPython配置文档可在此处找到:IPython 配置简介 — IPython 文档,其中描述了force_interact
选项:终端IPython选项 — IPython文档。
ipython -i -- sometest.py 1 2 3 4
语法。注意不要改变原意,使句子更加通俗易懂,同时不添加解释或其他内容。 - Daniel Sokolowskiipython --pdb -- somtest.py 1 2 3 4
。 - SpinUp __ A Davis