[英]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.