tl;dr
- identify the used config file with
Dev Containers: Open Container Configuration File
- set
python.defaultInterpreterPath
- run
Python: Clear Workspace Interpreter Setting
- re-open VS Code
Config file
I attach VS Code to a container started with docker compose
. docker compose
runs in Ubuntu terminal, not from within VS Code.
For a long time I though devcontainer.json
was used to configure the container. However that is not the case when attaching to a running container as per https://code.visualstudio.com/docs/devcontainers/attach-container.
You can easily verify this by running Dev Containers: Open Container Configuration File
, which on Ubuntu opens a file like ~/.config/Code/User/globalStorage/ms-vscode-remote.remote-containers/imageConfigs/{image|container-name}.json
.
That is the file where you want to add customizations.vscode.settings.python.defaultInterpreterPath
, e.g.
{
"workspaceFolder": "/app",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.black-formatter",
"ms-python.flake8",
"ms-python.isort"
],
"settings": {
"python.defaultInterpreterPath": "/app/.venv/bin/python"
}
}
}
}
Python interpreter
If you have issues with VS Code not selecting the configured interpreter, it's most likely due to the following note mentioned in https://code.visualstudio.com/docs/python/environments
Note: Changes to the python.defaultInterpreterPath setting are not picked up after an interpreter has already been selected for a workspace; any changes to the setting will be ignored once an initial interpreter is selected for the workspace.
The note is explained in https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath.
You need to clear the initial interpreter setup from VS Code’s persistent storage in order to make VS Code use the one configured in the step above.
Run Python: Clear Workspace Interpreter Setting
, then re-open VS Code and it should use the configured Python interpreter.