【VSCode】【Python】The Python path in your debug configuration is invalid

目录

一. 问题

二. 环境

三. 解释

四. 解决

4.1 用户区配置

4.2 工作区配置

五. 测试

六. 参考


一. 问题

        升级 VSCode 后,不知啥时候,python debug 的时候,提示 Python 解释器路径无效 ?

        网上搜了一大堆无用的信息,也不管用的,哎,只能自己去官网折腾和鼓捣了;

总之:

  •  python.pythonPath 从settings 中完全删除是一个巨大的破坏性更改;

-- 此对话已被锁定为过于激烈且仅限于合作者。

二. 环境

        环境信息, 如下所示:

# VSCode
版本: 1.57.1 (Universal)
提交: 507ce72a4466fbb27b715c3722558bb15afa9f48
日期: 2021-06-17T13:28:32.912Z
Electron: 12.0.7
Chrome: 89.0.4389.128
Node.js: 14.16.0
V8: 8.9.255.25-electron.0
OS: Darwin x64 19.4.0

# Python
python: 2.7 ❌
python: 3.9 ✅
ms-python.python: v2021.6.944021595
ms-python.vscode-pylance: v2021.6.2

三. 解释

        参考:

                1. https://github.com/microsoft/vscode-python/wiki/AB-Experiments#tldr

                2. https://devblogs.microsoft.com/python/python-in-visual-studio-code-may-2020-release/

                3. 弃用 PythonPath

        有如下信息:

  • The python.pythonPath setting is no longer used by the Python extension.
  • A new optional setting python.defaultInterpreterPath is introduced in the user and workspace scope, from which the extension will read the value when loading a project for the first time.
  • Changes to the python.defaultInterpreterPath will not be picked up by the Python extension once an interpreter is already selected for the workspace. The extension will also not set nor change the value of this setting, it will only read from it.
  • A VSCode internal storage is introduced which will now store the interpreter settings in the workspace & workspace folder scope. i.e workspace settings are no longer stored in settings.json/.code-workspace, but an internal storage.
    • You can change the value stored in workspace settings using Python: Select Interpreter command.
    • See the Python output channel to check the value of the interpreter selected.
    • You can clear the value stored using Python: Clear Workspace Interpreter Setting command.

啥意思呢?

我用机器翻译一下:

  • python.pythonPath: Python 扩展不再使用该设置。
  • python.defaultInterpreterPath: 在用户和工作区范围中引入了一个新的可选设置,扩展将在首次加载项目时从中读取值。
  • python.defaultInterpreterPath: 一旦为工作区选择了解释器,Python 扩展就不会获取对的更改。扩展也不会设置或更改此设置的值,它只从中读取。
  • 引入了 VSCode 内部存储,现在将解释器设置存储在工作区和工作区文件夹范围内。即工作区设置不再存储在 settings.json/.code-workspace 中,而是存储在内部存储中。


                - 您可以使用Python: Select Interpreter 命令更改存储在工作区设置中的值。
                - 查看 Python 输出通道以检查所选解释器的值。
                - 您可以使用Python: Clear Workspace Interpreter Setting 命令清除存储的值。

哎,一言难尽

        Path to Python, you can use a custom version of Python by modifying this setting to include the full path. This default setting is used as a fallback if no interpreter is selected for the workspace. The extension will also not set nor change the value of this setting, it will only read from it.

翻译(引用):

        对于Python的路径,您可以通过修改此设置以包含完整路径来使用自定义版本的Python。如果没有为工作区选择解释器,则此默认设置用作回退。扩展也不会设置或更改此设置的值,它只从中读取。

环境自动选择过程,如下:

  1. 选择具有最新Python 版本的环境
  2. 按字母顺序选择第一个

        如果想在状态栏得到 "Select Python Environment" 的提示,可以设置 "Select Python Environment" : " " ,重启 VSCode 即可;

        The "python.pythonPath" setting in your settings.json is no longer used by the Python extension. If you want, you can use a new setting called "python.defaultInterpreterPath" instead. Keep in mind that you need to change the value of this setting manually as the Python extension doesn’t modify it when you change interpreters. Learn more.

