python学习之路☞1.安装 Miniforge Python 环境

python学习之路☞1.安装 Miniforge Python 环境

python学习之路☞1.安装 Miniforge Python 环境

#python,#linux,#windows,#macos,

系统环境

系统环境是 mac mini m4 原生系统环境

af5ab649831964@af5ab649831964s-Mac-mini.local 
------------------------------------------------- 
OS: macOS 15.4 24E5228e arm64 
Host: Mac16,10 
Kernel: 24.4.0 
Uptime: 4 days, 19 hours, 1 min 
Packages: 14 (brew) 
Shell: zsh 5.9 
Resolution: 1920x1200 
DE: Aqua 
WM: Quartz Compositor 
WM Theme: Blue (Dark) 
Terminal: node 
CPU: Apple M4 
GPU: Apple M4 
Memory: 3194MiB / 16384MiB 

环境管理工具

环境管理工具是 github 项目 conda-forge/miniforge 的 mambaforge 包
mamba is a CLI tool to manage conda s environments.也就是说 mamba 是命令行式的 conda 环境管理工具我打算用这个作为管理 py 环境的管理工具

Shell 终端操作安装 Miniforge 环境

# GitHub 项目 URI
URI="conda-forge/miniforge"

# 获取最新版本
VERSION=$(curl -sL "https://github.com/$URI/releases" | grep -Eo '/releases/tag/[^"]+' | awk -F'/tag/' '{print $2}' | head -n 1)
echo "Latest version: $VERSION"

# 获取操作系统和架构信息
OS=$(uname -s)
ARCH=$(uname -m)

# 映射平台到官方命名
case "$OS" in
  Linux)
    PLATFORM="Linux"
    if [[ "$ARCH" == "arm64" || "$ARCH" == "aarch64" ]]; then
      ARCH="aarch64"
    elif [[ "$ARCH" == "x86_64" ]]; then
      ARCH="x86_64"
    else
      echo "Unsupported architecture: $ARCH"
      #exit 1
    fi
    ;;
  Darwin)
    PLATFORM="MacOSX"
    if [[ "$ARCH" == "arm64" || "$ARCH" == "aarch64" ]]; then
      ARCH="arm64"
    elif [[ "$ARCH" == "x86_64" ]]; then
      ARCH="x86_64"
    else
      echo "Unsupported architecture: $ARCH"
      #exit 1
    fi
    ;;
  *)
    echo "Unsupported OS: $OS"
    #exit 1
    ;;
esac

# 输出最终平台和架构
echo "Platform: $PLATFORM"
echo "Architecture: $ARCH"

# 拼接下载链接和校验码链接
TARGET_FILE="Miniforge3-$VERSION-$PLATFORM-$ARCH.sh"
SHA256_FILE="$TARGET_FILE.sha256"
URI_DOWNLOAD="https://github.com/$URI/releases/download/$VERSION/$TARGET_FILE"
URI_SHA256="https://github.com/$URI/releases/download/$VERSION/$SHA256_FILE"
echo "Download URL: $URI_DOWNLOAD"
echo "SHA256 URL: $URI_SHA256"

# 检查文件是否存在
if [[ -f "/tmp/$TARGET_FILE" ]]; then
  echo "File already exists: /tmp/$TARGET_FILE"
  
  # 删除旧的 SHA256 文件(如果存在)
  if [[ -f "/tmp/$SHA256_FILE" ]]; then
    echo "Removing old SHA256 file: /tmp/$SHA256_FILE"
    rm -fv "/tmp/$SHA256_FILE"
  fi

  # 下载新的 SHA256 文件
  echo "Downloading SHA256 file..."
  curl -L -C - --retry 3 --retry-delay 5 --progress-bar -o "/tmp/$SHA256_FILE" "$URI_SHA256"

  # 校验文件完整性
  # shasum 校验依赖 perl 可能 linux 系统需要手动安装
  echo "Verifying file integrity for /tmp/$TARGET_FILE..."
  cd /tmp || exit 1
  if ! shasum -a 256 -c "$SHA256_FILE"; then
    echo "SHA256 checksum failed. Removing file and retrying..."
    rm -fv "/tmp/$TARGET_FILE"
  else
    echo "File integrity verified successfully."
  fi
fi

