VS代码终端:无法加载Activate.ps1,因为在此系统上禁用了运行脚本

2024-06-24 12:40:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python创建了一个虚拟环境,现在在vscode中从命令行激活该虚拟环境时,我发现了错误

PS C:\Users\hpoddar\Desktop\WebDev\ReactComplete\DjangoReact\ArticlesApp\APIProject> ..\venv\scripts\activate    
..\venv\scripts\activate : File C:\Users\hpoddar\Desktop\WebDev\ReactComplete\DjangoReact\ArticlesApp\venv\scripts\Activate.ps1 cannot be loaded because running 
scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ ..\venv\scripts\activate
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

这是我的项目结构

enter image description here

但是,如果我从命令行激活它,它将正常工作

Python版本:3.9.2

enter image description here


Tags: 命令行venv错误虚拟环境scriptsvscodeusersps
2条回答

一种方法是将VSCode中的终端更改为命令提示符,而不是PowerShell

  1. 打开终端右侧的下拉列表并选择Select Default Profile1

  2. 从选项中选择命令提示。 enter image description here

或者,您也可以在PowerShell中将执行策略设置为RemoteSignedUnrestricted

注意:这只影响当前用户

  1. 开放式PowerShell

  2. 运行以下命令:Set-ExecutionPolicy RemoteSigned -Scope CurrentUserSet-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

(删除-Scope CurrentUser以应用于所有用户)

在vscode终端中执行此命令,它对我有效

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

相关问题 更多 >