Issue
The Problem
Newest version of virtualenv (16.7.2) on python v.3.7.4 has 4 additional lines for the "activate.ps1" script, which when run on Windows10 powerhsell gives the error: You must 'source' this script: PS> . .\ENV\Scripts\activate
How do I fix this? (please note that I have read and done all that was mentioned on the other forum questions as well as the manual for virtualenv related to windows and powershell.)
Steps I took / things tried:**
I have set the execution policy to RemoteSigned (as recommended in other forums):
Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine RemoteSigned
When I want to activate virtualenv, I run .\ENV\Scripts\activate
Where the problem is
The problem is with lines 3 to 6 of the activate.ps1 script that is auto generated by virtualenv when you make a new virtual environment:
if (@($null,"Internal") -notcontains $myinvocation.commandorigin) {
Write-Host -Foreground red "You must 'source' this script: PS> . $($myinvocation.invocationname)"
exit 33
}
It seems that $myinvocation.commandorigin
is set to Runspace instead of Internal
Question
How do I fix this? Any ideas? Thanks :)))
Note that I don't want to manually adjust every auto-gen activate.ps1
file.
Solution
Let's have a look at that error message:
You must 'source' this script: PS> . .\ENV\Scripts\activate
Hmmmm... - PS>
is probably just the prompt, which leaves us with this:
. .\ENV\Scripts\activate
# ^
# |
# Check out this guy
That, the lonely .
in front of the path, that is the dot-source operator in powershell.
According to the documentation, it:
Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope.
I haven't had a look at virtualenv
, but I assume it'll want to define a number of variables and to ensure that these persist after the script has run, it needs to be run in the current scope.
So this is the literal command you have to run to fix it:
. .\ENV\Scripts\activate
Answered By - Mathias R. Jessen Answer Checked By - Mildred Charles (WPSolving Admin)