VSCode PowerShell扩展初始化失败,无法激活Python虚拟环境求助
解决PowerShell Editor Service连接失败无法激活Python虚拟环境的问题
问题重现
使用Activate.ps1激活Python虚拟环境时,触发以下错误:
[Warn - 2:20:50 pm] OmniSharp.Extensions.LanguageServer.Server.LspServerOutputFilter: Tried to send request or notification before initialization was completed and will be sent later OmniSharp.Extensions.JsonRpc.RequestCancelled | @Request='OmniSharp.Extensions.JsonRpc.RequestCancelled' [Error - 2:21:26 pm] Connection to PowerShell Editor Services (the Extension Terminal) was closed. See below prompt to restart! [Error - 2:21:26 pm] Server initialization failed. Message: Pending response rejected since connection got disposed Code: -32097 [Error - 2:21:26 pm] PowerShell Editor Services client: couldn't create connection to server. Message: Pending response rejected since connection got disposed Code: -32097 [Error - 2:21:26 pm] Sending request powerShell/getCommand failed. Message: Pending response rejected since connection got disposed Code: -32097
已尝试重装VSCode、清除缓存,问题未解决。
解决方案
调整PowerShell执行策略
以管理员身份打开PowerShell,执行:Set-ExecutionPolicy RemoteSigned输入
Y确认,允许本地脚本运行(默认策略会阻止Activate.ps1这类脚本)。重置VSCode PowerShell扩展配置
打开VSCode,按Ctrl+Shift+P打开命令面板,选择Preferences: Open Settings (JSON),添加或修改:"powershell.powerShellExePath": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", "powershell.enableProfileLoading": false指定系统默认PowerShell路径,禁用用户配置文件加载,避免扩展与自定义配置冲突。
清理PowerShell扩展缓存
关闭VSCode,删除以下目录:%USERPROFILE%\.vscode\extensions\ms-vscode.PowerShell-*(替换*为实际版本号)%USERPROFILE%\AppData\Roaming\Code\CachedData中所有与PowerShell相关的子目录
重启VSCode,扩展会自动重建缓存。
绕开扩展直接激活环境
在系统PowerShell中进入虚拟环境目录,执行:.\Scripts\Activate.ps1激活成功后,执行
code .打开VSCode,此时编辑器会继承已激活的虚拟环境。重建虚拟环境
删除损坏的虚拟环境,重新创建:Remove-Item -Recurse -Force .\venv python -m venv venv .\venv\Scripts\Activate.ps1排除环境文件损坏导致的脚本执行异常。
内容的提问来源于stack exchange,提问作者shu-toz
相关产品推荐
相关产品推荐

