检查ipython中最后一个命令的退出状态 [英] Check the exit status of last command in ipython

查看:53
本文介绍了检查ipython中最后一个命令的退出状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何检查ipython中最后执行的命令(退出代码)的状态吗?

Does anybody know how to check the status of the last executed command (exit code) in ipython?

推荐答案

我假设您正在谈论使用!转义符从IPython运行命令:

I assume that you are talking about running commands from IPython using the ! escape:

In[1]: !echo hello
hello

In[2]:

使用Google,我找到了文档,并且没有提到在任何地方捕获的命令的退出状态.使用dir(),我寻找了可能包含该信息的变量名,但没有找到任何东西.我尝试了x = !ls语法,并且x被设置为命令的输出行列表;那里没有退出状态.

Using Google I found the documentation and there is no mention of the exit status of the command being captured anywhere. Using dir() I looked for a variable name that might be holding that information and I didn't find anything. I tried the x = !ls syntax and x gets set to a list of output lines from the command; there is no exit status in there.

简而言之,我不认为IPython甚至无法捕获这些信息.在这一点上,我想检查一下IPython的源代码以尝试找出其他内容.

In short, I don't think IPython is even capturing this information. At this point I would want to check the source code to IPython to try to figure out anything else.

您始终可以使用os.system()运行命令,并从中获取退出状态.

You can always just run the command with os.system() and get an exit status from that.

In[1]: !launch_eva
launch_eva: could not open AT Field

In[2]: import os
In[3]: exit_status = os.system("launch_eva")
launch_eva: could not open AT Field

In[4]: exit_status
3

In[5]:

因此,我们看到命令launch_eva在无法打开AT字段时返回退出状态3.

Thus we see the command launch_eva is returning exit status 3 when it can't open an AT Field.

似乎这是IPython应该保存的东西.有很多小的隐藏变量.您应该对此提出功能请求.

It seems like this is something that IPython should be saving. There are plenty of little hidden variables. You should file a feature request about this.

注意:这已在Ubuntu的IPython 0.10.1中进行了测试.另一个答案是"piotr",它表示退出代码将在IPython 0.11中捕获,即将发布.我从 https://github.com/ipython/ipython.git 的Git存储库中克隆了源代码. a>并用python ipython.py进行了测试;正如piotr所说,退出状态保存在名为_exit_status的变量中.

NOTE: This was tested in IPython 0.10.1 on Ubuntu. Another answer, by "piotr", says that exit code will be captured in IPython 0.11, due to release soon. I cloned the source code from the Git repository at https://github.com/ipython/ipython.git and tested it with python ipython.py; as piotr said, the exit status is saved in a variable called _exit_status.

这篇关于检查ipython中最后一个命令的退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