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.Python Installation
Before setting up environments, ensure Python 3.8 or higher is installed:- macOS
- Windows
- Linux
Local Environment Setup
- UV (Recommended)
- venv (Standard Library)
- Conda
UV is a fast Python package manager written in Rust that handles virtual environments and dependencies.
Install UV
- macOS / Linux
- Windows
- Homebrew
Create a Project with UV
Register the Kernel with Jupyter
Project Structure
After setup, your project should look like:UV automatically creates and manages the
.venv directory. You don’t need to activate it manually—use uv run to execute commands in the environment.Adding Packages Later
VS Code Configuration
Select Your Python Interpreter
- Open VS Code Command Palette (
Cmd+Shift+P/Ctrl+Shift+P) - Run “Python: Select Interpreter”
- Choose your virtual environment (
.venvor 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:
- Click the kernel selector in the top-right corner
- Select your registered kernel (e.g., “Python (my-analysis)”)
- The kernel should start and show as connected
Recommended VS Code Extensions
Install these extensions for the best Jupyter experience: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:Connect VS Code to Remote Server
- Open VS Code Command Palette (
Cmd+Shift+P/Ctrl+Shift+P) - Run “Jupyter: Specify Jupyter Server for Connections”
- Select “Existing”
- Enter the server URL:
http://localhost:8888/?token=abc123... - Your remote kernels should now appear in the kernel picker
JupyterHub Connection
For enterprise JupyterHub deployments:- Get your JupyterHub URL (e.g.,
https://hub.company.com) - Follow the same connection steps as above
- Authenticate with your JupyterHub credentials
- Select from available hub-managed kernels
Best Practices
One environment per project
One environment per project
Avoid using a single global environment. Create dedicated environments for each project to prevent dependency conflicts and ensure reproducibility.
Pin your dependencies
Pin your dependencies
Always pin package versions for reproducible environments:UV (pyproject.toml):pip (requirements.txt):UV automatically creates a
uv.lock file for exact reproducibility.Use .gitignore for virtual environments
Use .gitignore for virtual environments
Never commit virtual environments to version control:Cross-platform note: Use forward slashes (
/) in .gitignore even on Windows—Git handles the conversion automatically.Document your setup
Document your setup
Include setup instructions in your project README:
Keep environments minimal
Keep environments minimal
Only install packages you actually need. Large environments are slow to create and can have dependency conflicts.
Use pyproject.toml for modern projects
Use pyproject.toml for modern projects
pyproject.toml is the modern standard for Python project configuration:Troubleshooting
Kernel not appearing in VS Code
Kernel not appearing in VS Code
- Ensure
ipykernelis installed in your environment - Run the kernel install command:
- Restart VS Code
- Check “Jupyter: Select Interpreter to Start Jupyter Server” in Command Palette
Package import errors in notebook
Package import errors in notebook
The notebook might be using a different kernel than expected:
- Check the active kernel in the top-right of the notebook
- Ensure it matches your environment with the installed packages
- Restart the kernel after installing new packages
UV command not found
UV command not found
Add UV to your PATH:Then restart your terminal or run
- macOS / Linux
- Windows
source ~/.zshrc.Remote kernel disconnects frequently
Remote kernel disconnects frequently
- Check network stability
- Increase Jupyter timeout settings on the server:
- Consider using VS Code Remote - SSH extension for a more stable connection
Permission denied when installing kernel
Permission denied when installing kernel
Use the Or use
--user flag to install kernels in your user directory:--prefix for a specific location:'python' is not recognized (Windows)
'python' is not recognized (Windows)
If you see this error on Windows:
- Ensure Python is installed:
winget install Python.Python.3.11 - Check Add Python to PATH was selected during installation
- Manually add Python to PATH:
- Open System Properties → Environment 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
- Restart your terminal
Python command differences (Linux/Mac vs Windows)
Python command differences (Linux/Mac vs Windows)
Linux/macOS:
- Use
python3andpip3to avoid Python 2.x conflicts - Example:
python3 -m venv .venv
- Use
python(Python 3 is the default) - Or use
pylauncher:py -3.11 -m venv .venv
Quick Reference
UV Commands
| Command | Description |
|---|---|
uv init | Initialize new project |
uv add <package> | Add dependency |
uv remove <package> | Remove dependency |
uv sync | Sync environment with lock file |
uv run <command> | Run command in environment |
uv lock | Update lock file |
venv Commands
| Command | Description |
|---|---|
python -m venv .venv | Create environment |
source .venv/bin/activate | Activate (macOS/Linux) |
.venv\Scripts\activate | Activate (Windows) |
deactivate | Deactivate environment |
pip freeze > requirements.txt | Export dependencies |
Kernel Commands
| Command | Description |
|---|---|
python -m ipykernel install --user --name <name> | Register kernel |
jupyter kernelspec list | List installed kernels |
jupyter kernelspec uninstall <name> | Remove kernel |