3

I created a virtual environment in python, now while activating the same from my command line in vscode I am getting the error

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

This is my project structure

enter image description here

However if I activate the same from my command line, it works without any error.

Python version : 3.9.2

enter image description here

Himanshu Poddar
  • 3,438
  • 5
  • 34
  • 67
  • https://stackoverflow.com/search?q=%5Bvirtualenv%5D+Activate.ps1+cannot+be+loaded+because+running++scripts+is+disabled+on+this+system – phd Oct 17 '21 at 17:23

2 Answers2

9

A way is changing the terminal in VSCode to Command Prompt instead of PowerShell.

  1. Open the drop-down on the right of the terminal and choose Select Default Profile 1

  2. Select Command Prompt from the options. enter image description here

Or, you can also set the execution policy to RemoteSigned or Unrestricted in PowerShell

Note: This only affects the current user

  1. Open PowerShell

  2. Run the following command: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser OR Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

(Remove -Scope CurrentUser to apply to all users)

PyxlDavon
  • 1,036
  • 4
  • 13
5

Execute this command in the vscode terminal, it worked for me

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
  • suggesting to run a code without explaining what it's supposed to do, or not adding a link that explains it, just because "it worked for you" is not a good practise. – Mahrkeenerh Oct 17 '21 at 14:52