# 如果文件不存在或之前校验失败
if [[ ! -f "/tmp/$TARGET_FILE" ]]; then
  echo "Downloading file..."
  curl -L -C - --retry 3 --retry-delay 5 --progress-bar -o "/tmp/$TARGET_FILE" "$URI_DOWNLOAD"

  # 删除旧的 SHA256 文件并重新下载
  if [[ -f "/tmp/$SHA256_FILE" ]]; then
    echo "Removing old SHA256 file: /tmp/$SHA256_FILE"
    rm -fv "/tmp/$SHA256_FILE"
  fi
  echo "Downloading SHA256 file..."
  curl -L --progress-bar -o "/tmp/$SHA256_FILE" "$URI_SHA256"

  # 校验完整性
  # shasum 校验依赖 perl 可能 linux 系统需要手动安装
  echo "Verifying file integrity for /tmp/$TARGET_FILE..."
  cd /tmp || exit 1
  if ! shasum -a 256 -c "$SHA256_FILE"; then
    echo "Download failed: SHA256 checksum does not match."
    #exit 1
  else
    echo "File integrity verified successfully."
  fi
fi

# 创建安装目录
echo "Installing Miniforge..."
sudo mkdir -pv /opt/Mambaforge
sudo chmod -Rv a+x /opt/Mambaforge
OS=$(uname -s)

# 设置安装目录权限
if [[ "$OS" == "Darwin" ]]; then
  # macOS 使用 "admin" 作为用户组
  sudo chown -Rv $(whoami):admin /opt/Mambaforge
elif [[ "$OS" == "Linux" ]]; then
  # Linux 通常用户组为用户名
  sudo chown -Rv $(whoami):$(whoami) /opt/Mambaforge
else
  echo "Unsupported OS: $OS"
  #exit 1
fi

# 安装 Miniforge
bash "/tmp/$TARGET_FILE" -b -f -p /opt/Mambaforge

# 清理 Miniforge 缓存和安装包,这个命令在某些 linux 系统环境里似乎具有破坏性,先注释吧
echo "Cleaning up..."
#/opt/Mambaforge/bin/conda clean -afy
#rm -fv "/tmp/$TARGET_FILE" "/tmp/$SHA256_FILE"

# 初始化 Miniforge 环境
export PATH=/opt/Mambaforge/bin:$PATH
# 某些 linux 系统可能需要导入 Miniforge 的 lib 环境
# 如果你需要这个,就执行,之后可能还需要手动写入到 bash 或 zsh 的配置文件中,以持续生效
# 但是我不确定是不是所有的 linux 都会 lib 缺失,先注释吧
#export LD_LIBRARY_PATH=/opt/Mambaforge/lib:$LD_LIBRARY_PATH


# 获取操作系统信息
OS=$(uname -s)
# 获取当前 shell 名称
CURRENT_SHELL=$(basename "$SHELL")

echo "Detected shell: $CURRENT_SHELL"
echo "Detected OS: $OS"

case "$CURRENT_SHELL" in
  bash)
    if ! grep -q "conda initialize" "$HOME/.bashrc"; then
      echo "Initializing conda for bash..."
      conda init bash
    fi
    if [[ "$OS" == "Linux" ]]; then
      source "$HOME/.bashrc"
    elif [[ "$OS" == "Darwin" ]]; then
      source "$HOME/.bash_profile"
    else
      echo "Unsupported OS: $OS"
    fi
    ;;
  zsh)
    if ! grep -q "conda initialize" "$HOME/.zshrc"; then
      echo "Initializing conda for zsh..."
      conda init zsh
    fi
    if [[ "$OS" == "Linux" ]]; then
      source "$HOME/.zshrc"
    elif [[ "$OS" == "Darwin" ]]; then
      echo "Ensuring .zshrc is sourced from .zprofile..."
      if ! grep -q "source ~/.zshrc" "$HOME/.zprofile"; then
        echo "source ~/.zshrc" >> "$HOME/.zprofile"
      fi
      source "$HOME/.zprofile"
    else
      echo "Unsupported OS: $OS"
    fi
    ;;
  fish)
    echo "Initializing mamba for fish..."
    mamba init fish
    ;;
  *)
    echo "Unsupported shell: $CURRENT_SHELL"
    ;;
esac

# 更新 Mamba 和 Conda
mamba update -n base -c defaults mamba -y
mamba update -n base -c defaults conda -y

Miniforge项目也支持 windows powershell 下载安装,如果你有兴趣可以研究一下

PowerShell 执行策略设置禁止运行 .ps1 脚本,因此会报 SecurityError

