Skip to content

Jupyter Notebooks

Jupyter Notebooks are essential for interactive deep learning development and experimentation.

# Activate your environment
source ml-env/bin/activate

# Install Jupyter
pip install jupyter notebook jupyterlab

# Launch Jupyter
jupyter notebook
# or
jupyter lab

To use your Python environment in Jupyter notebooks:

# Activate your environment
source ml-env/bin/activate

# Install ipykernel
pip install ipykernel

# Add environment as kernel
python -m ipykernel install --user --name ml-env --display-name "Python 3.11 (ML)"

# Verify it's added
jupyter kernelspec list
jupyter kernelspec list

Output example:

Available kernels:
  ml-env       /home/user/.local/share/jupyter/kernels/ml-env
  python3      /usr/share/jupyter/kernels/python3
  pytorch-env  /home/user/.local/share/jupyter/kernels/pytorch-env
# Remove a kernel
jupyter kernelspec uninstall unwanted-kernel

# Example
jupyter kernelspec uninstall old-tf-env
# Remove old kernel
jupyter kernelspec uninstall old-name

# Re-add with new display name
python -m ipykernel install --user --name myenv --display-name "New Display Name"
%%time
# Your code here
model.train()

Shows total execution time for the cell.

# Check GPU availability
import torch
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"GPU: {torch.cuda.get_device_name(0)}")
print(f"GPU count: {torch.cuda.device_count()}")

# Monitor GPU memory
print(f"Memory allocated: {torch.cuda.memory_allocated(0) / 1e9:.2f} GB")
print(f"Memory reserved: {torch.cuda.memory_reserved(0) / 1e9:.2f} GB")
# Add to first cell - automatically reload changed modules
%load_ext autoreload
%autoreload 2
# Display all outputs (not just last one)
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

Issue: Empty Browser Page on Jupyter Start

Section titled “Issue: Empty Browser Page on Jupyter Start”

Problem: Jupyter launches but shows blank page

Solution:

# Hard refresh browser
Ctrl + Shift + R  # Windows/Linux
Cmd + Shift + R   # Mac

# Or clear browser cache and restart Jupyter
jupyter notebook --no-browser
# Then manually open the URL shown

Issue: Kernel Not Found / “Dead Kernel”

Section titled “Issue: Kernel Not Found / “Dead Kernel””

Problem: Notebook can’t connect to kernel

Solution:

# 1. Check if kernel exists
jupyter kernelspec list

# 2. Reinstall ipykernel in environment
source ml-env/bin/activate
pip install --force-reinstall ipykernel
python -m ipykernel install --user --name ml-env --display-name "Python (ML)"

# 3. Restart Jupyter

Problem: Notebook uses system Python instead of your environment

Solution:

# Verify kernel Python path
jupyter kernelspec list
cat ~/.local/share/jupyter/kernels/ml-env/kernel.json

# Should point to your environment, e.g.:
# "/home/user/ml-env/bin/python"

# If wrong, remove and re-add kernel
jupyter kernelspec uninstall ml-env
source ml-env/bin/activate
python -m ipykernel install --user --name ml-env

Issue: ImportError for Packages Installed in Environment

Section titled “Issue: ImportError for Packages Installed in Environment”

Problem: Package installed but import fails in notebook

Check you’re using the right kernel:

# In notebook cell
import sys
print(sys.executable)
# Should show: /home/user/ml-env/bin/python

# If wrong, change kernel:
# Kernel → Change Kernel → Select correct environment
# Rebuild JupyterLab
jupyter lab build

# Or clear cache and rebuild
jupyter lab clean
jupyter lab build

Error:

AttributeError: partially initialized module 'charset_normalizer'
has no attribute 'md__mypyc' (most likely due to a circular import)

Fix:

pip install -U --force-reinstall charset-normalizer
  • Simpler interface
  • Lighter weight
  • Better for single-notebook work
jupyter notebook
  • Multi-tab interface
  • Built-in terminal
  • File browser
  • Extension system
jupyter lab

Recommendation: Use JupyterLab for most work, Notebook for simplicity.

On remote machine:

jupyter notebook --no-browser --port=8888

On local machine:

# Create SSH tunnel
ssh -L 8888:localhost:8888 user@remote-server

# Open browser to http://localhost:8888

For team environments, consider JupyterHub:

  • Multi-user support
  • Resource management
  • Authentication

See JupyterHub docs for setup.

# Install extension manager
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

# Enable useful extensions
jupyter nbextension enable execute_time/ExecuteTime
jupyter nbextension enable toc2/main  # Table of contents
jupyter nbextension enable varInspector/main  # Variable inspector
jupyter nbextension enable codefolding/main  # Code folding
# GPU monitoring dashboard
pip install jupyterlab-nvdashboard
jupyter labextension install jupyterlab-nvdashboard

# Git integration
pip install jupyterlab-git
# Cell 1: Imports and setup
import torch
import numpy as np
%load_ext autoreload
%autoreload 2

# Cell 2: Configuration
BATCH_SIZE = 32
LEARNING_RATE = 1e-3
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'

# Cell 3+: Your work
...
# Convert notebook to Python script
jupyter nbconvert --to script notebook.ipynb

# Generates notebook.py
# Add to .gitignore
*.ipynb_checkpoints/
.ipynb_checkpoints

# Clean output before committing
jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace notebook.ipynb