Skip to content

Commit 2018067

Browse files
committed
chore: update DEEPNOTE_KERNEL_IMPLEMENTATION.md to better explain install
1 parent 12fd559 commit 2018067

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

DEEPNOTE_KERNEL_IMPLEMENTATION.md

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@ This implementation adds automatic kernel selection and startup for `.deepnote`
2929
- **Upgrades pip** in the venv to the latest version (ensures latest pip features and fixes)
3030
- Checks if `deepnote-toolkit` is installed in the venv
3131
- Installs the toolkit and `ipykernel` from the hardcoded S3 wheel URL
32-
- **Registers a kernel spec** using `ipykernel install --user --name deepnote-venv-<hash>` that points to the venv's Python interpreter
33-
- This ensures packages installed via `pip` are available to the kernel
32+
- Installs ipykernel in the venv to enable kernel functionality
3433
- Outputs installation progress to the output channel
3534
- Verifies successful installation
3635
- Reuses existing venvs for the same `.deepnote` file
3736

3837
**Key Methods:**
3938

4039
- `getVenvInterpreter(deepnoteFileUri)`: Gets the venv Python interpreter for a specific file
41-
- `ensureInstalled(interpreter, deepnoteFileUri)`: Creates venv, installs toolkit and ipykernel, and registers kernel spec
42-
- `getVenvHash(deepnoteFileUri)`: Creates a unique hash for both kernel naming and venv directory paths
40+
- `ensureInstalled(interpreter, deepnoteFileUri)`: Creates venv, installs toolkit and ipykernel
41+
- `getVenvHash(deepnoteFileUri)`: Creates a unique hash for venv directory paths
4342
- `getDisplayName(deepnoteFileUri)`: Gets a friendly display name for the kernel
4443

4544
#### 3. **Deepnote Server Starter** (`src/kernels/deepnote/deepnoteServerStarter.node.ts`)
@@ -81,9 +80,9 @@ This implementation adds automatic kernel selection and startup for `.deepnote`
8180
- Activation service that listens for notebook open events and controller selection changes
8281
- Automatically selects Deepnote kernel for `.deepnote` files
8382
- Queries the Deepnote server for available kernel specs
84-
- **Prefers the venv kernel spec** (`deepnote-venv-<hash>`) that was registered by the installer and uses the venv's Python interpreter
85-
- This ensures the kernel uses the same environment where packages are installed
86-
- Falls back to other Python kernel specs if venv kernel not found
83+
- **Selects a server-native Python kernel spec** (e.g., `python3-venv` or any available Python kernel)
84+
- The Deepnote server is started with the venv's Python interpreter, ensuring the kernel uses the venv environment
85+
- Environment variables (`PATH`, `VIRTUAL_ENV`) are configured so the server and kernel use the venv's Python
8786
- Registers the server with the server provider
8887
- Creates kernel connection metadata
8988
- Registers the controller with VSCode
@@ -134,8 +133,6 @@ Upgrade pip to latest version in venv
134133
135134
pip install deepnote-toolkit[server] and ipykernel in venv
136135
137-
Register kernel spec pointing to venv's Python
138-
139136
Verify installation
140137
141138
DeepnoteServerStarter.getOrStartServer(venv, fileUri)
@@ -152,7 +149,7 @@ Register server with DeepnoteServerProvider
152149
153150
Query server for available kernel specs
154151
155-
Select venv kernel spec (deepnote-venv-<hash>) or fall back to other Python kernel
152+
Select any available Python kernel spec (e.g., python3-venv)
156153
157154
Create DeepnoteKernelConnectionMetadata with server kernel spec
158155
@@ -172,7 +169,7 @@ User runs cell → Executes on Deepnote kernel
172169
- **Notebook Type**: `deepnote`
173170
- **Venv Location**: `~/.vscode/extensions/storage/deepnote-venvs/<hashed-path>/` (e.g., `venv_a1b2c3d4`)
174171
- **Server Provider ID**: `deepnote-server`
175-
- **Kernel Spec Name**: `deepnote-venv-<file-path-hash>` (registered via ipykernel to point to venv Python)
172+
- **Kernel Spec**: Uses server-native Python kernel specs (e.g., `python3-venv`)
176173
- **Kernel Display Name**: `Deepnote (<notebook-filename>)`
177174