下面给出解决方式;

四. 解决

工作区解释器的路径现在将存储在 VS Code 的持久存储中,而不是settings.json 文件中;

您可以使用Python: Select Interpreter 命令更改特定于工作区的值。在命令面板上使用该命令时,存在一个新选项来指定解释器路径。

4.1 用户区配置

{
    "breadcrumbs.symbolSortOrder": "type",
    "editor.formatOnType": true,
    "editor.codeActionsOnSave": [
        "editor.action.formatDocument"
    ],
    "editor.tabSize": 4,
    "editor.detectIndentation": false,
    "editor.suggestSelection": "first",
    "editor.fontSize": 14,
    "editor.fontLigatures": false,
    "editor.formatOnSave": true,
    // "editor.formatOnType": true,
    "editor.wordWrap": "wordWrapColumn",
    "editor.wordWrapColumn": 140,
    "editor.columnSelection": false,
    "editor.mouseWheelZoom": false,
    "editor.renderWhitespace": "all",
    "editor.renderControlCharacters": true,
    "editor.suggest.shareSuggestSelections": true,
    "editor.renderIndentGuides": true,
    "editor.rulers": [
        100
    ],
    "explorer.confirmDelete": false,
    "extensions.autoUpdate": false,
    // "debug.toolBarLocation": "docked",
    "debug.console.fontSize": 14,
    // "files.autoSave": "afterDelay",
    "files.insertFinalNewline": true,
    "files.autoGuessEncoding": true,
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/*.pyc": true,
        "**/__pycache__": true,
    },
    "git.autofetch": true,
    "git.confirmSync": false,
    "guides.enabled": false,
    "python.defaultInterpreterPath": "${workspaceFolder}/venv2/bin/python",
    // https://github.com/microsoft/vscode-python/wiki/AB-Experiments
    "python.experiments.enabled": true,
    "python.experiments.optInto": [
        // "pythonDeprecatePythonPath",
        // "pythonTensorboardExperiment",
        // "pythonDiscoveryModule",
        // "pythonDiscoveryModuleWithoutWatcher",
        // "pythonJediLSP",
        // "pythonSurveyNotification",
        "All"
    ],
    "python.experiments.optOutFrom": [
        // "pythonDeprecatePythonPath",
        // "pythonTensorboardExperiment",
        // "pythonDiscoveryModule",
        // "pythonDiscoveryModuleWithoutWatcher",
        // "pythonJediLSP",
        // "pythonSurveyNotification",
        // "All"
    ],
    "python.logging.level": "info",
    "python.venvFolders": [
        "${workspaceFolder}/venv2",
        "${workspaceFolder}/venv"
    ],
    "python.venvPath": "${workspaceFolder}/venv2",
    // "markdown.extension.toc.githubCompatibility": true,
    "markdownlint.config": {
        "MD033": false,
        "MD034": false,
    },
    "markdown.extension.katex.macros": {},
    "markdown.extension.toc.levels": "2..6",
    "markdown-pdf.highlightStyle": "github.css",
    // "markdown-pdf.headerTemplate":"<div style=\"font-size: 9px; margin-left: 1cm;\"> <span class='title'></span></div> <div style=\"font-size: 9px; margin-left: auto; margin-right: 1cm; \"> <span class='date'></span></div>",
    "markdown-pdf.headerTemplate": "<div></div>",
    "scm.defaultViewMode": "tree",
    "scm.alwaysShowActions": true,
    "workbench.iconTheme": "vscode-icons",
    "workbench.editor.enablePreview": true,
    "workbench.editor.enablePreviewFromQuickOpen": true,
    "window.zoomLevel": 0,
    "workbench.editorAssociations": {
        "*.ipynb": "jupyter.notebook.ipynb"
    },
    // "workbench.sideBar.location": "left",
    // "workbench.settings.openDefaultKeybindings": true,
    // "workbench.settings.openDefaultSettings": true,
    // "workbench.settings.useSplitJSON": true,
    "terminal.integrated.copyOnSelection": true,
    "terminal.integrated.fontSize": 14,
    "terminal.integrated.profiles.linux": {
        "bash": {
            "path": "bash"
        },
        "zsh": {
            "path": "zsh"
        },
        "fish": {
            "path": "fish"
        },
        "tmux": {
            "path": "tmux",
            "icon": "terminal-tmux"
        },
        "pwsh": {
            "path": "pwsh",
            "icon": "terminal-powershell"
        }
    },
}

