Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtualenv v16.7.2 powershell activate script: "You must 'source' this script: PS> . .\ENV\Scripts\activate" error

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.

like image 947
ffarhour Avatar asked Aug 01 '19 23:08

ffarhour


3 Answers

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
like image 167
Mathias R. Jessen Avatar answered Nov 11 '22 10:11

Mathias R. Jessen


Screenshot attached for reference. I've just encountered the same issue but I did the following:

  1. Create a new virtual environment;

    python -m venv directory

  2. Navigate into the newly created directory;

    cd directory

  3. Activate the virtual environment.

    .\Scripts\activate

This resolved my problem. I hope it helps...

like image 24
Marothi Codes Avatar answered Nov 11 '22 09:11

Marothi Codes


I have also faced this issue. To solve this I created a new virtualenvironment as follows:

python -m venv directory-name

To activate:

Scripts>./activate

And Now it's working fine...

like image 4
Shamim Hossen Avatar answered Nov 11 '22 10:11

Shamim Hossen