【问题标题】:why i am getting - file cannot be loaded because running scripts is disabled on this system为什么我得到 - 文件无法加载,因为运行脚本在这个系统上被禁用
【发布时间】:2023-02-12 01:40:14
【问题描述】:

在 pycharm 中加载 python 文件时,我收到以下警告,之前它不存在,print('Hello') 也按预期工作,但我无法安装 django。

File C:\Users\abhi\PycharmProjects\django_apps\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.
    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess

【问题讨论】:

  • 您是否尝试阅读 URL?
  • 请注意,此错误与 Python 无关,而是与 PowerShell 脚本有关。
  • 是的,我已经尝试了那里给出的命令,但收到另一个错误 -'Set-ExecutionPolicy' is not recognized as an internal or external command, operable program or batch file.

标签: python error-handling pycharm virtualenv sys


【解决方案1】:

尝试检查一下,这可能会有所帮助。

以下是使运行 PowerShell 脚本成为可能的方法:

打开 Windows PowerShell 或您使用的 IDE 的管理级终端 目前正在使用。输入Get-ExecutionPolicy。它显示了政策 对于当前的 PowerShell 会话。您也可以输入 Get-ExecutionPolicy -List 列出所有范围的策略。 get-execution-policy 更改范围内的执行策略 想。为此,您需要的命令是:Set-ExecutionPolicy -ExecutionPolicy <policy name> -Scope <scope>。一般来说,我们 推荐使用Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned,因为它更安全。 set-execution-policy 如果不能解决问题,输入: Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

Link

【讨论】:

  • Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted这对我有用
【解决方案2】:

我发现了两种可以成功解决此问题的方法:

  1. 通过启用 PowerShell 执行策略:

    PS C:Usersusr1>设置-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

    例如,当我想在项目的根文件夹中运行 yarn 命令时遇到了这个问题。然后通过运行上面的命令,我能够毫无问题地运行命令 yarn 。

    1. 使用 bash 终端而不是 powershell 终端 当您通过运行 Set-ExecutionPolicy 命令从 powershell 终端收到错误时,您可以使用此方法。就像下面的错误: 在这种情况下,通过将终端切换到 bash,您可以运行脚本:

【讨论】:

    猜你喜欢
    • 2021-10-27
    • 2017-04-28
    • 2020-03-06
    • 2021-02-22
    • 2013-10-06
    • 2022-10-25
    • 2020-06-20
    • 2023-02-13
    • 2020-03-11
    相关资源
    最近更新 更多