Hi fellow pythonistas, there seems to be a problem when virtualenv is used in PowerShell.
When I try to activate my environment in PowerShell like..
> env
Windows users
In Powershell:
set-executionpolicy remotesigned
Finally, Run
your_virtualenv_name\Scripts\activate.ps1
Instead of
your_virtualenv_name\Scripts\activate.bat
In CMD Just run:
your_virtualenv_name\Scripts\activate.bat
I wrote a little script to activate it.
# Don't forget to change execution policies
# Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
# More info https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7
if (Test-Path env:VIRTUAL_ENV) {
deactivate
}
$env = .\venv\Scripts\activate.ps1
# use $env to set variables, for instance $env:FLASK_APP = "main.py"
Remember to save file with PowerShell extension .ps1
.
I had that problem too! and finally found out what should we do in windows...
ok, follow these steps:
1)Type powershell in search bar of windows then right click on it and select Run as Administrator
(if you have problem in that check this)
2) Run the following command in powershell: Set-ExecutionPolicy Unrestricted
3) Rerun the activation command:.\\env\Scripts\activate.ps1
(just run the exact command! be careful about name of your environment.)
and that's it!:)
I wrote this quick little script to handle my activation and startup of a dev server.
$ep = Get-ExecutionPolicy
if ($ep -eq 'RemoteSigned') {
$root = "C:\Users\ALeven\OneDrive\!code_projects\!django_projects\"
$test = Read-Host -Prompt 'Would you like to activate the python environment? y/n'
if ($test -eq 'y') {
$activatestr = ($root + "\work_venv\Scripts\Activate.ps1")
& $activatestr
}
$test = Read-Host -Prompt 'Would you like to run the python server? y/n'
if ($test -eq 'y') {
$whichserver = Read-Host -Prompt 'Enter the name of the project.'
$path = ($root + $whichserver)
$runserverstr = ($path + "\manage.py")
python.exe $runserverstr runserver
}
} else {
Write-host "Execution Policy does not allow this script to run properly"
Write-host "If you have the proper permissions,"
Write-Host "Please close powershell,"
Write-host "then right click the powershell icon and run as administrator"
Write-host "Once in the powershell environment, execute the following:"
Write-host "Set-ExecutionPolicy RemoteSigned -Force"
}
Enjoy.
A quick work-around would be to invoke cmd and then run your activate.bat from within the cmd session. For example:
PS C:\my_cool_env\Scripts> cmd
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\my_cool_env\Scripts>activate.bat
(my_cool_env) C:\my_cool_env\Scripts>
Just 2 more suggestions:
Because it is always problematic to weaken a security policy i would advice to do it the minimal way for Powershell: invoke Powershell not as administrator but as the user who wants to use the virtualenv functions. Type "set-executionpolicy -executionpolicy unrestricted -scope currentuser". This way the policy is changed only for one user and not for the whole machine.
Secondly I would advice to download from github the sources "regisf/virtualenvwrapper-powershell". After download unpack the zip-file to a local directory and run the file "Install.ps1" inside. This will expand the Powershell profile permanently on your machine and hence enable the use of all "virtalenvwrapper-win" commands including "workon". After that you will not notice any difference in the behaviour of Powershell and the Commandshell concerning virtualenv.