informative error on missing dependencies for singleuser server

- defer jupyter_core import that caused earlier, less informative ImportError
- point to `pip install jupyterhub[singleuser]` in the error
- use `raise from` so original import error is still reported
This commit is contained in:
Min RK
2024-10-21 09:10:33 +02:00
parent b2d9f93601
commit 6da70e9960
2 changed files with 26 additions and 4 deletions

View File

@@ -63,9 +63,29 @@ if _as_extension:
f"Cannot use JUPYTERHUB_SINGLEUSER_EXTENSION={_extension_env} with JUPYTERHUB_SINGLEUSER_APP={_app_env}." f"Cannot use JUPYTERHUB_SINGLEUSER_EXTENSION={_extension_env} with JUPYTERHUB_SINGLEUSER_APP={_app_env}."
" Please pick one or the other." " Please pick one or the other."
) )
try:
from .extension import main from .extension import main
except ImportError as e:
# raise from to preserve original import error
raise ImportError(
"Failed to import JupyterHub singleuser extension."
" Make sure to install dependencies for your single-user server, e.g.\n"
" pip install 'jupyterhub[singleuser]'"
) from e
else: else:
try:
from .app import SingleUserNotebookApp, main from .app import SingleUserNotebookApp, main
except ImportError as e:
# raise from to preserve original import error
if _app_env:
_app_env_log = f"JUPYTERHUB_SINGLEUSER_APP={_app_env}"
else:
_app_env_log = "default single-user server"
raise ImportError(
f"Failed to import {_app_env_log}."
" Make sure to install dependencies for your single-user server, e.g.\n"
" pip install 'jupyterhub[singleuser]'"
) from e
# backward-compatibility # backward-compatibility
if SingleUserNotebookApp is not None: if SingleUserNotebookApp is not None:

View File

@@ -22,8 +22,6 @@ rather than keeing these monkey patches around.
import os import os
from pathlib import Path from pathlib import Path
from jupyter_core import paths
def _is_relative_to(path, prefix): def _is_relative_to(path, prefix):
""" """
@@ -68,6 +66,10 @@ def _disable_user_config(serverapp):
2. Search paths for extensions, etc. 2. Search paths for extensions, etc.
3. import path 3. import path
""" """
# delayed import to avoid triggering early ImportError
# with unmet dependencies
from jupyter_core import paths
original_jupyter_path = paths.jupyter_path() original_jupyter_path = paths.jupyter_path()
jupyter_path_without_home = list(_exclude_home(original_jupyter_path)) jupyter_path_without_home = list(_exclude_home(original_jupyter_path))