# 检查当前执行策略,这里如果返回 Restricted 代表不允许执行脚本。
## powershell-脚本运行权限政策
### Restricted 默认的设置, 不允许任何script运行
### AllSigned 只能运行经过数字证书签名的script
### RemoteSigned 运行本地的script不需要数字签名,但是运行从网络上下载的script就必须要有数字签名
### Unrestricted 允许所有的script运行
Get-ExecutionPolicy -List
# 临时允许脚本运行 如果你只想在当前 PowerShell 会话中运行脚本,可以执行
# 这样可以在当前会话运行脚本,但不影响系统设置
Set-ExecutionPolicy Bypass -Scope Process -Force -Verbose
# 永久允许脚本运行 如果你想始终允许脚本运行,可以设置当前用户权限 RemoteSigned 
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force -Verbose
# 永久全局用户允许脚本运行,如果你想任何用户始终脚本运行,可以在管理员模式下打开 powershell 执行策略更改为 Unrestricted
set-executionpolicy Unrestricted -Force -Verbose
Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force -Verbose

0
1

GitHub 只支持 TLS 1.2 或更高版本。如果 PowerShell 使用的是较旧的 TLS 版本,可能导致请求 github.com 失败。

# 可以手动启用 TLS 1.2 解决旧版本 PowerShell 请求 github.com 失败。
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# 测试请求 github.com
Test-NetConnection -ComputerName github.com -Port 443

2

Windows PowerShell 下载尝试安装 Miniforge

# ===============================
# Install-Miniforge.ps1
# ===============================
# GitHub 项目 URI
$URI = "conda-forge/miniforge"

# 获取 GitHub Releases 页面并解析最新版本号
$releasesUrl = "https://github.com/$URI/releases"
Write-Host "Fetching latest version from $releasesUrl..."
try {
    $response = Invoke-WebRequest -Uri $releasesUrl -UseBasicParsing
} catch {
    Write-Error "Failed to fetch releases page."
    exit 1
}

# 使用正则表达式查找版本号(匹配 /releases/tag/后面的字符串)
$regex = '/releases/tag/(?<version>[^\"]+)'
$matches = [regex]::Matches($response.Content, $regex)
if ($matches.Count -eq 0) {
    Write-Error "No version information found."
    exit 1
}
$VERSION = $matches[0].Groups["version"].Value
Write-Host "Latest version: $VERSION"

# 获取操作系统和架构信息
# Windows 系统固定为 "Windows"
$OS = "Windows"
# 通过环境变量判断 CPU 架构
switch ($env:PROCESSOR_ARCHITECTURE) {
    "AMD64" { $ARCH = "x86_64" }
    # 目前官方仅支持 x86_64 架构,如果以后有支持 arm64 可以更改为 "ARM64" { $ARCH = "arm64" }
    "ARM64" { $ARCH = "x86_64" }
    default {
        Write-Error "Unsupported architecture: $($env:PROCESSOR_ARCHITECTURE)"
        exit 1
    }
}
Write-Host "Platform: $OS"
Write-Host "Architecture: $ARCH"

# 拼接下载链接和校验码链接
# Windows 下安装文件扩展名为 .exe
$TARGET_FILE = "Miniforge3-$VERSION-$OS-$ARCH.exe"
$SHA256_FILE = "$TARGET_FILE.sha256"
$URI_DOWNLOAD = "https://github.com/$URI/releases/download/$VERSION/$TARGET_FILE"
$URI_SHA256 = "https://github.com/$URI/releases/download/$VERSION/$SHA256_FILE"
Write-Host "Download URL: $URI_DOWNLOAD"
Write-Host "SHA256 URL: $URI_SHA256"

# 设置临时目录(使用 Windows 内置的 %TEMP% 文件夹)
$tempDir = $env:TEMP
$targetFilePath = Join-Path $tempDir $TARGET_FILE
$sha256FilePath = Join-Path $tempDir $SHA256_FILE

# 定义函数用于对比目标文件的 SHA256 校验值
function Verify-FileHash {
    param (
        [string]$FilePath,
        [string]$Sha256Path
    )
    Write-Host "Verifying file integrity for $FilePath..."
    $calculatedHash = (Get-FileHash -Path $FilePath -Algorithm SHA256).Hash.ToLower()
    $sha256Content = Get-Content -Path $Sha256Path -ErrorAction Stop
    # 文件中的格式通常为 "HASH  filename",提取第一个部分
    $expectedHash = $sha256Content.Split(" ")[0].Trim().ToLower()
    if ($calculatedHash -eq $expectedHash) {
        Write-Host "File integrity verified successfully."
        return $true
    } else {
        Write-Error "SHA256 checksum failed. Expected: $expectedHash, Calculated: $calculatedHash"
        return $false
    }
}

