繁体   English   中英

在bash脚本中处理python错误,没有错误代码

[英]Handling python error in bash script with no error codes

我正在运行python cli工具Papermill。 我正在运行jupyter笔记本,并且希望能够在笔记本运行时检测assertionErrors。 当我从bash脚本运行papermill时,即使笔记本中存在python错误,我也总是得到0退出代码。

我发现了检测退出代码有效的方法,但是python错误并未抛出任何代码

例如:在我的nbTest.sh中,我正在运行笔记本并寻找错误

papermill python_notebooks/testing/$d python_notebooks/testing/results/$d.results.ipynb || True
if [ $? -eq 0 ]
then
  echo "Successfully executed script"
else
  # Redirect stdout from echo command to stderr.
  echo "Script exited with error." 
fi
done

作为输出,我得到:

.
.
.
Input Notebook:  python_notebooks/testing/paperTest.ipynb
Output Notebook: python_notebooks/testing/results/paperTest.ipynb.results.ipynb
 83%|████████▎ | 5/6 [00:00<00:00,  1.97it/s]
Traceback (most recent call last):
 ...
---------------------------------------------------------------------------
Exception encountered at "In [5]":
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-5-d941c6663321> in <module>()
----> 1 assert passed == False

AssertionError: 
Successfully executed script

我希望退出代码为1,但无法处理python故障

Jupyter和Papermill组合的部分优点在于,错误会保留在笔记本中以及错误发生的位置。 一次运行多个ETL作业时,这非常有用。 如果您还想访问笔记本之外的错误,我可以将特定的错误从try catcherrors.txt文件中。 然后,可以通过读取错误(如果有)来完成脚本。

在这篇文章中讨论了各种错误记录技术。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM