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运行任务的终端类型
- 打开命令面板(快捷键
Ctrl+Shift+P),输入Tasks: Configure Task,选择创建tasks.json文件并选中Python相关模板 - 在生成的
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
相关产品推荐
相关产品推荐

