Cannot find module while loading Jupyter Server Extension

I want to download the Jupyter Notebook Server Extension to 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 with extension." Is there something I need to do for the Jupyter / python session to recognize the module path?

+5
source share
1 answer

Figured it out -

it turns out that calling the Jupyter Notebook for importlib.import_module sets package = None, which means that relative paths will not work.

As a workaround, you can modify the ~ / .jupyter / jupyter_notebook_config.py script file to add a local directory to PYTHONPATH so that the module can be found.

 import sys sys.path.append("C:\\Users\\eric\\server_ext") c.NotebookApp.server_extensions = [ 'extension' ] 
+6
source

Source: https://habr.com/ru/post/1234651/


All Articles