I'm trying to create a project with the following structure:
my_file.ipynb
my_package_directory > __init__.py test.py
Within test.py
lets say I have a very simple class:
class Test:
def __init__(self, name):
self.name = name
from with in a code cell I try to insatiate the class and print out the defined variable:
from my_package_directory.test import Test
test = Test('bob')
print(test.name)
If I try to run the cell I get an error:
ImportError: cannot import name 'Test' from 'my_package_directory.test'
Is there a certain way to do this in a Jupyter notebook?
Thank you.
CodePudding user response:
Probably the root of your files is not in the python module search path. You can check your module search path with:
import sys
print(sys.path)
If you append the location of your notebook and your package to that path, it should be importable:
sys.path.append('/path/to/where/jupyter/notebook/resides')
from my_package_directory.test import Test # should work
Note that you have to restart the kernel whenever you change the implementation of your module - This can be circumvented with some jupyter notebook magic.