virtualenv v16.7.2 powershell 激活脚本:"You must 'source' this script: PS> . .\ENV\Scripts\activate" 错误

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

问题

python v.3.7.4 上的最新版本的 virtualenv (16.7.2) 有 4 行额外的“activate.ps1”脚本,当 运行 Windows10 powerhsell 报错:You must 'source' this script: PS> . .\ENV\Scripts\activate 我该如何解决? (请注意,我已经阅读并完成了其他论坛问题中提到的所有内容以及与 windows 和 powershell 相关的 virtualenv 手册。)

我采取的步骤/尝试过的事情:**

我已将执行策略设置为 RemoteSigned(按照其他论坛的建议):

Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine    RemoteSigned

当我想激活virtualenv时,我运行 .\ENV\Scripts\activate

问题出在哪里

问题出在激活的第 3 到 6 行。ps1 创建新虚拟环境时由 virtualenv 自动生成的脚本:

if (@($null,"Internal") -notcontains $myinvocation.commandorigin) {
    Write-Host -Foreground red "You must 'source' this script: PS> . $($myinvocation.invocationname)"
    exit 33
}

似乎 $myinvocation.commandorigin 设置为 Runspace 而不是 Internal

问题

我该如何解决这个问题?有任何想法吗?谢谢 :))) 请注意,我不想手动调整每个自动生成 activate.ps1 文件。

我也遇到过这个问题。为了解决这个问题,我创建了一个新的虚拟环境,如下所示:

python -m venv directory-name

激活:

Scripts>./activate

现在一切正常...

Screenshot attached for reference. 我刚刚遇到了同样的问题,但我做了以下操作:

  1. 创建新的虚拟环境;

    python -m venv directory

  2. 导航到新创建的目录;

    cd directory

  3. 激活虚拟环境。

    .\Scripts\activate

这解决了我的问题。希望对您有所帮助...

让我们看一下该错误消息:

You must 'source' this script: PS> . .\ENV\Scripts\activate

Hmmmm... - PS> 可能只是提示,这给我们留下了这个:

  . .\ENV\Scripts\activate
# ^
# |
# Check out this guy

那个,路径前面那个孤独的.,就是powershell中的dot-source operator

根据the documentation,它:

Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope.

我还没有看过 virtualenv,但我假设它会想要定义一些变量并确保这些变量在脚本具有 运行 之后仍然存在,它需要在当前范围内 运行。

所以这是 文字 命令,您必须 运行 修复它:

. .\ENV\Scripts\activate

我也遇到了同样的问题,用这个解决了:

创建了一个虚拟环境: 虚拟环境

创建虚拟环境后

我使用以下方法激活它:

source environment/bin/activate 

它激活了我的虚拟环境