VS Code 无法识别 Python 虚拟环境

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

我在 Mac 上使用 VS Code 编写 Python 代码。我在我的项目文件夹中创建了一个名为“venv”的虚拟环境,并在我的项目文件夹中打开了 VS Code。我可以在资源管理器窗格中看到 venv 文件夹。但是,如果我将包安装到虚拟环境中,并尝试将包导入到 Python 模块中,然后运行该模块,VS Code 会引发 ModuleNotFound 错误,指出没有模块。

我按照 VS Code 文档在 VS Code 中使用 Python 环境 中的说明进行操作,方法是打开命令面板,选择 Python:选择解释器,然后选择“venv/bin/folder”。但是当我这样做时,我得到了这个错误:

Failed to set 'pythonPath'. Error: Unable to write into folder settings.  Please open the 'my_project' folder settings to correct errors/warnings in it and try again.

这些“文件夹设置”是什么?我在上面引用的文档中没有看到任何关于我的虚拟环境目录的文件夹设置的内容。

环境:
VS 代码 1.35.1
用于 VS 代码的 Python 0.2.3
Python 3.7.1

更新

听从@khuynh 的建议,我打开了 settings.json 并发现了一个错误,那就是我试图用“//”注释掉一行。我没有意识到 JSON 文件不能包含评论。

删除该行后,我再次运行“Python:选择解释器”,但这次出现了一个在顶部显示

.vscode > settings.json >> code-runner.executorMap.python
的选项卡。该选项卡包含以下代码:

    {
        "python.pythonPath": "/usr/local/bin/python3"
        "code-runner.executorMap.python": "python3 -u"
    }

“code-runner.executorMap.python”下方有一条红色波浪线,下面的“问题”窗口显示“未知配置设置”。我不明白这个设置有什么问题。

python visual-studio-code vscode-code-runner
7个回答
10
投票

现在好像叫

python.defaultInterpreterPath


3
投票

如果您的工作场所设置文件中存在错误,则选择 Python 解释器将不起作用。您在“python.pythonPath”行后缺少逗号。

(我看到其他人在评论中回答了这个问题,但我想发布一个答案,让其他人更容易找到。)


2
投票

这听起来像是您直接打开了一个 Python 文件,而不是打开包含该文件的文件夹。如果你选择后者,那么你可以在你的设置中指定 Python 解释器。

否则,您可能会遇到权限错误,从而无法创建

.vscode
文件夹来保存您的设置。


2
投票

要在具有 Python 虚拟环境的 Mac 上设置 Visual Studio Code,请使用快捷方式“command + shift + p”编辑 Visual Studio Code 中的 JSON 工作区设置

在 Visual Studio Code 版本:1.45.1 中,workspace.json 设置如下所示:

{
    "folders": [
          {
              "path": "/Users/me/Documents/Projects/djangoproject"
          }
      ],
      "settings": {
          "python.pythonPath": "/Users/me/virtualenvs/djangovenv/bin/python3"
      }
}

这将在 Mac 上打开新的 bash 终端时自动激活 python 虚拟环境。


0
投票

刚才,我在从工作 PC 移动到家庭 PC 后重新打开外部驱动器上的 VS Code 工作区并遇到此错误。

我通过打开 VS 代码设置 cmd+shift+p 解决了这个问题,然后输入“设置”并选择选项“首选项:打开设置(JSON)”

然后我编辑了这一行:

"python.pythonPath": "C:\\Python37\\python3.exe",

以下内容(检查 python 可执行文件的路径):

"python.pythonPath": "C:\\Python37\\python.exe",

修复。

更新: 我想到的是,当我下周从我的工作计算机访问该项目时,我可能会再次遇到这个问题,因为我在那里安装了不同的 python 版本。会公布结果


0
投票

对我来说,wsl2 中的 python 插件丢失了。安装它并更改为 python.defaultInterpreterPath(新)为我修复了它。


0
投票
  1. 确保您已经在 VS Code 上安装了 Python 扩展。 这是链接:https://marketplace.visualstudio.com/items?itemName=ms-python.python

  1. python.pythonPath
    好像变成了
    python.defaultInterpreterPath

另一种方法是您可以使用 SHIFT + COMMAND + P 然后选择解释器。

这是我的设置文件。希望对某人有帮助。

{
    "python.defaultInterpreterPath": "venv/bin/python",
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=120"
    ],
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true,
    "python.linting.banditEnabled": false,
    "python.linting.mypyEnabled": false,
    "python.formatting.autopep8Args": ["--max-line-length", "250", "--experimental"],
    "editor.wrappingIndent": "indent",
    "editor.autoIndent": "keep",
    "workbench.colorCustomizations": {
        "tab.activeBackground": "#777a7a"
    },
    "[python]": {
        "editor.formatOnSave": true,
        "editor.insertSpaces": true,
        "editor.tabSize": 2,
    },
    "[yaml]": {
        "editor.formatOnSave": true,
        "editor.insertSpaces": true,
        "editor.tabSize": 2,
        "editor.quickSuggestions": {
            "other": true,
            "comments": false,
            "strings": true
        },
        "editor.autoIndent": "keep"
    },
    "files.trimTrailingWhitespace": true,
    "files.insertFinalNewline": true,
    "editor.rulers": [
        {
            "column": 120,
            "color": "#424142"
        },
    ],
}
© www.soinside.com 2019 - 2024. All rights reserved.