VSCode 远程容器默认 python 解释器

问题描述 投票:0回答:2

我有一个 dockerfile 来创建一个带有 miniconda 的容器并安装一些软件包(此处已修剪):

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

然后我使用 VSCode,将“远程容器”扩展为“在容器中打开文件夹”。

然后我打开一个 python 文件并按 F5 运行,但它无法识别某些包。我必须单击 VSCode 右下角将解释器从“3.9.2 64-bit”(/usr/bin/python3) 更改为“3.10.4 ('base':conda)”(/opt/conda/bin /蟒蛇)。

有没有办法避免这最后一步?也许向 devcontainer.json 文件添加一些内容?到目前为止的主要想法是尝试修改 PATH 环境变量,以便它不会检测到 3.9.2 python,或者使用 dockerfile 中的命令实际删除 3.9.2 python 文件夹或链接,但这些想法似乎都一样相当丑。

python docker visual-studio-code conda vscode-remote
2个回答
1
投票

您是否尝试在

devcontainer.json
中添加“设置”字段,以便可以指定
python.pythonPath
值?

像这样:

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

0
投票

这个问题现在有点老了,但我首先在谷歌搜索相同的东西时发现了它。

截至 2023 年 11 月,对我有效的设置是:

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

如果这还不够,我还将

"python.defaultInterpreterPath": "/usr/local/bin/python",
复制到
.vscode/settings.json
只是为了确定。

© www.soinside.com 2019 - 2024. All rights reserved.