3

I have a dockerfile to create a container with miniconda and install a few packages (trimmed here):

FROM continuumio/miniconda3:4.11.0

# install the necessary packages
RUN conda install -c conda-forge python=3.10.4 \
  ipykernel=6.13.0 \
  numpy=1.22.3

ENV APP_DIR /app
WORKDIR ${APP_DIR}

CMD /bin/bash

I then use VSCode, with the "remote-containers" extension to "open folder in container".

I then open a python file and hit F5 to run, but it doesn't recognize some packages. I have to click in VSCode lower right corner to change the interpreter from "3.9.2 64-bit"(/usr/bin/python3) to "3.10.4 ('base':conda)" (/opt/conda/bin/python).

Is there a way to avoid this last step? Perhaps adding something to the devcontainer.json file? Main idea so far would be to try to modify the PATH environment variable so that it doesn't detect the 3.9.2 python, or actually delete the 3.9.2 python folder or link using a command in the dockerfile, but those ideas both seem pretty ugly.

3 Answers 3

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In my case the python.defaultInterpreterPath get ignored or will be overwritten by a nother setting. Adding the Python interpreter path to launch.json was working in my case. {... "python": "/usr/local/bin/python3",...}
In case it helps other people: The Python interpreter is set for new terminals. Sometimes I have a terminal already created when I open VS Code, this terminal does not get the Python interpreter that I specified activated. I have to create a new one, after all the initialisation is done, for having the right interpreter configured.
Confirm still works as at July 2025. I also use the variables for the dynamic position within the workspace folder as I'm using uv: "python.defaultInterpreterPath": "${containerWorkspaceFolder}/.venv/bin/python"
3

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",
    ]
}

1 Comment

I have a devcontainer.json file very similar to this, but my VSCode still defaults to a different python version than the one I specified in the "settings" clause. What could I do to make VSCode use the correct one?
2

tl;dr

  1. identify the used config file with Dev Containers: Open Container Configuration File
  2. set python.defaultInterpreterPath
  3. run Python: Clear Workspace Interpreter Setting
  4. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.