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

相关推荐
Echo_NGC223721 小时前
【论文解读】Attention Is All You Need —— AI 时代的“开山之作“,经典中的经典(transformer小白导读)
人工智能·python·深度学习·神经网络·机器学习·conda·transformer
何中应1 天前
Conda安装&使用
python·conda·python3.11
qq_229058012 天前
conda中安装 rdkit版本的postgresql然后在Win11中使用虚拟环境里的rdkit
数据库·postgresql·conda
独隅5 天前
Anaconda被误删后抢救手册
conda
矢志航天的阿洪5 天前
手动安装Gurobi并配置gurobipy到Python环境(Windows/Conda)
windows·python·conda
GL_Rain6 天前
conda通过environment.yml创建虚拟环境(指定路径)报错解决教程
conda
雕刻刀9 天前
linux中复制conda环境
linux·python·conda
乐园游梦记9 天前
在pycharm中添加Conda创建的openmmlab虚拟环境作为解释器
ide·pycharm·conda
贵沫末10 天前
Python——图像处理项目Conda环境搭建
开发语言·python·conda
佳xuan10 天前
wsl(linux)安装miniconda及虚拟环境
linux·人工智能·conda