解决 Powershell 7 集成 Conda 报错:conda usage: conda-script.py...


[{“source”:{“position”:6,“lines”:[“#endregion

再对比C:\\Users\\YourName\\Documents\\PowerShell 下的 Microsoft.PowerShell_profile.ps1 ,并没有这一段。但同一个目录下也存在一个profile.ps1 ,那个里面是有这一段的。像这样:

于是我把那串 conda 相关的内容复制到Microsoft.PowerShell_profile.ps1 中,然后再次打开 powershell ,结果并没有改变。

解决问题

现在Microsoft.PowerShell_profile.ps1profile.ps1 中的内容都是一样的,我尝试把Microsoft.PowerShell_profile.ps1 删掉之后,再打开 PowerShell ,猜测它会被迫加载profile.ps1 的配置(即使两个文件的内容都是一样的)。

但这次结果就不一样了,conda 正常能用,并且行头显示了当前环境。

第一次的时候这个 Powershell 还会偷偷再创建一个Microsoft.PowerShell_profile.ps1 ,但我重启终端并再删除一次这个文件之后,它就不会自己创建了。

总结

微软这个程序不知道怎么写的,明明两个文件的内容都一样的,Microsoft.PowerShell_profile.ps1profile.ps1 的效果就是不一样。

感觉profile.ps1 还能用完全是因为微软的仁慈,之前的旧版 Windows Powershell 的相关代码不愿意删除,还保留着老版本的配置文件兼容性。以后还是尽量用 profile.ps1 吧。

”]},“target”:{“position”:6,“lines”:[“#endregion

再对比C:\\Users\\YourName\\Documents\\PowerShell 下的 Microsoft.PowerShell_profile.ps1 ,并没有这一段。但同一个目录下也存在一个profile.ps1 ,那个里面是有这一段的。像这样:

于是我把那串 conda 相关的内容复制到Microsoft.PowerShell_profile.ps1 中,然后再次打开 powershell ,结果并没有改变。

解决问题

现在Microsoft.PowerShell_profile.ps1profile.ps1 中的内容都是一样的,我尝试把Microsoft.PowerShell_profile.ps1 删掉之后,再打开 PowerShell ,猜测它会被迫加载profile.ps1 的配置(即使两个文件的内容都是一样的)。

但这次结果就不一样了,conda 正常能用,并且行头显示了当前环境。

第一次的时候这个 Powershell 还会偷偷再创建一个Microsoft.PowerShell_profile.ps1 ,但我重启终端并再删除一次这个文件之后,它就不会自己创建了。

微软这个程序不知道怎么写的,明明两个文件的内容都一样的,Microsoft.PowerShell_profile.ps1profile.ps1 的效果就是不一样。

感觉profile.ps1 还能用完全是因为微软的仁慈,之前的旧版 Windows Powershell 的相关代码不愿意删除,还保留着老版本的配置文件兼容性。以后还是尽量用 profile.ps1 吧。

问题复发+本质解决

当我重启电脑之后,conda 又报错,但行头会显示环境,像这样:

经过另外一番搜索,发现有人上周才提出了issue:https://github.com/conda/conda/issues/14537

还有另外几个issue:https://github.com/conda/conda/issues/14292https://github.com/conda/conda/pull/14517

然后就被修复了!原来是 Bug,瞎折腾了

暂时的解决办法是按下面的做,或者直接更新 conda install conda=25.1.1

我像这样编辑了 conda.psm1,它工作正常:

function Invoke-Conda() {","    # Don't use any explicit args here, we'll use $args and tab completion","    # so that we can capture everything, INCLUDING short options (e.g. -n).","    if ($Args.Count -eq 0) {","        # No args, just call the underlying conda executable.","        & $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA;","    }","    else {","        $Command = $Args[0];","        if ($Args.Count -ge 2) {","            $OtherArgs = $Args[1..($Args.Count - 1)];","        } else {","            $OtherArgs = @();","        }","        switch ($Command) {","            \"activate\" {","                Enter-CondaEnvironment @OtherArgs;","                $Env:_CE_M = $null; $Env:_CE_CONDA = $null;","            }","            \"deactivate\" {","                Exit-CondaEnvironment;","                $Env:_CE_M = $null; $Env:_CE_CONDA = $null;","            }","","            default {","                # There may be a command we don't know want to handle","                # differently in the shell wrapper, pass it through","                # verbatim.","                & $Env:CONDA_EXE $Env:_CE_M $Env:_CE_CONDA $Command @OtherArgs;","            }","        }","    }","}

”]},“type”:“CHANGE”}]