# 先查看临时目录中是否已经存在下载的安装包
if (Test-Path $targetFilePath) {
    Write-Host "File already exists: $targetFilePath"
    # 删除旧的 SHA256 文件(若存在)
    if (Test-Path $sha256FilePath) {
        Write-Host "Removing old SHA256 file: $sha256FilePath"
        Remove-Item $sha256FilePath -Force
    }
    Write-Host "Downloading SHA256 file..."
    try {
        Invoke-WebRequest -Uri $URI_SHA256 -OutFile $sha256FilePath -UseBasicParsing
    } catch {
        Write-Error "Failed to download SHA256 file."
        exit 1
    }
    if (-not (Verify-FileHash -FilePath $targetFilePath -Sha256Path $sha256FilePath)) {
        Write-Host "Removing corrupted file: $targetFilePath"
        Remove-Item $targetFilePath -Force
    }
}

# 若安装包不存在或校验失败,则重新下载
if (-not (Test-Path $targetFilePath)) {
    Write-Host "Downloading file..."
    try {
        Invoke-WebRequest -Uri $URI_DOWNLOAD -OutFile $targetFilePath -UseBasicParsing
    } catch {
        Write-Error "Failed to download file."
        exit 1
    }
    if (Test-Path $sha256FilePath) {
        Write-Host "Removing old SHA256 file: $sha256FilePath"
        Remove-Item $sha256FilePath -Force
    }
    Write-Host "Downloading SHA256 file..."
    try {
        Invoke-WebRequest -Uri $URI_SHA256 -OutFile $sha256FilePath -UseBasicParsing
    } catch {
        Write-Error "Failed to download SHA256 file."
        exit 1
    }
    if (-not (Verify-FileHash -FilePath $targetFilePath -Sha256Path $sha256FilePath)) {
        Write-Error "Download failed: SHA256 checksum does not match."
        exit 1
    }
}

# -------------------------------
# 开始安装 Miniforge
# -------------------------------
Write-Host "Installing Miniforge..."

# 设置安装目录,建议安装在用户目录下(“仅针对此用户”安装)
$installDir = Join-Path $env:USERPROFILE "Miniforge3"

# 启动静默安装:
# 通常 Miniforge Windows 安装程序支持 /S 静默参数以及 /D=安装目录参数(注意:/D 参数后不能有空格,也不要加引号)
$installerArgs = @("/S", "/D=$installDir")
Write-Host "Executing installer: $targetFilePath with arguments: $installerArgs"
try {
    Start-Process -FilePath $targetFilePath -ArgumentList $installerArgs -Wait -NoNewWindow
} catch {
    Write-Error "Installation failed."
    exit 1
}

# -------------------------------
# 安装后初始化 Conda 环境 (针对 PowerShell)
# -------------------------------
$condaExe = Join-Path $installDir "Scripts\conda.exe"
if (Test-Path $condaExe) {
    Write-Host "Initializing Conda for PowerShell..."
    & $condaExe init powershell
} else {
    Write-Warning "conda.exe not found in $installDir\Scripts. Skipping conda initialization."
}

# (可选)更新 Conda 及安装 mamba(一个更快的包管理器)
Write-Host "Updating Conda..."
& $condaExe update --yes --all

Write-Host "Installing mamba in base environment..."
& $condaExe install -n base -c conda-forge mamba --yes

Write-Host "Updating mamba and conda..."
& $condaExe run mamba update -n base -c defaults mamba --yes
& $condaExe run mamba update -n base -c defaults conda --yes

Write-Host "Miniforge installation completed. Please restart your shell to apply changes."
powershell

Mambaforge 创建 py 环境

在 shell 中使用 Mambaforge 创建最新的 py 环境

# 获取最新的 python 版本 ${PY_VERSION}
mamba repoquery search python --channel conda-forge
PY_VERSION=$(mamba repoquery search python --channel conda-forge | grep "python" | awk '{print $2}' | sort -V | tail -n 1)
echo $PY_VERSION

# mamba 安装 python ${PY_VERSION} 版本并将环境命名为 py${PY_VERSION}
mamba create -n py${PY_VERSION} python=${PY_VERSION} -c conda-forge -y

# conda 激活 py${PY_VERSION} 环境
conda activate py${PY_VERSION}

