环境介绍

我目前的环境:Powershell 7.5.0,装了 Oh my posh,在C:\Users\YourName\Documents\PowerShell,下已经有Microsoft.PowerShell_profile.ps1,这是之前为了配置 Oh my posh 创建的。

遇到的问题

装好 conda 之后,发现 cmd、windows Powershell 均可以正常使用 conda 命令,而 Powershell 7 则不行,会有如下报错:


❯ conda usage: conda-script.py [-h] [-v] [--no-plugins] [-V] COMMAND ... conda-script.py: error: argument COMMAND: invalid choice: '' (choose from 'activate', 'clean', 'commands', 'compare', 'config', 'create', 'deactivate', 'env', 'export', 'info', 'init', 'install', 'list', 'notices', 'package', 'build', 'content-trust', 'convert', 'debug', 'develop', 'doctor', 'index', 'inspect', 'metapackage', 'render', 'repoquery', 'skeleton', 'token', 'server', 'pack', 'repo', 'remove', 'uninstall', 'rename', 'run', 'search', 'update', 'upgrade')

于是先询问 GPT ,反复询问之后有一个办法可以解决问题。

在 Powershell 7 内运行 & "C:\Users\YourName\anaconda3\shell\condabin\conda-hook.ps1" 后,就可以正常使用 conda 相关命令了。

但这样做还有一个问题,那就是当前环境并不会显示在行头(也就是开头显不显示 base 之类的...)

于是继续上网查资料,找到这个博客:https://hackf5.medium.com/how-to-enable-anaconda-in-powershell-7-on-windows-394ba62c3f9c

根据他所说,需要在 C:\Users\YourName\Documents\WindowsPowerShell 下找到 profile.ps1 。我这边是如下内容:

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "C:\Users\bored\anaconda3\Scripts\conda.exe") {
    (& "C:\Users\bored\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#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;
            }
        }
    }
}

Life is a Rainmeter