[英]Running Jupyter notebook test in pytest. OSError
我有一个python测试,它应该运行Jupyter笔记本文件并检查它是否有错误。 当我运行它时,它返回一个错误: OSError: [Errno 8] Exec format error: './file.ipynb'
有谁知道如何解决这一问题?
我在类似问题中发现的似乎不是我的情况。
我的代码如下:
import os
import subprocess
import tempfile
import nbformat
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
dirname, __ = os.path.split(path)
os.chdir(dirname)
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = [path, fout.name, "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=60",
"--output"]
subprocess.check_call(args)
fout.seek(0)
nb = nbformat.read(fout, nbformat.current_nbformat)
errors = [output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"]\
if output.output_type == "error"]
return nb, errors
def test_ipynb():
nb, errors = _notebook_run('./file.ipynb')
assert errors == []
你的args
错了。 你实际上是在呼唤什么
$ ./file.ipynb tempfile.ipynb nbconvert --to notebook \
--execute --ExecutePrerocessor.timeout=60 --output
这不起作用,因为file.ipynb
不是可执行文件。 你需要调用jupyter
:
$ jupyter nbconvert ./file.ipynb --output tempfile.ipynb --to notebook \
--execute --ExecutePrerocessor.timeout=60
转换为Python args
,这将是例如:
import shutil
...
jupyter_exec = shutil.which('jupyter')
if jupyter_exec is not None:
args = [jupyter_exec, "nbconvert", path,
"--output", fout.name,
"--to", "notebook",
"--execute", "--ExecutePreprocessor.timeout=60"]
subprocess.check_call(args)
else:
# jupyter not installed or not found in PATH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.