简体   繁体   中英

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

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

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

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

I Also Faced the same issue solved it by using this :

Created A Virtual Environment By : virtualenv environment

After Creating Virtual Environment

I activated it by using :

source environment/bin/activate 

And It activated my Virtual Environment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM