Activate.ps1 cannot be loaded because running scripts is disabled on this system

March 8th 2024 783 views • 2 comments

[REPOST] I am a noob at Python in Windows. I am getting env\Scripts\Activate.ps1 cannot be loaded because running scripts is disabled on this system. The issue in my Windows machine while activating the virtual environment. Can somebody help me, I am on my Visual Studio CODE.

PS C:\Users\USER\Projects\content_idea_gen> env/Scripts/activate  
env/Scripts/activate : File C:\Users\USER\Projects\content_idea_gen\env\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
+ env/Scripts/activate
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

@codie Here's the solution. It’s the issue from your VSCODE.

  1. Open VSCODE

  2. Press F1, and type Preferences: Open Settings (JSON) and click on it. i.e Simply open settings.json

  3. Go to the bottom of settings.json

  4. Add this line at the end of your visual studio code: "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]

  5. Your bottom settings.json should look like this..

….
“explorer.confirmDelete”: false,
“terminal.integrated.shell.windows”: “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”,
“terminal.integrated.shellArgs.windows”: [“-ExecutionPolicy”, “Bypass”]
}

You need to change the PowerShell execution policy to allow scripts to run. Here's how to do it:

1. Open PowerShell as Administrator:

- Press Win + X and select "Windows PowerShell (Admin)".

2. Set the Execution Policy:

- Run this command to allow local scripts to run:

```powershell

Set-ExecutionPolicy RemoteSigned

```

3. Confirm the Change:

- Type Y and press Enter to confirm.

4. Activate the Virtual Environment:

- Close and reopen PowerShell or your VS Code terminal.

- Activate your virtual environment with:

```powershell

.\env\Scripts\Activate.ps1

```

This should resolve the issue and allow you to activate your virtual environment.

For better understanding, here are the some excellent learning platforms.

1. W3 School

2. Iqra Technology

3. JavatPoint