Importing own functions

How can I use a function from one notebook in another?

I need to use a class from 02_functions.ipynb in 03_nodes.ipynb.
My lib_name is pct.
These create pct/functions.py and pct/nodes.py

The class in functions.py is called Integration and in 03_nodes.ipynb I have

from pct.functions import Integration

But that gives the error,


ImportError Traceback (most recent call last)
<ipython-input-92-4d9382de84fb> in <module>
----> 1 from pct.functions import Integration

ImportError: cannot import name ‘Integration’ from ‘pct.functions’ (unknown location)

What am I doing wrong?

After a great deal of digging around I found a workaround in another post.

That is, for windows, to have a cell with,

import sys
sys.path.append("…")

I also had to restart the kernel.

Perhaps, I am missing something, but I would’ve thought that using other modules from within your own library would be fundamental need. So, it seems surprising that there is not a standard way of doing it, or the workaround is not documented. It would save people a lot of time. :slight_smile:

1 Like