virtualenv v16.7.2 powershell activate 脚本:"你必须'source'这个脚本:PS>..ENVScriptsactivate"错误

2022-01-15 00:00:00 python virtualenv windows-10 powershell

问题描述

python v.3.7.4 上最新版本的 virtualenv (16.7.2) 为activate.ps1"增加了 4 行.脚本,在 Windows10 powerhsell 上运行时会出现错误:You must 'source' this script: PS>..ENVScriptsactivate我该如何解决?(请注意,我已经阅读并完成了其他论坛问题以及与 windows 和 powershell 相关的 virtualenv 手册中提到的所有内容.)

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

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

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

当我想激活 virtualenv 时,我运行 .ENVScriptsactivate

When I want to activate virtualenv, I run .ENVScriptsactivate

问题在于创建新虚拟环境时由 virtualenv 自动生成的 activate.ps1 脚本的第 3 到 6 行:

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
}

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

It seems that $myinvocation.commandorigin is set to Runspace instead of Internal

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

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:

您必须获取"此脚本:PS>..ENVScriptsactivate

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

Hmmmm... - PS> is probably just the prompt, which leaves us with this:

  . .ENVScriptsactivate
# ^
# |
# Check out this guy

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

That, the lonely . in front of the path, that is the dot-source operator in powershell.

根据 文档,它:

在当前范围内运行脚本,以便将脚本创建的任何函数、别名和变量添加到当前范围内.

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,但我认为它需要定义一些变量并确保这些变量在脚本运行后仍然存在,它需要运行在当前范围内.

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.

所以这是你必须运行的 literal 命令来修复它:

So this is the literal command you have to run to fix it:

. .ENVScriptsactivate

相关文章