178175
## Usage
@@ -322,13 +319,15 @@ The implementation uses VSCode's Jupyter server provider API to properly integra
322319

323320
### Kernel Spec Resolution
324321

325-
The implementation uses a hybrid approach:
322+
The implementation uses server-native kernel specs with venv isolation:
326323

327-
1. **Registers per-venv kernel specs**: The installer registers kernel specs using `ipykernel install --user --name deepnote-venv-<hash>` that point to each venv's Python interpreter
328-
2. **Queries server for available specs**: Connects to the running Deepnote server using `JupyterLabHelper` and queries available kernel specs via `getKernelSpecs()`
329-
3. **Prefers venv kernel specs**: Looks for the registered venv kernel spec (`deepnote-venv-<hash>`) first
330-
4. **Falls back gracefully**: Falls back to other Python kernel specs (like `python3-venv`) if the venv kernel spec is not found
331-
5. **Uses server-compatible specs**: This ensures compatibility with the Deepnote server's kernel configuration while maintaining venv isolation
324+
1. **Queries server for available specs**: Connects to the running Deepnote server using `JupyterLabHelper` and queries available kernel specs via `getKernelSpecs()`
325+
2. **Selects any Python kernel spec**: Selects any available Python kernel spec (e.g., `python3-venv`, `python3`, or the first available kernel)
326+
3. **Venv isolation via server environment**: The Deepnote server is started with the venv's Python interpreter and environment variables configured:
327+
- `PATH` is prepended with the venv's `bin/` directory
328+
- `VIRTUAL_ENV` points to the venv path
329+
- `PYTHONHOME` is removed to avoid conflicts
330+
4. **Result**: The kernel uses the venv's Python environment even though it's using a server-native kernel spec, ensuring packages installed via `pip` are available
332331

333332
### Virtual Environment Path Handling
334333

@@ -408,25 +407,20 @@ These changes ensure that Deepnote notebooks can execute cells reliably by:
408407
2. But the kernel was using a different Python interpreter (system Python or different environment)
409408
3. So `pip install` went to one environment, but imports came from another
410409

411-
**Root Cause**: The kernel was using the venv's Python (correct), but shell commands (`!pip install`) were using the system Python or pyenv (wrong) because they inherit the shell's PATH environment variable.
412-
413-
**Solution**: Two-part fix:
414-
415-
1. **Kernel spec registration** (ensures kernel uses venv Python):
410+
**Root Cause**: Both the kernel and shell commands (`!pip install`) need to use the venv's Python interpreter, but by default they would use whatever Python is in the system PATH.
416411

417-
- Install `ipykernel` in the venv along with deepnote-toolkit
418-
- Use `python -m ipykernel install --user --name deepnote-venv-<hash>` to register a kernel spec that points to the venv's Python interpreter
419-
- In the kernel selection logic, prefer the venv kernel spec (`deepnote-venv-<hash>`) when querying the server for available specs
420-
421-
2. **Environment variable configuration** (ensures shell commands use venv Python):
412+
**Solution**: Configure the Deepnote server's environment to use the venv:
422413

423414
- When starting the Jupyter server, set environment variables:
424415
- Prepend venv's `bin/` directory to `PATH`
425416
- Set `VIRTUAL_ENV` to point to the venv
426417
- Remove `PYTHONHOME` (can interfere with venv)
427-
- This ensures `!pip install` and other shell commands use the venv's Python
418+
- Install `ipykernel` in the venv along with deepnote-toolkit
419+
- Use any server-native Python kernel spec (e.g., `python3-venv`)
420+
- The kernel inherits the server's environment, so it uses the venv's Python
421+
- Shell commands (`!pip install`) also inherit the server's environment, so they use the venv's Python
428422

429-
**Result**: Both the kernel and shell commands now use the same Python environment (the venv), so packages installed via `!pip install` or `%pip install` are immediately available for import.
423+
**Result**: Both the kernel and shell commands use the same Python environment (the venv), so packages installed via `!pip install` or `%pip install` are immediately available for import.
430424

431425
### Issue 6: "Venv created with outdated pip version"
432426

0 commit comments

Comments
 (0)