Windows 11下VSCode无法激活Python虚拟环境的问题求助
Windows 11下VSCode激活Python虚拟环境报错解决办法
问题现象
在Windows 11系统的VSCode中激活Python虚拟环境时触发错误,无需使用Python环境管理器扩展也会出现该问题,具体错误信息如下:
& : File C:\Users\mudit\vscode\python django projects\dev\venv\Scripts\Activate.ps1 cannot be loaded. The file C:\Users\mudit\vscode\python django projects\dev\venv\Scripts\Activate.ps1 is not digitally signed. You cannot run this script on the current system. For more information about running scripts and setting execution policy, see about_Execution_Policies. At line:1 char:3
- & "c:/Users/mudit/vscode/python django projects/dev/venv/Scripts/Acti ...
- CategoryInfo : SecurityError: (:) [], PSSecurityException
- FullyQualifiedErrorId : UnauthorizedAccess
解决步骤
该问题由Windows PowerShell执行策略限制导致,按以下操作修改即可:
- 以管理员身份打开PowerShell:右键点击开始菜单,选择「Windows PowerShell (管理员)」
- 查看当前执行策略:执行以下命令
默认通常返回Get-ExecutionPolicyRestricted,此策略禁止运行任何脚本 - 修改执行策略:执行以下命令
按提示输入Set-ExecutionPolicy RemoteSignedY确认修改,该策略允许运行本地未签名脚本,同时要求远程脚本必须签名,兼顾安全性与使用需求 - 验证修改结果:再次执行
Get-ExecutionPolicy,确认返回RemoteSigned - 重启VSCode:重新打开VSCode后即可正常激活虚拟环境
若仅需临时运行脚本,不想修改全局策略,可在当前PowerShell会话中临时设置:
Set-ExecutionPolicy -Scope Process RemoteSigned
此设置仅对当前会话生效,关闭PowerShell后自动恢复原策略
内容的提问来源于stack exchange,提问作者MUDIT DAHRA
相关产品推荐
相关产品推荐

