from pathlib import Path
import sys
import nbformat
from nbclient import NotebookClient

source=Path(sys.argv[1]).resolve()
target=source.with_name(source.stem+'.executed.ipynb')
if target.exists():
    raise FileExistsError(target)
notebook=nbformat.read(source,as_version=4)
nbformat.validate(notebook)
NotebookClient(notebook,timeout=30,kernel_name='python3',allow_errors=False,
               resources={'metadata':{'path':str(source.parent)}}).execute()
with target.open('x',encoding='utf-8') as handle:
    nbformat.write(notebook,handle)
print('executed:',target.name)
