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

相关推荐
砍材农夫11 天前
python环境|conda安装和使用(1)
开发语言·后端·python·conda
匆匆整棹还11 天前
mamba的安装和版本对应
conda
2601_9618752412 天前
花生十三资料1200题|题库|刷题
conda·pytest·pillow·pip·web3.py·ipython·gunicorn
没有钱的钱仔13 天前
自动创建conda虚拟环境,并安装依赖包
conda
砍材农夫14 天前
python环境|pip|uv|venv|Conda区别
后端·python·conda·pip·uv
pixelpilot115 天前
Conda:跨平台的二进制包管理器
其他·conda
lg_cool_15 天前
使用conda管理python运行环境并关联vscode
vscode·python·conda
javajenius15 天前
Pixi:用 Rust 重写 Conda 体验的包管理工具
开发语言·其他·rust·conda
小白弄潮儿15 天前
Conda 使用入门指南
conda
DFT计算杂谈16 天前
WannierTools输入文件wt.in一键批量生成脚本
java·前端·chrome·python·算法·conda