Import jupyter notebook script into a python script

I have a simple notebook script: ‘test_a.ipynb’ which contains a test_a() function.

test_a.ipynb is called in a python script: ‘test_b.py’ in the following way:

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

The ‘test_b’ is then called by another python script and executed in the following way:

from test_b import *
test_b()

The error message is:

NameError: name 'test_a' is not defined

but the following script runs fine:

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

Any help are appreciated.

I can recommend importnb which makes using notebooks-as-modules slightly more ergnomic…

3 Likes