# 查看当前 py${PY_VERSION} 环境 Python 版本
python -V
# Python 版本回显如下说明部署 Python 环境成功
# Python Python 3.13.2

# 查看当前 py${PY_VERSION} 环境 pip 版本
pip -V
# pip 版本回显如下说明部署 Python 环境成功
# pip 25.0.1 from /opt/Mambaforge/envs/py3.13.2/lib/python3.13/site-packages/pip (python 3.13)

3

在 powershell 中使用 Mambaforge 创建最新的 py 环境

# ===============================
# CreateLatestPyEnv.ps1
# ===============================
# 查询 conda-forge 频道中可用的 python 包版本
# 获取预览最新的 python 版本
mamba repoquery search python --channel conda-forge

# 不知道为什么这个检索过程在 windows 上极其缓慢
Write-Host "查询可用的 Python 版本信息(使用 mamba repoquery)..."
$mambaOutput = & mamba repoquery search python --channel conda-forge

if (-not $mambaOutput) {
    Write-Error "未能从 mamba 获取有效的搜索结果。请检查网络或 mamba 配置。"
    exit 1
}

# 从搜索结果中提取版本信息
# 假设输出中的每一行类似于:"    python   3.13.2   ...",我们取第三个字段
$versions = @()
foreach ($line in $mambaOutput) {
    if ($line -match "python") {
        $fields = $line -split "\s+"
        if ($fields.Length -ge 2) {
            $versions += $fields[2]
        }
    }
}

if ($versions.Count -eq 0) {
    Write-Error "未能提取到任何可用的 Python 版本信息。"
    exit 1
}

# 对版本进行排序并取最后一个,即最新版本
# 方法1:直接遍历全部版本,但是无法过滤含有字符串的版本
#$PY_VERSION = $versions | Sort-Object -Unique -Version | Select-Object -Last 1

# 方法2:如果想保留所有版本,可以尝试使用 Try/Catch 对每个版本进行转换,
# 并仅对能转换的版本排序,这里我们额外输出那些无法转换的版本:
$parseableVersions = @()
foreach ($ver in $versions) {
    try {
        $v = [version]$ver
        $parseableVersions += $ver
    }
    catch {
        Write-Host "跳过不能转换为 [version] 的版本:$ver"
    }
}

if ($parseableVersions.Count -eq 0) {
    Write-Error "没有找到可转换为 [version] 的版本。"
} else {
    $PY_VERSION = ($parseableVersions | Sort-Object -Unique -Property { [version]$_ } | Select-Object -Last 1).ToString()
    Write-Host "最新的可转换 Python 版本是:$PY_VERSION"
}

Write-Host "最新的 Python 版本: $PY_VERSION"

# 使用 mamba 创建环境
$envName = "py$PY_VERSION"
Write-Host "创建环境 '$envName' 并安装 Python $PY_VERSION..."
& mamba create -n $envName python=$PY_VERSION -c conda-forge -y

# 激活环境
Write-Host "尝试激活环境 '$envName'..."
# 注意: 在脚本中执行 'conda activate' 只影响当前子进程,不会改变调用 PowerShell 的环境。
& conda activate $envName

# 检查 Python 版本
Write-Host "当前环境中的 Python 版本:"
# 查看当前 py${PY_VERSION} 环境 Python 版本
python -V
# Python 版本回显如下说明部署 Python 环境成功
# Python Python 3.13.2

# 检查 pip 版本
Write-Host "当前环境中的 pip 版本:"
# 查看当前 py${PY_VERSION} 环境 pip 版本
pip -V
# pip 版本回显如下说明部署 Python 环境成功
# pip 25.0.1 from /opt/Mambaforge/envs/py3.13.2/lib/python3.13/site-packages/pip (python 3.13)

Write-Host "Python 环境部署成功!"

接下来可以进入 python 解释器命令行模式执行输出 hello world

python
print('Hello World!')
# 打印出 Hello World! 
# 最后退出 python 命令行模式
exit()

4

卸载 Miniforge 环境

当你不需要这个环境,官方也提供了 shell 的卸载方法

# 检测当前 shell
CURRENT_SHELL=$(basename "$SHELL")
OS=$(uname -s)
echo "Detected shell: $CURRENT_SHELL"
echo "Detected OS: $OS"

