modcell enables a Jupyter notebook cell as a module. It allows you to import a cell from another ipynb file by using modcell keyword.
modcell searches ipynb files registered in sys.path. modcell identifies # modcell comment in top of a cell and imports it as a module.
pip install modcell
You import the modecell in a file where you import a cell from another ipynb file.
For example in hello.ipynb:
import modcell as mods
import test_module as mod
x = mod.TestModule()
x.hello()In test_module.ipynb:
# modcell
class TestModule:
def __init__(self):
pass
def hello(self):
print('Hello from TestModule')Output:
Hello from TestModule
You can import a file from a sub-directory:
from mydir import sub_file as mod
x = mod.SubTestModule()
x.hello()sub_file under mydir:
# modcell
class SubTestModule:
def __init__(self):
pass
def hello(self):
print('Hello from SubTestModule')Output:
Hello from SubTestModule
Add the directory path to sys.path.
In sub_receiver.ipynb in parent directory:
import sys
sys.path.append('..')
import modcell as mods
import test_module as mod
x = mod.TestModule()
x.hello()Output:
Hello from TestModule