Suppose your project has the following structure and you want to do imports in the notebook.ipynb
:
/app
/mypackage
mymodule.py
/notebooks
notebook.ipynb
If you are running Jupyter inside a docker container without any virtualenv it might be useful to create Jupyter (ipython) config in your project folder:
/app
/profile_default
ipython_config.py
Content of ipython_config.py
:
c.InteractiveShellApp.exec_lines = [
'import sys; sys.path.append("/app")'
]
Open the notebook and check it out:
print(sys.path)
['', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages/IPython/extensions', '/root/.ipython', '/app']
Now you can do imports in your notebook without any sys.path
appending in the cells:
from mypackage.mymodule import myfunc