vscode-python: Activating an environment fails because running powershell scripts is disabled by default on Windows

Environment data

  • VS Code version: 1.27.1 (user setup)
  • Extension version (available under the Extensions sidebar): 2018.8.0
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7…0
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): venv
  • Relevant/affected Python packages and their versions: N/A

Actual behavior

Creating a new terminal in a fresh Windows install results in a powershell error

PS C:\dev\loadtest> & c:/dev/loadtest/env/Scripts/activate.ps1
& : File C:\dev\loadtest\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:3
+ & c:/dev/loadtest/env/Scripts/activate.ps1
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Expected behavior

I don’t need need to disable running scripts in able to use VS code

Steps to reproduce:

  1. Fresh install of windows
  2. install python
  3. create virtual environment
  4. try to activate it in VS Code

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 41
  • Comments: 72 (19 by maintainers)

Most upvoted comments

Hmmm… this feels like it might be a VSCode problem.

To fix the issue, you can simply open a Powershell window as administrator and type the following:

Set-ExecutionPolicy RemoteSigned

And hit enter, respond y to any prompts that appear.

I was able to work around this problem by adding the following to my VS Code settings:

"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]

Just to add to the description of the issue at hand I had same problem using Pipenv virtual environment. Pipenv version: 2018.11.14 VSCode version : 1.29.1

Worked after updating my execution policy from using the following PS cmdlet:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Here is the link to the documentation on ExecutionPolicy from Microsoft

Cheers

OPEN POWERSHELL IN ADMINISTRATER MODE AND USE THIS COMMANDS REFERENCE : https://docs.microsoft.com/da-dk/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-5.1

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

`Try the new cross-platform PowerShell https://aka.ms/pscore6`

PS C:\WINDOWS\system32> cd..
PS C:\WINDOWS> cd..
PS C:\> cd d:
PS D:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
PS D:\> Get-ExecutionPolicy
RemoteSigned
PS D:\> Get-ExecutionPolicy -LIST

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine       Undefined


PS D:\>

I hope your problem is solved
if not then just follow the reference link

FYI on the latest versions of windows you can also open “developer settings” (start -> search developer settings)" and check this box on the bottom of the page: image

OPEN POWERSHELL IN ADMINISTRATER MODE AND USE THIS COMMANDS REFERENCE : https://docs.microsoft.com/da-dk/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-5.1

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

`Try the new cross-platform PowerShell https://aka.ms/pscore6`

PS C:\WINDOWS\system32> cd..
PS C:\WINDOWS> cd..
PS C:\> cd d:
PS D:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
PS D:\> Get-ExecutionPolicy
RemoteSigned
PS D:\> Get-ExecutionPolicy -LIST

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine       Undefined


PS D:\>

I hope your problem is solved if not then just follow the reference link

This Work on my windows 10

Installing the PowerShell extension on VSCode fixed the problem for me.

It might also be easier for us to contribute upstream to Python an Activate.ps1 that isn’t automatically generated and thus can be signed as necessary.

Watch python/cpython#14967, may even make it into 3.8.

Yeah I’m also seeing the same issue 😅 the default execution policy in PowerShell for client pcs is restricted, which doesn’t allow even signed scripts. Changing it to RemoteSigned fixes the issue for Python 3.8 without compromising security that much.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

This solution worked for me :

  1. Start VSCode as Administrator
  2. Open the Integrated Terminal
  3. Type

> Set-ExecutionPolicy –ExecutionPolicy RemoteSigned

and that’s it !

As somebody that’s new python this has been a bit of a pain. Thanks to this thread I sorted it by switching to cmd as my default shell and its all working as expected now. From a UX point of view I would expect the built in terminal to be using the same interpreter that I have selected from the vscode GUI.

P.S. any reason why I would want to use powershell over cmd moving forward?

thanks

You can simply open a Powershell window as administrator

type the following:

Set-ExecutionPolicy RemoteSigned

press enter, and then press y to any prompts that appear. And its done.

For windows this solution worked for me :

  • Open the Integrated Terminal (VSCode)
  • Type Set-ExecutionPolicy –ExecutionPolicy RemoteSigned

OPEN POWERSHELL IN ADMINISTRATER MODE AND USE THIS COMMANDS REFERENCE : https://docs.microsoft.com/da-dk/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-5.1

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

`Try the new cross-platform PowerShell https://aka.ms/pscore6`

PS C:\WINDOWS\system32> cd..
PS C:\WINDOWS> cd..
PS C:\> cd d:
PS D:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
PS D:\> Get-ExecutionPolicy
RemoteSigned
PS D:\> Get-ExecutionPolicy -LIST

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine       Undefined


PS D:\>

I hope your problem is solved if not then just follow the reference link

This actually worked. Thanks

Just opened PowerShell and typed: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

@qubitron @brettcannon Agreed on the docs part, but what about prompting to change the execution policy for PowerShell in VS Code, and if “yes” is selected then we add “terminal.integrated.shellArgs.windows”: [“-ExecutionPolicy”, “Bypass”] to settings.json?

You can relax the execution policies, by taking these simple steps.

Press the windows-button on your keyboard. Type ‘PowerShell’ Right-click Windows PowerShell Click Run as Administrator Run the following command and confirm with ‘Y’

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

We have changed the execution policies to allow local and remote signed scripts and we have done this for the scope of your machine. Conclusion: You can now run PowerShell scripts on your computer.

Great success!

(It worked for me)

The solution for this is not that vast or difficult. You only need to paste "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"] in your settings.json file. See here how to do this: Solution for Activate.ps1 cannot be loaded in Visual Studio code.

you just need change default terminal to CMD or Git Bash

Activate.ps1 will be signed starting in Python 3.8, so I don’t know if we want to go down the route of trying to hack around this if it will be solved implicitly over time and won’t have us trying to do more in the terminal which we know from experience is extremely finicky.

@luabud we could, but I’m a little hesitant since it’s a security change so we would have to be very careful and clear on the messaging of what they are opting into having us do on their behalf.

Well, we could do the following:

  • If shell is powershell
  • If batch file exists, then shell into CMD and execute bath file
  • Else if, powershell script exists, excute powershell script, with the prompts to elevate if necessary.

I.e. where possible use batch files. This would be better as we wouldn’t have to display prompts for powershell script exec elevation…

can we inject some powershell code prior to running activate.ps1 when we launch a python terminal?

Yes we can. Changing the label, as this isn’t a bug. we’re trying to improve the UX for users of the extension.

@d3r3kk Your original suggestion works, changing the policy will work, however @qubitron doesn’t want to have to do it manually. Basically we should do what ever we can to remove/reduce such road blocks.