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

跟風遠走 提交于 2019-12-04 01:20:43

问题


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.


回答1:


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



回答2:


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...




回答3:


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...



来源:https://stackoverflow.com/questions/57318343/virtualenv-v16-7-2-powershell-activate-script-you-must-source-this-script-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!