【问题标题】:import jupyter notebook script into a python script将 jupyter notebook 脚本导入 python 脚本
【发布时间】:2021-11-17 22:52:31
【问题描述】:

我有一个简单的笔记本脚本:'test_a.ipynb',其中包含一个 test_a() 函数。

test_a.ipynb 在 python 脚本中调用:'test_b.py',方式如下:

 get_ipython().run_line_magic('run', 'test_a.ipynb')
 def test_b():
    test_a()

'test_b' 然后被另一个 python 脚本调用并以下列方式执行:

from test_b import *
test_b()

错误信息是:

NameError: name 'test_a' is not defined

但以下脚本运行良好:

 get_ipython().run_line_magic('run', 'test_a.ipynb')
 def test_b():
    test_a()
 #
 test_b()

感谢任何帮助。

【问题讨论】:

    标签: python-3.x jupyter-notebook ipython


    【解决方案1】:

    解决方法是将test_b.py转换成notebook脚本,在运行脚本中使用%run命令调用:

     %run test_b.ipynb 
     test_b()
    

    之后一切正常。

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2021-10-29
    • 2021-05-03
    • 2023-03-11
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    相关资源
    最近更新 更多