重点配置如下信息:

{
    "python.defaultInterpreterPath": "${workspaceFolder}/venv2/bin/python",
    // https://github.com/microsoft/vscode-python/wiki/AB-Experiments
    "python.experiments.enabled": true,
    "python.experiments.optInto": [
        // "pythonDeprecatePythonPath",
        // "pythonTensorboardExperiment",
        // "pythonDiscoveryModule",
        // "pythonDiscoveryModuleWithoutWatcher",
        // "pythonJediLSP",
        // "pythonSurveyNotification",
        "All"
    ],
    "python.experiments.optOutFrom": [
        // "pythonDeprecatePythonPath",
        // "pythonTensorboardExperiment",
        // "pythonDiscoveryModule",
        // "pythonDiscoveryModuleWithoutWatcher",
        // "pythonJediLSP",
        // "pythonSurveyNotification",
        // "All"
    ],
    "python.logging.level": "info",
    "python.venvFolders": [
        "${workspaceFolder}/venv2",
        "${workspaceFolder}/venv"
    ],
    "python.venvPath": "${workspaceFolder}/venv2",
}

4.2 工作区配置

{
    "[python]": {
        "editor.defaultFormatter": "ms-python.python"
    },
    "python.analysis.extraPaths": [
        "./src",
        "./mock",
        "./test",
        "./venv2/bin",
        "./venv2/Lib/python2.7/site-packages"
    ],
    "python.analysis.logLevel": "Trace",
    // "python.analysis.watchSearchPaths": true,
    "python.analysis.typeCheckingMode": "basic",
    // "python.analysis.typeCheckingMode": "strict",
    "python.analysis.diagnosticMode": "openFilesOnly",
    // https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md
    "python.analysis.diagnosticSeverityOverrides": {},
    "python.analysis.useLibraryCodeForTypes": true,
    "python.analysis.autoImportCompletions": true,
    "python.autoComplete.addBrackets": true,
    "python.analysis.completeFunctionParens": true,
    // "python.autoComplete.addBrackets": false,
    // "python.analysis.completeFunctionParens": false,
    "python.autoComplete.extraPaths": [
        "./src",
        "./mock",
        "./tests",
        "./venv2/bin",
        "./venv2/Lib/python2.7/site-packages",
        // "./venv/bin",
        // "./venv/Lib/python3.7/site-packages",
    ],
    // "python.autoUpdateLanguageServer": true,
    // "python.defaultInterpreterPath": "${workspaceFolder}/venv2/bin/python",
    // "python.defaultInterpreterPath": "venv2/bin/python",
    // "python.diagnostics.sourceMapsEnabled": true,
    "python.envFile": "${workspaceFolder}/.env",
    // "python.pythonPath": "${workspaceFolder}/venv2/bin/python",
    // "python.pythonPath": "venv2/bin/python",
    // "python.pythonPath": "venv2\\Scripts\\python.exe",
    "python.formatting.provider": "yapf",
    "python.formatting.yapfArgs": [
        "--style",
        "{based_on_style: pep8, indent_width: 20}"
    ],
    // "python.formatting.yapfArgs": [
    //     "--style", "{based_on_style: pep8 ,indent_width: 4, column_limit: 140}",
    // ],
    // "python.languageServer": "Jedi",
    "python.languageServer": "Pylance",
    // "python.languageServer": "Microsoft",
    // "python.logging.level": "info",
    "python.linting.enabled": true,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=140",
        // "--ignore=E251",
        "--verbose",
    ],
    "python.linting.ignorePatterns": [
        ".vscode/*.py",
        "**/site-packages/**/*.py"
    ],
    "python.linting.lintOnSave": true,
    "python.linting.pylintEnabled": false,
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "test_*.py"
    ],
    // "python.testing.autoTestDiscoverOnSaveEnabled": true,
    "python.testing.cwd": "${workspaceFolder}",
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": false,
    "python.testing.promptToConfigure": false,
    "python.testing.unittestEnabled": true,
    // "workbench.settings.openDefaultSettings": true,
    // "workbench.settings.editor": "ui",
    // "workbench.settings.openDefaultKeybindings": true,
    // "workbench.settings.useSplitJSON": true,
}

