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:
- Fresh install of windows
- install python
- create virtual environment
- 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)
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:
And hit enter, respond
yto any prompts that appear.I was able to work around this problem by adding the following to my VS Code settings:
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:
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
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:
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.ps1that 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.
This solution worked for me :
> Set-ExecutionPolicy –ExecutionPolicy RemoteSignedand 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 RemoteSignedpress enter, and then press y to any prompts that appear. And its done.
For windows this solution worked for me :
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 LocalMachineWe 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.ps1will 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:
I.e. where possible use batch files. This would be better as we wouldn’t have to display prompts for powershell script exec elevation…
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.