VSCode remote-container default python interpreter
Asked Answered
K

3

2

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.

Knifeedged answered 19/5, 2022 at 13:49 Comment(0)
S
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",
    ]
}
Spiderwort answered 19/5, 2022 at 14:2 Comment(1)
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?Dowdy
C
7

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.

Cohla answered 13/11, 2023 at 4:29 Comment(1)
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",...}Dogleg
S
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",
    ]
}
Spiderwort answered 19/5, 2022 at 14:2 Comment(1)
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?Dowdy
R
0

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.

Redfield answered 4/8 at 20:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.