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

相关推荐
SoulRoar.3 天前
WSL2 + Conda 环境下MinerU 4.x GPU 服务部署与 systemd 常驻指南
conda·wsl·文档解析·mineru
benjiangliu15 天前
恩培趣味计算机视觉-01-conda环境和Python库
开发语言·python·conda
weixin_4617694019 天前
conda create 创建容器 Failed to resolve ‘repo.anaconda.com‘问题
开发语言·python·conda·repo·网络问题·无法创建
饭来_23 天前
Ubuntu 中出现 CondaToSNonInteractiveError 的解决方法
ubuntu·conda·debug
emo_2b25 天前
Pycharm配置conda解释器时遇到的问题及解决方法
linux·pycharm·conda
Είναι η κοπέλα1 个月前
llama.cpp 与 GGUF 格式:本地大模型的“裸引擎“
开发语言·人工智能·pytorch·python·conda
Είναι η κοπέλα1 个月前
显存计算与模型选择:你的显卡能跑多大的模型
人工智能·pytorch·python·开源·conda
天蓝蓝的本我1 个月前
conda日常用到的命令积累1-登录linux,安装pycharm
linux·pycharm·conda
Είναι η κοπέλα1 个月前
Python 虚拟环境:venv、conda 与 Python 版本选择全解
开发语言·python·conda
夏天拐跑了西瓜1 个月前
一文入门LangChain:从框架认知到构建你的第一个AI Agent
python·langchain·conda·agent