Skip to main content

Overview

A properly configured Python environment is essential for working with Sphinx and Jupyter notebooks. This guide covers setting up local environments using modern tools like UV and traditional venv, as well as connecting to remote Jupyter servers.
Recommendation: Use UV for new projects. It’s significantly faster than pip and provides better dependency resolution.

Python Installation

Before setting up environments, ensure Python 3.8 or higher is installed:
# Using Homebrew (recommended)
brew install [email protected]

# Or download from python.org
# https://www.python.org/downloads/

Local Environment Setup


VS Code Configuration

Select Your Python Interpreter

  1. Open VS Code Command Palette (Cmd+Shift+P / Ctrl+Shift+P)
  2. Run “Python: Select Interpreter”
  3. Choose your virtual environment (.venv or conda environment)
VS Code should show your environment in the status bar, e.g., Python 3.11.0 ('.venv': venv)

Select Jupyter Kernel

When you open a .ipynb file:
  1. Click the kernel selector in the top-right corner
  2. Select your registered kernel (e.g., “Python (my-analysis)”)
  3. The kernel should start and show as connected
If your kernel doesn’t appear, ensure you ran the ipykernel install command and restart VS Code.
Install these extensions for the best Jupyter experience:
ExtensionPurpose
PythonPython language support
JupyterNotebook support (required for Sphinx)
PylanceFast IntelliSense and type checking

Remote Jupyter Server

Connect to a Jupyter server running on a remote machine, cloud instance, or HPC cluster.

Start a Remote Jupyter Server

On the remote machine:
# Install Jupyter
pip install jupyter

# Start server with remote access
jupyter notebook --no-browser --ip=0.0.0.0 --port=8888
The server will output a URL with a token:
http://127.0.0.1:8888/?token=abc123...
Security: Never expose Jupyter servers to the public internet without authentication. Use SSH tunneling for secure connections.

Connect VS Code to Remote Server

  1. Open VS Code Command Palette (Cmd+Shift+P / Ctrl+Shift+P)
  2. Run “Jupyter: Specify Jupyter Server for Connections”
  3. Select “Existing”
  4. Enter the server URL: http://localhost:8888/?token=abc123...
  5. Your remote kernels should now appear in the kernel picker

JupyterHub Connection

For enterprise JupyterHub deployments:
  1. Get your JupyterHub URL (e.g., https://hub.company.com)
  2. Follow the same connection steps as above
  3. Authenticate with your JupyterHub credentials
  4. Select from available hub-managed kernels
Some JupyterHub deployments require an API token. Generate one from the JupyterHub web interface under Token settings.

Best Practices

Avoid using a single global environment. Create dedicated environments for each project to prevent dependency conflicts and ensure reproducibility.
# Good: Project-specific environment
my-project/.venv/

# Bad: Global environment shared across projects
~/.global-venv/
Always pin package versions for reproducible environments:UV (pyproject.toml):
[project]
dependencies = [
    "pandas>=2.0.0,<3.0.0",
    "numpy>=1.24.0,<2.0.0",
]
pip (requirements.txt):
pandas==2.1.4
numpy==1.26.3
UV automatically creates a uv.lock file for exact reproducibility.
Never commit virtual environments to version control:
# .gitignore
.venv/
venv/
env/
__pycache__/
*.pyc
*.pyo
.ipynb_checkpoints/
*.egg-info/
dist/
build/
Cross-platform note: Use forward slashes (/) in .gitignore even on Windows—Git handles the conversion automatically.
Include setup instructions in your project README:
## Setup

1. Install UV: `curl -LsSf https://astral.sh/uv/install.sh | sh`
2. Install dependencies: `uv sync`
3. Register kernel: `uv run python -m ipykernel install --user --name my-project`
4. Open VS Code and select the kernel
Only install packages you actually need. Large environments are slow to create and can have dependency conflicts.
# Good: Install what you need
uv add pandas matplotlib

# Avoid: Installing everything "just in case"
uv add pandas numpy scipy matplotlib seaborn plotly bokeh altair ...
pyproject.toml is the modern standard for Python project configuration:
[project]
name = "my-analysis"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = [
    "pandas>=2.0.0",
    "matplotlib>=3.7.0",
    "ipykernel>=6.25.0",
]

[project.optional-dependencies]
dev = ["pytest", "black", "ruff"]

Troubleshooting

  1. Ensure ipykernel is installed in your environment
  2. Run the kernel install command:
    python -m ipykernel install --user --name my-env --display-name "Python (my-env)"
    
  3. Restart VS Code
  4. Check “Jupyter: Select Interpreter to Start Jupyter Server” in Command Palette
The notebook might be using a different kernel than expected:
  1. Check the active kernel in the top-right of the notebook
  2. Ensure it matches your environment with the installed packages
  3. Restart the kernel after installing new packages
Add UV to your PATH:
# Add to ~/.zshrc or ~/.bashrc
export PATH="$HOME/.cargo/bin:$PATH"
Then restart your terminal or run source ~/.zshrc.
  • Check network stability
  • Increase Jupyter timeout settings on the server:
    jupyter notebook --NotebookApp.shutdown_no_activity_timeout=3600
    
  • Consider using VS Code Remote - SSH extension for a more stable connection
Use the --user flag to install kernels in your user directory:
python -m ipykernel install --user --name my-env
Or use --prefix for a specific location:
python -m ipykernel install --prefix=/path/to/kernels --name my-env
If you see this error on Windows:
  1. Ensure Python is installed: winget install Python.Python.3.11
  2. Check Add Python to PATH was selected during installation
  3. Manually add Python to PATH:
    • Open System PropertiesEnvironment Variables
    • Under User variables, edit Path
    • Add: C:\Users\YourUsername\AppData\Local\Programs\Python\Python311
    • Add: C:\Users\YourUsername\AppData\Local\Programs\Python\Python311\Scripts
  4. Restart your terminal
Alternatively, use the Python Launcher:
py -m venv .venv
py -m pip install package-name
Linux/macOS:
  • Use python3 and pip3 to avoid Python 2.x conflicts
  • Example: python3 -m venv .venv
Windows:
  • Use python (Python 3 is the default)
  • Or use py launcher: py -3.11 -m venv .venv
Cross-platform script:
# Works on all platforms
python -m venv .venv  # or python3 on Linux/macOS

Quick Reference

UV Commands

CommandDescription
uv initInitialize new project
uv add <package>Add dependency
uv remove <package>Remove dependency
uv syncSync environment with lock file
uv run <command>Run command in environment
uv lockUpdate lock file

venv Commands

CommandDescription
python -m venv .venvCreate environment
source .venv/bin/activateActivate (macOS/Linux)
.venv\Scripts\activateActivate (Windows)
deactivateDeactivate environment
pip freeze > requirements.txtExport dependencies

Kernel Commands

CommandDescription
python -m ipykernel install --user --name <name>Register kernel
jupyter kernelspec listList installed kernels
jupyter kernelspec uninstall <name>Remove kernel