最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Windows中VSCode的PowerShell终端启动ps脚本出错:无法加载文件 venv\Scripts\activate.ps1 因为在此系统上禁止运行脚本

VSCode crifan 998浏览 0评论
折腾:
【未解决】windows中用PyInstaller打包mitmdump的Python脚本为exe
期间,后来试了试VSCode中集成的PowerShell中去启动virtualenv,结果激活环境失败:
PS D:\dev\DevRoot\mitmdumpurlsaver> .\venv\Scripts\activate
.\venv\Scripts\activate : 无法加载文件 D:\dev\DevRoot\mitmdumpurlsaver\venv\Scripts\activate.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/
go.microsoft.com/fwlink/?LinkID=135170
 中的
about_Execution_Policies。
所在位置 行:1 字符: 1
+ .\venv\Scripts\activate
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
重启PS试试
问题依旧:
所以去找找,如何解除运行脚本
win10 powershell 因为在此系统上禁止运行脚本
PowerShell因为在此系统中禁止执行脚本问题的解决方法_小黄花的故事的博客-CSDN博客
powershell 因为在此系统上禁止运行脚本… – 掘金
PS D:\dev\DevRoot\mitmdumpurlsaver> about_Execution_Policy
about_Execution_Policy : 无法将“about_Execution_Policy”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 1
+ about_Execution_Policy
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (about_Execution_Policy:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
不对。
PS D:\dev\DevRoot\mitmdumpurlsaver> get-executionpolicy
Restricted
是受限制的。
PS D:\dev\DevRoot\mitmdumpurlsaver> set-executionpolicy remotesigned
set-executionpolicy : 对注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell”的访问被拒绝。 要更改默认(LocalMachine)作用域的执行策略,请使用“以管理员身份运行
”选项启动 Windows PowerShell。要更改当前用户的执行策略,请运行 "Set-ExecutionPolicy -Scope CurrentUser"。
所在位置 行:1 字符: 1
+ set-executionpolicy remotesigned
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
结果没权限。
PS D:\dev\DevRoot\mitmdumpurlsaver> Set-ExecutionPolicy -Scope CurrentUser


位于命令管道位置 1 的 cmdlet Set-ExecutionPolicy
请为以下参数提供值:
ExecutionPolicy:
但是不知道输入啥,直接回车
Set-ExecutionPolicy : 无法绑定参数“ExecutionPolicy”。无法将值“”转换为类型“Microsoft.PowerShell.ExecutionPolicy”。错误:“无法处理标识符名称 ,因为该名称与以下枚举器名称相同或非常类似: Unrestri
cted, RemoteSigned, AllSigned, Restricted, Default, Bypass, Undefined。请使用更具体的标识符名称。”
所在位置 行:1 字符: 1
+ Set-ExecutionPolicy -Scope CurrentUser
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-ExecutionPolicy],ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
又报错了。
尝试去找到powershell
搜索 power shell,找到了。
点击 以管理员身份运行
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。


PS C:\Windows\system32> set-executionpolicy remotesigned


执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”): A
PS C:\Windows\system32> get-executionpolicy
RemoteSigned
貌似可以了。
再回去VSCode中PS中activate试试
先kill掉当前PS窗口:
新建一个
然后就可以正常激活虚拟环境了:
【总结】
此处windows 10中,默认PowerShell禁止了运行其他不信任的脚本。
所以此处VSCode的终端,其内置调用的PowerShell,启动时尝试激活Python虚拟环境virtualenv的脚本
d:/dev/DevRoot/mitmdumpurlsaver/venv/Scripts/activate.ps1
结果报错,没权限运行。
解决办法:去允许PowerShell中运行ps脚本。
步骤:
Windows10中找到PowerShell脚本(比如左下角搜索 power shell)
点击 以管理员身份运行
启动后,输入:
set-executionpolicy remotesigned
再输入A,表示全部允许。即可。
另外:
用:
get-executionpolicy
确认输出是:RemoteSigned
即确认已解除限制。
然后VSCode新建终端时,默认PowerShell启动virtualenv就正常了:
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
                                    & d:/dev/DevRoot/mitmdumpurlsaver/venv/Scripts/activate.ps1
(venv) PS D:\dev\DevRoot\mitmdumpurlsaver>

转载请注明:在路上 » 【已解决】Windows中VSCode的PowerShell终端启动ps脚本出错:无法加载文件 venv\Scripts\activate.ps1 因为在此系统上禁止运行脚本

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
99 queries in 0.185 seconds, using 23.44MB memory