pythonvisual-studio-codevirtualenvcommand-promptunauthorizedaccessexcepti

VS code terminal : Activate.ps1 cannot be loaded because running scripts is disabled on this system


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


Solution

  • 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)