Dev Container — Python
Overview
Configure a dev container for Python development using the official Python feature or base image. Supports version selection, tool installation via pipx, virtual environments, and JupyterLab.
Python Feature
{
"features": {
"ghcr.io/devcontainers/features/python:1": {
"version": "3.12"
}
}
}
Feature Options
| Option |
Default |
Description |
version |
latest |
Python version (3.12, 3.11, latest, os-provided, none) |
installTools |
true |
Install tools listed in toolsToInstall via pipx |
toolsToInstall |
flake8,autopep8,black,yapf,mypy,pydocstyle,pycodestyle,bandit,pipenv,virtualenv,pytest,pylint |
Comma-separated tools |
installJupyterlab |
false |
Install JupyterLab |
optimize |
false |
Compile Python with optimizations (slower build) |
installPath |
/usr/local/python |
Installation directory |
Full Example
{
"name": "Python 3.12 Development",
"image": "mcr.microsoft.com/devcontainers/python:3.12",
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {}
},
"forwardPorts": [8000],
"portsAttributes": {
"8000": { "label": "App", "onAutoForward": "notify" },
"8888": { "label": "Jupyter", "onAutoForward": "notify" }
},
"postCreateCommand": "pip install -r requirements.txt",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.black-formatter",
"charliermarsh.ruff"
],
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python",
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true
}
}
}
}
}
Using the Base Image vs Feature
| Approach |
When to Use |
"image": "mcr.microsoft.com/devcontainers/python:3.12" |
Python-only projects |
Base image + ghcr.io/devcontainers/features/python:1 |
Multi-language projects |
Virtual Environments
{
"postCreateCommand": "python -m venv .venv && . .venv/bin/activate && pip install -r requirements.txt",
"containerEnv": {
"VIRTUAL_ENV": "/workspace/.venv",
"PATH": "/workspace/.venv/bin:${containerEnv:PATH}"
}
}
JupyterLab
{
"features": {
"ghcr.io/devcontainers/features/python:1": {
"version": "3.12",
"installJupyterlab": true
}
},
"forwardPorts": [8888],
"postStartCommand": "jupyter lab --ip=0.0.0.0 --no-browser --NotebookApp.token=''"
}
Poetry / uv / PDM
For modern package managers, install them in postCreateCommand:
{
"postCreateCommand": "pip install uv && uv sync",
"containerEnv": {
"UV_LINK_MODE": "copy"
}
}
Best Practices
- Use the dedicated
mcr.microsoft.com/devcontainers/python image for Python-only projects.
- Pin the Python version to a minor release (
3.12, 3.11) for reproducibility.
- Prefer Ruff over separate flake8/black installs — it replaces multiple tools with a single fast one.
- Use
postCreateCommand for pip install so dependencies are cached in Codespaces prebuilds.
- Set
python.defaultInterpreterPath in VS Code settings to avoid interpreter selection prompts.
- For data science projects, enable
installJupyterlab in the feature and forward port 8888.
- Use
containerEnv to configure VIRTUAL_ENV if your project expects a venv.
1---2name: python-23description: |4 Use when configuring dev containers for Python projects. Covers the Python feature, virtual environments, tool installation, JupyterLab, and linter/formatter setup.5 USE FOR: Python dev container setup, Python feature configuration, virtual environments, JupyterLab, pip/uv/poetry tooling6 DO NOT USE FOR: .NET dev containers (use dotnet), TypeScript dev containers (use typescript), general devcontainer.json (use devcontainer)7license: MIT8metadata:9 displayName: "Dev Container — Python"10 author: "Tyler-R-Kendrick"11compatibility: claude, copilot, cursor12references:13 - title: "Dev Containers Specification"14 url: "https://containers.dev/"15 - title: "Python Official Documentation"16 url: "https://docs.python.org/3/"17---1819# Dev Container — Python2021## Overview22Configure a dev container for Python development using the official Python feature or base image. Supports version selection, tool installation via pipx, virtual environments, and JupyterLab.2324## Python Feature25```jsonc26{27 "features": {28 "ghcr.io/devcontainers/features/python:1": {29 "version": "3.12"30 }31 }32}33```3435### Feature Options36| Option | Default | Description |37|--------|---------|-------------|38| `version` | `latest` | Python version (`3.12`, `3.11`, `latest`, `os-provided`, `none`) |39| `installTools` | `true` | Install tools listed in `toolsToInstall` via pipx |40| `toolsToInstall` | `flake8,autopep8,black,yapf,mypy,pydocstyle,pycodestyle,bandit,pipenv,virtualenv,pytest,pylint` | Comma-separated tools |41| `installJupyterlab` | `false` | Install JupyterLab |42| `optimize` | `false` | Compile Python with optimizations (slower build) |43| `installPath` | `/usr/local/python` | Installation directory |4445## Full Example46```jsonc47{48 "name": "Python 3.12 Development",49 "image": "mcr.microsoft.com/devcontainers/python:3.12",50 "features": {51 "ghcr.io/devcontainers/features/github-cli:1": {}52 },53 "forwardPorts": [8000],54 "portsAttributes": {55 "8000": { "label": "App", "onAutoForward": "notify" },56 "8888": { "label": "Jupyter", "onAutoForward": "notify" }57 },58 "postCreateCommand": "pip install -r requirements.txt",59 "customizations": {60 "vscode": {61 "extensions": [62 "ms-python.python",63 "ms-python.vscode-pylance",64 "ms-python.black-formatter",65 "charliermarsh.ruff"66 ],67 "settings": {68 "python.defaultInterpreterPath": "/usr/local/bin/python",69 "[python]": {70 "editor.defaultFormatter": "charliermarsh.ruff",71 "editor.formatOnSave": true72 }73 }74 }75 }76}77```7879## Using the Base Image vs Feature80| Approach | When to Use |81|----------|-------------|82| `"image": "mcr.microsoft.com/devcontainers/python:3.12"` | Python-only projects |83| Base image + `ghcr.io/devcontainers/features/python:1` | Multi-language projects |8485## Virtual Environments86```jsonc87{88 "postCreateCommand": "python -m venv .venv && . .venv/bin/activate && pip install -r requirements.txt",89 "containerEnv": {90 "VIRTUAL_ENV": "/workspace/.venv",91 "PATH": "/workspace/.venv/bin:${containerEnv:PATH}"92 }93}94```9596## JupyterLab97```jsonc98{99 "features": {100 "ghcr.io/devcontainers/features/python:1": {101 "version": "3.12",102 "installJupyterlab": true103 }104 },105 "forwardPorts": [8888],106 "postStartCommand": "jupyter lab --ip=0.0.0.0 --no-browser --NotebookApp.token=''"107}108```109110## Poetry / uv / PDM111For modern package managers, install them in `postCreateCommand`:112```jsonc113{114 "postCreateCommand": "pip install uv && uv sync",115 "containerEnv": {116 "UV_LINK_MODE": "copy"117 }118}119```120121## Best Practices122- Use the dedicated `mcr.microsoft.com/devcontainers/python` image for Python-only projects.123- Pin the Python version to a minor release (`3.12`, `3.11`) for reproducibility.124- Prefer Ruff over separate flake8/black installs — it replaces multiple tools with a single fast one.125- Use `postCreateCommand` for `pip install` so dependencies are cached in Codespaces prebuilds.126- Set `python.defaultInterpreterPath` in VS Code settings to avoid interpreter selection prompts.127- For data science projects, enable `installJupyterlab` in the feature and forward port 8888.128- Use `containerEnv` to configure `VIRTUAL_ENV` if your project expects a venv.