profile
viewpoint

Ask questionsActivating 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

Create python terminal 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
microsoft/vscode-python

Answer questions DonJayamanne

Not sure whether we've explored this. Here's what I tried and it works, basically bypass the restriction temporarily, then restore it (applies only to current session):

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process ; & ./venv/scripts/activate.ps1 ; Set-ExecutionPolicy -ExecutionPolicy Default -Scope Process

  • 3 commands chained
  • First disable for current process (only current process)
  • Next perform activation (based on docs, if a prompt is necessary, it will be displayed to the user - great)
  • Finally, after env activation, we restore the execution policy

Pros:

  • We can optionally add a prompt (yes/no) using the -Confirm flag, giving the user more control.

Set-ExecutionPolicy -Confirm -ExecutionPolicy Unrestricted -Scope Process ; & ./venv/scripts/activate.ps1 ; Set-ExecutionPolicy -ExecutionPolicy Default -Scope Process

  • It works (unless there's something wrong I can't see)
  • We bypass security only for execution of the activation scripts
  • Changes to policies are scoped to the current process and safely restored (we could make this a script if required)
  • Using the policy of Unrestriced will result in a prompt being displayed to user for downloaded scripts - i.e. letting user decide - I think thats a good thing).
  • We can determine the current execution level as well (VS Code have recently added the ability to create terminals that aren't visible to users. This way we can create a terminal and run scripts to determine the execution level - if we want to do so)

Finally: I tried and works on a vanilla Windows 10 setup (had to install one today to test something else).

  • Execution policy is Restricted before and after the above scripts are executed
  • User is prompted (Yes/No) when using the -Confirm flag
  • Works as expected
useful!
source:https://uonfu.com/
answerer
Don Jayamanne DonJayamanne Software engineer with a soft spot for .NET and Node.js. Loves tinkering with Arduino and Raspberry Pi. Lives in a cave.
Github User Rank List