Enabling Python Virtualenv in Windows PowerShell
- Diniz Martins
- Dec 7, 2022
- 1 min read
Virtualenv is one of the most important tools in Python developers' toolkit. Now that Virtualenv supports PowerShell natively, you can run the script which is the equivalent of venv/bin/activate in Linux.
However, you might run into the following error when you try to run the script:
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows File C:\Users\STENGE\PycharmProjects\pythonProject1\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. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess |
You may use Set-Execution Policy to allow the current user to execute scripts as follows.
Check the result of your ExecutionPolicy - here we have "restricted" and we need to change to "RemoteSigned"
PS C:\WINDOWS\system32> Get-ExecutionPolicy Restricted |
PS C:\WINDOWS\system32> Set-ExecutionPolicy RemoteSigned 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"): Yes |
After that, check if your policy changed:
PS C:\WINDOWS\system32> Get-ExecutionPolicy RemoteSigned |
That's it! You should now be able to execute Activate.ps1 and get a fully isolated Python development environment for your projects.
PS C:\Users\diniz\PycharmProjects\pythonProject1> .\venv\Scripts\activate (venv) PS C:\Users\diniz\PycharmProjects\pythonProject1> |
All good!
Comments