【问题标题】:Python debugging does not start in VS Code: "The Python path in your debug configuration is invalid."Python 调试未在 VS Code 中启动:“您的调试配置中的 Python 路径无效。”
【发布时间】:2021-09-13 17:46:46
【问题描述】:

我尝试通过F5 在Windows 10 上的VS Code 以及右上角的GUI 调试菜单中启动Python 调试。然而,它总是在右下角显示以下错误窗口,说明"The Python path in your debug configuration is invalid."

我登录了一个与名为“Example”的项目相关的python环境:

username@hd1pcms0347 MINGW64 ~/Projects
$ ls
Example  Example-venv

我像这样激活虚拟环境(venv):

username@hd1pcms0347 MINGW64 ~/Projects
$ source Example-venv/Scripts/activate

(Example-venv) 
username@hd1pcms0347 MINGW64 ~/Projects

“示例”项目的顶级目录树如下所示:

Example:.
├───.git
├───.vscode
├───docs
└───src
    ├───build
    ├───config
    ├───scripts
    └───tests

我要调试的 Python 脚本位于文件夹“scripts”和“tests”的一些子目录中。

现在,连接到当前venvpython.exe位于这里:

/c/Users/andreas.luckert/Projects/Merck-venv/Scripts/python.exe

which python的输出:

(Example-venv)
username@hd1pcms0347 MINGW64 ~/Projects/Example-venv/Scripts
$ which python
/c/Users/username/Projects/Example-venv/Scripts/\Users\username\Projects\Example-venv/Scripts/python

在左下角,我选择了正确的 Python 解释器路径。

至于全局和本地settings.json,分别位于C:\Users\username\AppData\Roaming\Code\User\settings.jsonC:\Users\username\Projects\Example\.vscode\settings.json,内容相同(我只保留了调试和python相关的条目):

{
    // * Breadcrumbs options
    "debug.allowBreakpointsEverywhere": true,
    // * JUPYTER options
    "jupyter.sendSelectionToInteractiveWindow": true,
    // NOTE on provenance: from org-mode VS Code extension docs ("too tone")
    //  * PYTHON options
    "[python]": {
        "editor.rulers": [
            80,
            120
        ],
        "editor.defaultFormatter": "ms-python.python"
    },
    // "python.defaultInterpreterPath": "${env:PYTHON_EXE_LOC}",
    "python.defaultInterpreterPath": "/c/Users/username/Projects/Example-venv/Scripts/python.exe",
    // "python.envFile": "${workspaceFolder}${pathSeparator}.vscode${pathSeparator}vscode.env",
    "python.analysis.completeFunctionParens": true,
    "python.autoComplete.addBrackets": true,
    "python.terminal.activateEnvironment": true,
    "python.terminal.executeInFileDir": false,
    "python.terminal.launchArgs": [
        "-c",
        "\"from IPython import start_ipython; start_ipython()\""
    ],
    // Possible values: "Jedi", "Pylance", "Microsoft", "None".
    "python.languageServer": "Pylance",
    "python.jediMemoryLimit": 1,
    "python.linting.enabled": true,
    "python.linting.lintOnSave": true,
    "python.linting.maxNumberOfProblems": 100,
    "python.linting.ignorePatterns": [
        ".vscode/*.py",
        "**/site-packages/**/*.py"
    ],
    "python.analysis.diagnosticSeverityOverrides": {
        "reportUnusedImport": "information",
        "reportMissingImports": "none"
    },
    "python.linting.pylintPath": "C:\\Users\\username\\Projects\\Example-venv\\Scripts\\pylint.exe",
    "python.linting.pylintEnabled": true,
    "python.linting.pylintUseMinimalCheckers": false,
    "python.analysis.useImportHeuristic": true,
    "python.formatting.provider": "yapf",
    "python.testing.autoTestDiscoverOnSaveEnabled": true,
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        ".",
        "-p",
        "*test*.py"
    ],
    "python.testing.pytestArgs": [],
}

最后,位于C:\Users\username\Projects\Example\.vscode\launch.json 的本地launch.json 包含以下内容:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            // "python": "${workspaceFolder}${pathSeparator}.vscode${pathSeparator}vscode.env",
            "python": "/c/Users/username/Projects/Example-venv/Scripts/python.exe",
            "redirectOutput": true,
            "justMyCode": false,
            "logToFile": true,
            "stopOnEntry": false,
        },
        ...
    ]
}

我已经尝试了很多配置,但我似乎无法摆脱错误,即使所有这些路径都应该是正确的。

【问题讨论】:

    标签: python-3.x visual-studio-code vscode-settings vscode-debugger pythonpath


    【解决方案1】:

    投入

    "python.defaultInterpreterPath": "C:\\Users\\username\\Projects\\Example-venv\\Scripts\\python.exe"
    

    在我的全局和本地 settings.json

    "python": "C:\\Users\\username\\Projects\\Example-venv\\Scripts\\python.exe"
    

    在我的本地 launch.json 中已经解决了这个问题。 这肯定是因为Windows 需要反斜杠,而且它们本身需要被反斜杠转义,因此是双“”。

    然而,这似乎不是整个设置最优雅的解决方案。 如果有人有更好的设置想法,请随时发布答案。

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 2021-06-20
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 2018-08-16
      • 2020-11-10
      相关资源
      最近更新 更多