1

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

我找到了检测退出代码的方法,但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 故障

4

1 回答 1

0

Jupyter 和 papermill 组合的部分美妙之处在于错误保存在笔记本中以及它们发生的位置。这在一次运行多个 ETL 作业时非常有用。如果您还想访问笔记本之外的错误,我会将特定错误errors.txttry catch. 然后,您的脚本可以通过读取错误(如果有)来完成。

本文讨论了各种错误记录技术。

于 2019-10-05T01:23:26.063 回答