pythondockervisual-studio-codecondavscode-remote

VSCode remote-container default python interpreter


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.


Solution

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