# 反初始化 conda
case "$CURRENT_SHELL" in
  bash)
    echo "Reversing conda initialization for bash..."
    conda init --reverse bash
    
    if [[ "$OS" == "Linux" ]]; then
      source ~/.bashrc
    elif [[ "$OS" == "Darwin" ]]; then
      source ~/.bash_profile
    fi
    ;;
  zsh)
    echo "Reversing conda initialization for zsh..."
    conda init --reverse zsh
    source ~/.zshrc
    ;;
  *)
    echo "Unsupported shell: $CURRENT_SHELL"
    ;;
esac

# 删除 Mambaforge 基本环境文件夹
CONDA_BASE_ENVIRONMENT=$(conda info --base)
echo "Next command will delete ${CONDA_BASE_ENVIRONMENT} and all its contents."
echo "Are you sure you want to proceed? This action cannot be undone! (y/N): "
read confirm
if [[ "$confirm" == "y" || "$confirm" == "Y" ]]; then
  sudo rm -frv "${CONDA_BASE_ENVIRONMENT}"
else
  echo "Deletion aborted."
fi

# 删除遗留的全局配置文件
if [[ -f "${HOME}/.condarc" ]]; then
  echo "Removing ${HOME}/.condarc"
  rm -fv "${HOME}/.condarc"
fi

if [[ -d "${HOME}/.conda" ]]; then
  echo "Removing ${HOME}/.conda and its contents"
  rm -frv "${HOME}/.conda"
fi

echo "Uninstallation complete"

powershell 卸载方法,模仿官方 shell 的卸载方法

# ===============================
# Uninstall-Miniforge_CommandLine.ps1
# ===============================

# 1. 获取当前 Miniforge 的安装目录(base 环境路径)
$CondaBase = (& conda info --base).Trim()
if ([string]::IsNullOrWhiteSpace($CondaBase)) {
    Write-Error "无法获取 conda base 环境路径,请确认 conda 已经在 PATH 中。"
    exit 1
}
Write-Host "Detected Miniforge base installation at: $CondaBase"

# 2. 反初始化 conda(针对 PowerShell)
Write-Host "Reversing conda initialization for PowerShell..."
try {
    conda init --reverse powershell
    Write-Host "conda initialization reversed."
}
catch {
    Write-Warning "反初始化 conda 出现问题:$_"
}

# 3. 尝试调用内置卸载程序(如果存在)
# 在安装目录中查找文件名匹配 Uninstall*.exe 的卸载程序
$uninstallExe = Get-ChildItem -Path $CondaBase -Filter "Uninstall*.exe" -File | Select-Object -First 1

if ($uninstallExe) {
    Write-Host "找到卸载程序:$($uninstallExe.FullName)"
    
    # 调用卸载程序,静默执行(根据实际卸载器支持的参数调整,此处假设 /S 为静默参数)
    try {
        Start-Process -FilePath $uninstallExe.FullName -ArgumentList "/S" -Wait -NoNewWindow
        Write-Host "卸载程序执行成功。"
    }
    catch {
        Write-Warning "卸载程序执行失败:$_"
    }
}
else {
    Write-Warning "未找到匹配“Uninstall*.exe”的文件,可能需要手动卸载。"
}


# 4. 删除 Miniforge 安装目录(提示确认后删除)
Write-Host "`n接下来将删除以下目录及其所有内容:"
Write-Host "  $CondaBase"
$confirm = Read-Host "确定要删除这个目录吗?这将不可逆, 输入 y 确认:"
if ($confirm -match '^[Yy]$') {
    try {
        Remove-Item -Path $CondaBase -Recurse -Force -Verbose
        Write-Host "Miniforge 安装目录已删除。"
    }
    catch {
        Write-Error "删除安装目录失败:$_"
    }
} else {
    Write-Host "删除操作已取消。"
}

# 5. 删除遗留的全局配置文件
$condarcPath = Join-Path $env:USERPROFILE ".condarc"
if (Test-Path $condarcPath) {
    Write-Host "Removing file: $condarcPath"
    try {
        Remove-Item -Path $condarcPath -Force -Verbose
    }
    catch {
        Write-Warning "删除 .condarc 失败:$_"
    }
}

$condaDirPath = Join-Path $env:USERPROFILE ".conda"
if (Test-Path $condaDirPath) {
    Write-Host "Removing directory: $condaDirPath"
    try {
        Remove-Item -Path $condaDirPath -Recurse -Force -Verbose
    }
    catch {
        Write-Warning "删除 .conda 目录失败:$_"
    }
}

Write-Host "`nMiniforge 卸载完成!如果你已使用卸载程序,此方法将进行最后的清理。"

参考

Releases · conda-forge/miniforge
Mamba User Guide — documentation

Comments