五. 测试

Python 2.7Python 3.8备注
VSCode(1.57.1 (Universal))

遗留与说明:

  1. Python 2.7 问题,没有 Debug 执行成功,依然提示设置 Python 解释器路径无效,怀疑 VSCode 慢慢再摒弃 Python 2.x 的支持力度;
  2. Python 3.8,目前 Debug 执行成功的;

六. 参考

  1. https://code.visualstudio.com/docs/python/environments
  2. https://code.visualstudio.com/docs/python/settings-reference#_general-settings
  3. https://github.com/microsoft/vscode-python/pull/16485
  4. https://github.com/microsoft/vscode-python/wiki/AB-Experiments
  5. https://marketplace.visualstudio.com/items?itemName=ms-python.python
  6. https://marketplace.visualstudio.com/items/ms-python.python/changelog
  7. https://github.com/microsoft/vscode-python/blob/67590fb2e2a1a439267cb267b86f88acac40b5d7/package.json#L1115-L1119
  8. https://github.com/microsoft/vscode-python/issues/2125
  9. https://github.com/microsoft/vscode-python/issues/2126
  10. https://github.com/microsoft/vscode-python/issues/11015
  11. https://github.com/microsoft/vscode-python/issues/12313
  12. https://github.com/microsoft/vscode-python/issues/12665

(完)

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VS Code中遇到Python SyntaxError: invalid syntax错误通常是由于代码中存在语法错误导致的。这种错误可能是拼写错误、缺少括号、冒号等简单的语法错误,也可能是缩进错误或者使用了不兼容的Python版本。 解决这个问题的方法有以下几种: 1. 检查代码语法:首先,你需要仔细检查代码,确保没有拼写错误、缺少括号、冒号等简单的语法错误。特别注意检查代码的缩进是否正确,Python对缩进非常敏感。 2. 检查Python版本:确保你正在使用的Python版本与你的代码兼容。可以在VS Code的终端中运行以下命令来查看Python版本: ```shell python --version ``` 如果你的代码使用了不兼容的Python版本,可以考虑切换到兼容的版本或者修改代码以适应当前版本。 3. 使用调试器:VS Code提供了强大的调试功能,可以帮助你找到代码中的错误。你可以在VS Code中设置断点,逐步执行代码并观察变量的值,从而找到错误所在。可以使用快捷键F5启动调试器。 4. 使用代码格式化工具:VS Code内置了代码格式化工具,可以帮助你自动对齐代码并修复一些简单的语法错误。可以使用快捷键Ctrl+Shift+P打开命令面板,然后输入"Format Document"来格式化代码。 5. 检查依赖库:如果你的代码依赖于某些第三方库,可能是由于缺少或者版本不兼容的库导致的错误。可以使用pip命令来安装或更新依赖库。 请注意,以上方法仅为常见解决方案,具体解决方法可能因情况而异。如果以上方法无法解决问题,请提供更多的错误信息或代码片段,以便更好地帮助你解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值