我正在使用air Flow(Docker(运行一个纸质命令。该脚本存储在S3上,我使用Papermill的Python客户端运行它。它最终遇到了完全无法理解的错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/ipython_genutils/ipstruct.py", line 132, in __getattr__
result = self[key]
KeyError: 'kernelspec'
我尝试查看文档但徒劳的。
我正在使用的代码是运行纸质命令是:
import time
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from mypackage.datastore import db
from mypackage.workflow.transform.jupyter_notebook import run_jupyter_notebook
dag_id = "jupyter-test-dag"
default_args = {
'owner': "aviral",
'depends_on_past': False,
'start_date': "2019-02-28T00:00:00",
'email': "aviral@some_org.com",
'email_on_failure': False,
'email_on_retry': False,
'retries': 0,
'retry_delay': timedelta(minutes=5),
'provide_context': True
}
dag = DAG(
dag_id,
catchup=False,
default_args=default_args,
schedule_interval=None,
max_active_runs=1
)
def print_context(ds, **kwargs):
print(kwargs)
print(ds)
return 'Whatever you return gets printed in the logs'
def run_python_jupyter(**kwargs):
run_jupyter_notebook(
script_location=kwargs["script_location"]
)
create_job_task = PythonOperator(
task_id="create_job",
python_callable=run_python_jupyter,
dag=dag,
op_kwargs={
"script_location": "s3://some_bucket/python3_file_write.ipynb"
}
)
globals()[dag_id] = dag
功能run_jupyter_notebook
是:
def run_jupyter_notebook(**kwargs):
"""Runs Jupyter notebook"""
script_location = kwargs.get('script_location', '')
if not script_location:
raise ValueError(
"Script location was not provided."
)
pm.execute_notebook(script_location, script_location.split(
'.ipynb')[0] + "_output" + ".ipynb")
我希望该代码也没有任何错误运行,因为我也在本地运行(不使用S3路径,使用本地文件系统路径(
jupyter将元数据添加到笔记本中。您的错误与某些元数据(在关键内核Pec下(丢失的事实有关。
kernelspec 对象的示例笔记本元数据:
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
因此,要解决错误,您需要纠正笔记本元数据以添加正确的kernelspec对象。如果要编辑笔记本JSON文档并在元数据中添加kernelspec对象,则最简单的方法是。
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
}
}
}
您的错误可能来自您使用清洁器从笔记本输出中读取的事实,例如 nbstripout python软件包。如果是这种情况,请在文档之后注意更改NBSTRIPOUT设置。