5

I have a notebook which I intend to use as a library for other notebooks. It has some functions that can ready certain types of files etc.

How can I include that notebook in a new notebook that should be able to use the functions in the library notebook?

kasperhj
  • 10,052
  • 21
  • 63
  • 106
  • https://stackoverflow.com/q/20186344/478206, https://stackoverflow.com/q/19082397/478206 and https://stackoverflow.com/q/19564625/478206 are all asking the same question. – Robie Basak Dec 05 '17 at 13:57

3 Answers3

7

You can just enter %run 'NotebookA.ipynb' in Notebook B and you should be good to go!

Mike Chan
  • 125
  • 2
  • 6
  • Just curious. Is it possible to include a relative path to the other runbook? If not, how about a full path? For some reason, %run is appending .py to the end of my filename. – Calab Oct 16 '20 at 15:28
5

googling for notebook import hook yield some examples. This is still experimental, but you are welcomed to improved it. I would suggest also using the --script flag that save an importable .py file every time you save the notebook.

Matt
  • 27,170
  • 6
  • 80
  • 74
2

With import_ipynb library using A.ipynb from B.ipynb is as simple as writing

import import_ipynb
import A

in B.ipynb.

Actually this library is just one file I've taken from the official jupyter howto and wrapped into a package installable via

pip install import-ipynb

It's also possible to write from A import foo, from A import * and so on.

axil
  • 1,558
  • 16
  • 17