Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find module when loading Jupyter Server Extension

Tags:

python

jupyter

I want to load a Jupyter Notebook Server Extension within a local directory:

server_ext/
|__  __init__.py
|__  extension.py

extension.py

from notebook.utils import url_path_join
from notebook.base.handlers import IPythonHandler

class HelloWorldHandler(IPythonHandler):
    def get(self):
        self.finish('Hello, world!')

def load_jupyter_server_extension(nbapp):
    """
    nbapp is istance of Jupyter.notebook.notebookapp.NotebookApp
    nbapp.web_app is isntance of tornado.web.Application - can register new tornado.web.RequestHandlers
    to extend API backend.
    """
    nbapp.log.info('My Extension Loaded')
    web_app = nbapp.web_app
    host_pattern = '.*$'
    route_pattern = url_path_join(web_app.settings['base_url'], '/hello')
    web_app.add_handlers(host_pattern, [(route_pattern, HelloWorldHandler)])

I run the following command from the directory containing server_ext:

jupyter notebook --NotebookApp.server_extensions="['server_ext.extension']"

But I get the error "No module named extension". Is there something I have to do to get Jupyter/python session to recognize the path to the module?

like image 373
ejang Avatar asked Oct 28 '15 01:10

ejang


People also ask

How do you resolve a module not found error in Jupyter Notebook?

If you're getting ModuleNotFoundError in Jupyter, first double-check that you installed the package with pip install . Triple-check you didn't misspell the package name in your import, otherwise you'll waste a lot of time reading this page.

Why is my Jupyter Notebook not launching?

Jupyter doesn't load or doesn't work in the browserTry disabling any browser extensions and/or any Jupyter extensions you have installed. Some internet security software can interfere with Jupyter. If you have security software, try turning it off temporarily, and look in the settings for a more long-term solution.

What is Jupyter server extension?

A Jupyter Server extension is typically a module or package that extends to Server's REST API/endpoints—i.e. adds extra request handlers to Server's Tornado Web Application. You can check some simple examples on the examples folder in the GitHub jupyter_server repository.


1 Answers

Figured it out-

it turns out that Jupyter Notebook's call to importlib.import_module sets package=None, which means that relative paths will not work.

As a workaround, the ~/.jupyter/jupyter_notebook_config.py script can be modified to append your local directory to the PYTHONPATH so that the module can be found.

import sys
sys.path.append("C:\\Users\\eric\\server_ext")

c.NotebookApp.server_extensions = [
    'extension'
]
like image 94
ejang Avatar answered Sep 29 '22 05:09

ejang