PowerShell 实现 conda 懒加载

问题

执行命令conda init powershell会在 profile.ps1中添加conda初始化的命令。
即使用户不需要用到conda,也会初始化conda环境,拖慢PowerShell的启动速度。

解决方案

本文展示了如何实现conda的懒加载,默认不加载conda环境,只有在用户执行conda命令时才加载。

(1) Path环境变量添加conda路径

  • 添加conda3的本地路径:D:\code\miniconda3
  • 添加conda3的脚本路径:D:\code\miniconda3\Scripts
    在这里插入图片描述

(2) 注销conda初始化命令

  • 进入文件夹:C:\Users<user_name>\Documents\WindowsPowerShell
  • 编辑profile.ps1文件,注释或删除conda初始化代码
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
# If (Test-Path "D:\code\miniconda3\Scripts\conda.exe") {
#    (& "D:\code\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
# }
#endregion

(3) 封装conda命令,实现懒加载

  • 进入文件夹:C:\Users<user_name>\Documents\WindowsPowerShell
  • 编辑Microsoft.PowerShell_profile.ps1文件,添加代码。
function Load-Conda {
    If (Test-Path "D:\code\miniconda3\Scripts\conda.exe") {
        (& "D:\code\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
    }

    conda @args
}

Set-Alias conda Load-Conda
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值