使用 scoop 安装的 miniconda3,在 PowerShell 中进行了 conda init
初始化(注意:不是 Windows PowerShell
,见下图,PowerShell
是在 Microsoft Store
中安装的,link)
问题:启动速度太慢,大概需要 4 秒
于是进行优化,原理:不要在 profile 里直接运行 conda 的 heavy hook,而是定义一个名为 conda 的代理函数。这个代理函数第一次被调用时,会执行真正的初始化(替换自己为真实的命令或导入 hook),然后将最初调用的参数传递给 conda。
1、注释掉原来的初始化脚本
位置:"C:\Users\XXX\Documents\PowerShell\profile.ps1"
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
##If (Test-Path "C:\Users\XXX\scoop\apps\miniconda3\current\Scripts\conda.exe") {
## (& "C:\Users\XXX\scoop\apps\miniconda3\current\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
##}
#endregion
2、添加新内容
位置:"C:\Users\XXX\Documents\PowerShell\Microsoft.PowerShell_profile.ps1"
#region conda initialize (deferred via proxy)
$condaExe = 'C:\Users\XXX\scoop\apps\miniconda3\current\Scripts\conda.exe'if (Test-Path $condaExe) {function Invoke-CondaInit {param([Parameter(ValueFromRemainingArguments=$true)]$Args)if (-not (Get-Variable -Name '__CondaInitialized' -Scope Script -ErrorAction SilentlyContinue)) {Set-Variable -Name '__CondaInitialized' -Value $true -Scope Scripttry {$hook = & $condaExe 'shell.powershell' 'hook'if ($LASTEXITCODE -ne 0 -or -not $hook) {throw "conda hook failed"}$hook | Out-String | Where-Object { $_ } | Invoke-ExpressionSet-Item -Path Function:conda -Value {param([Parameter(ValueFromRemainingArguments=$true)]$Args)& $condaExe @Args}}catch {Write-Verbose "Conda lazy init failed: $_"Set-Item -Path Function:conda -Value {param([Parameter(ValueFromRemainingArguments=$true)]$Args)& $condaExe @Args}}}& conda @Args}Set-Item -Path Function:conda -Value {param([Parameter(ValueFromRemainingArguments=$true)]$Args)Invoke-CondaInit @Args}
}
#endregion
其他:俺没有为 Windows Powershell
配置 conda 命令