Reputation: 523
I have a python test, which should run Jupyter notebook file and check it for errors.
When I run it, it returns an error: OSError: [Errno 8] Exec format error: './file.ipynb'
Does anyone know how to fix this?
What I've found in similar questions doesn't seem to be my case.
My code is below:
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 == []
Upvotes: 4
Views: 1207
Reputation: 66231
Your args
are wrong. What you are essentially calling is
$ ./file.ipynb tempfile.ipynb nbconvert --to notebook \
--execute --ExecutePrerocessor.timeout=60 --output
This doesn't work because file.ipynb
is not an executable. You need to invoke jupyter
instead:
$ jupyter nbconvert ./file.ipynb --output tempfile.ipynb --to notebook \
--execute --ExecutePrerocessor.timeout=60
Translated to Python args
, this would be for example:
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
Upvotes: 4