You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code点击运行Python代码时终端异常切换至Ubuntu的解决方法

解决VS Code在PowerShell中运行Python代码的设置方法

以下是几种可行的调整方式:

方式一:修改Python扩展的终端配置

  • 打开VS Code设置界面(快捷键Ctrl+,)
  • 搜索python.terminal.integrated.shell.windows,将值设置为PowerShell路径:C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • 同时搜索python.terminal.integrated.automationShell.windows,同样设置为上述路径
  • 也可直接在settings.json中添加/修改配置:
"python.terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"python.terminal.integrated.automationShell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"

方式二:自定义Python运行任务的终端类型

  1. 打开命令面板(快捷键Ctrl+Shift+P),输入Tasks: Configure Task,选择创建tasks.json文件并选中Python相关模板
  2. 在生成的tasks.json中,为Python任务指定PowerShell终端,示例配置:
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Python: 当前文件",
            "type": "shell",
            "command": "${command:python.interpreterPath} ${file}",
            "options": {
                "shell": {
                    "executable": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
                }
            },
            "problemMatcher": ["$python"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

方式三:调整终端默认配置并排除WSL干扰

  • 打开终端面板,点击右上角下拉箭头,选择选择默认配置文件,选中PowerShell
  • 若安装了WSL扩展,可在设置中搜索terminal.integrated.defaultProfile.windows,确保值为PowerShell,同时在terminal.integrated.profiles.windows中优先配置PowerShell:
"terminal.integrated.profiles.windows": {
    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
    },
    "Ubuntu": {
        "source": "Ubuntu",
        "icon": "terminal-ubuntu"
    }
},
"terminal.integrated.defaultProfile.windows": "PowerShell"

内容的提问来源于stack exchange,提问作者Ngo Tuan Kiet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 07:17:05