IPython-shell命令失败时引发异常
我正在使用 有没有办法让IPython在shell命令失败时引发异常?(非零出口代码)IPython-shell命令失败时引发异常,ipython,Ipython,我正在使用 有没有办法让IPython在shell命令失败时引发异常?(非零出口代码) 默认设置使它们以静默方式失败。从IPython4.0.1开始,!cmd被转换为get_ipython().system(repr(cmd))(ipython.core.inputtransformer.\u tr_system())。 在源代码中,它实际上是交互shell.system_raw(),正如inspect.getsourcefile()和inspect.getsource()可以看出的那样 它在W
默认设置使它们以静默方式失败。从
IPython
4.0.1开始,!cmd
被转换为get_ipython().system(repr(cmd))
(ipython.core.inputtransformer.\u tr_system()
)。
在源代码中,它实际上是交互shell.system_raw()
,正如inspect.getsourcefile()
和inspect.getsource()
可以看出的那样
它在Windows中委托给os.system()
,在其他操作系统中委托给subprocess.call()
。不可配置,从代码中可以看出
因此,您需要将其替换为调用子流程的内容
除了手工修补外,这也可以用手工完成。可用选项(可通过%config
magic查看)不允许将TerminalInteractiveShell
替换为另一个类,但几个TerminalIPythonApp
选项允许在启动时执行代码
不过,请仔细检查您是否真的需要此功能:查看系统的原始文件(
)的源代码可以发现它设置了\u exit\u code
变量,因此它实际上不会完全无声地失败。如果您使用代码>要执行shell命令,错误将以静默方式传递
!echo "hello" && exit 1
如果使用%%sh
单元格魔术来执行shell命令,则会出现以下错误:
%%sh
echo "hello" && exit 1
你好
---------------------------------------------------------------------------
CalledProcessError回溯(最近一次呼叫上次)
在里面
---->1获取\u ipython()。运行\u cell\u magic('sh','','echo“hello”&退出1\n')
~/anaconda/envs/altair dev/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run\u cell\u magic(self,magic\u name,line,cell)
2360,带自动内置存水弯:
2361参数=(魔法参数,单元格)
->2362结果=fn(*args,**kwargs)
2363返回结果
2364
命名脚本(行,单元格)中的~/anaconda/envs/altair dev/lib/python3.6/site-packages/IPython/core/magics/script.py
140其他:
141行=脚本
-->142返回自我。舍邦(线,单元)
143
144#编写一个基本文档字符串:
在shebang(自我、线、单元)
~/anaconda/envs/altair dev/lib/python3.6/site-packages/IPython/core/magic.py in(f,*a,**k)
185#但就这一点来说就太过分了。
186 def魔术装饰(arg):
-->187调用=λf,*a,**k:f(*a,**k)
188
189如果可调用(arg):
shebang中的~/anaconda/envs/altair dev/lib/python3.6/site-packages/IPython/core/magics/script.py(self、line、cell)
243系统标准冲洗()
244如果args.raise_错误和p.returncode=0:
-->245 raise CalledProcessError(p.returncode,cell,output=out,stderr=err)
246
247定义运行脚本(self、p、cell、to_close):
CalledProcessError:命令“b”echo“hello”&退出1\n“”返回非零退出状态1。
%%sh
echo "hello" && exit 1
hello
---------------------------------------------------------------------------
CalledProcessError Traceback (most recent call last)
<ipython-input-10-9229f76cae28> in <module>
----> 1 get_ipython().run_cell_magic('sh', '', 'echo "hello" && exit 1\n')
~/anaconda/envs/altair-dev/lib/python3.6/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2360 with self.builtin_trap:
2361 args = (magic_arg_s, cell)
-> 2362 result = fn(*args, **kwargs)
2363 return result
2364
~/anaconda/envs/altair-dev/lib/python3.6/site-packages/IPython/core/magics/script.py in named_script_magic(line, cell)
140 else:
141 line = script
--> 142 return self.shebang(line, cell)
143
144 # write a basic docstring:
<decorator-gen-110> in shebang(self, line, cell)
~/anaconda/envs/altair-dev/lib/python3.6/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
185 # but it's overkill for just that one bit of state.
186 def magic_deco(arg):
--> 187 call = lambda f, *a, **k: f(*a, **k)
188
189 if callable(arg):
~/anaconda/envs/altair-dev/lib/python3.6/site-packages/IPython/core/magics/script.py in shebang(self, line, cell)
243 sys.stderr.flush()
244 if args.raise_error and p.returncode!=0:
--> 245 raise CalledProcessError(p.returncode, cell, output=out, stderr=err)
246
247 def _run_script(self, p, cell, to_close):
CalledProcessError: Command 'b'echo "hello" && exit 1\n'' returned non-zero exit status 1.