微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python 调试未在 VS Code 中启动:“调试配置中的 Python 路径无效”

如何解决Python 调试未在 VS Code 中启动:“调试配置中的 Python 路径无效”

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

The Python path in your debug configuration is invalid

登录了与名为“示例”的项目相关的 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内容相同(我只保留调试相关的条目和蟒蛇):

{
    // * 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.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 需要反斜杠,而且它们本身需要被反斜杠转义,因此是双“”。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。