Videos
I'm starting to look into devcontainers with VSCode and I am looking for a container configuration (Dockerfile/devcontainer.json) that sets up a good Python development environment with some recommended extensions and settings (e.g. paths, venv, fromatting, linting) for beginners (its been some time since I was doing Python development), but that's not over-complicated.
I have the following resources that I have pieced together as a starting point and could use some input and feedback on these (I'm not sure if everything makes sense) and what to improve for a good development experience. Particularly I'm looking for input on the VSCode extensions / settings.
docker-compose.dev.yaml
version: '3.9'
networks: app-dev-network:
driver: bridge
name: app-dev-network
services:
backend:
image: backend-dev-image
container_name: backend-dev-container
build:
context: ./
dockerfile: ./Dockerfile.dev
volumes:
- ..:/workspace:cache
networks:
- app-dev-network
ports:
- target: 5000
published: 5000
command: sleep infinityDockerfile.dev
ARG VARIANT="3.11-bookworm"
FROM mcr.microsoft.com/devcontainers/python:${VARIANT}
RUN apt-get update && \
apt-get upgrade --yes --no-install-recommends && \
apt-get clean
ENV PYTHONUNBUFFERED=1devcontainer.json
{
"name": "Backend",
"dockerComposeFile": [
"./docker-compose.dev.yaml"
],
"service": "backend",
"workspaceFolder": "/workspace",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
],
"settings": {
"python.pythonPath": "${workspaceFolder}/.venv/bin/python",
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
}
}
},
"postCreateCommand": "pip3 install --user -r requirements.txt"
}
This question is a bit old now but I found it first on google searching for the same thing.
The setting that's worked for me as of Nov 2023 is:
devcontainer.json
{
"name": "python dev",
"image": "python:3.10",
"customizations":{
"vscode": {
"extensions":[
"ms-python.python",
"ms-python.vscode-pylance"
],
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python"
}
}
}
}
In case that's not enough, I also have "python.defaultInterpreterPath": "/usr/local/bin/python", copied into .vscode/settings.json just to be certain.
Did you try to add a "settings" field in your devcontainer.json so you can specifiy python.pythonPath value ?
Like this :
// devcontainer.json
{
"name": "My devcontainer",
"settings": {
"python.pythonPath": "/opt/conda/bin/python"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python",
"ms-azuretools.vscode-docker",
]
}
https://code.visualstudio.com/docs/python/python-tutorial
In the VScode documentation, there is a section called "Install and use packages" under the python tutorial. I think that will explain it to you.
Edit:
Have you added it to your dockerfile? Like:
RUN pip install pandas
Try to look at the answer to this question and see if it helps:
How to add Python libraries to Docker image
Edit2:
It seems like VSC are using the local environment, when it creates a container, and there isn't a devcontainer.json file.
I think you need to either install the libraries in your local environment or set up a development container in VSC:
https://code.visualstudio.com/docs/remote/create-dev-container
Microsoft have a GitHub repo with development container templates:
https://github.com/microsoft/vscode-dev-containers
If you already have set up a development container, can you please add your devcontainer.json file to your question, so it is possible to look at it?
Here is the tutorial for docker: https://code.visualstudio.com/docs/containers/quickstart-python#_add-docker-files-to-the-project
In your case I think you should just put the extra packages to generated requirements.txt. If that does not work out of the box, you can modify dockerfile extending the microsoft default python image.
