Skip to content

iblueer/python-switch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-switch

.zshrc 中的 switch-python() 工具独立出来的 CLI,专门用于在 macOS 上在 Homebrew Python (/opt/homebrew/bin) 与官方 Python (/usr/local/bin) 之间一键切换。

特性

  • 🧠 记住最近一次选择(存储于 ~/.python-switch/choice
  • 🪄 自动在新 Shell 中调整 PATH,无需重复编辑 .zshrc
  • 🚦 python-switch status 可以随时查看当前 python3 / pip 指向及版本
  • 🧹 内置 PATH 去重逻辑,避免无限堆叠同一路径
  • ⚙️ 提供 env 子命令,可输出 export 语句给 eval 或脚本使用

安装

cd ~/GitHub
./python-switch/install.sh

安装脚本会:

  1. python-switch 复制到 ~/.local/bin/
  2. 如有需要,为 ~/.zshrc 添加 ~/.local/binPATH
  3. ~/.zshrc 追加:
    # python-switch 自动 PATH 配置
    if command -v python-switch >/dev/null 2>&1; then
      eval "$(python-switch env --quiet)"
    fi
    # python-switch 自动 PATH 配置 END
    这样每次启动 shell 时都会读取最近的选择并调整 PATH。

安装后重新加载 zsh:

source ~/.zshrc

使用

python-switch brew      # 切到 Homebrew Python (/opt/homebrew/bin)
python-switch official  # 切到 官方 Python (/usr/local/bin)
python-switch status    # 查看当前 PATH、python/pip 状态
python-switch env       # 输出 export PATH=...(可与 eval 联用)

立即生效

python-switch brew / python-switch official 会更新记忆文件。若希望立刻作用于当前 shell,可随后执行:

eval "$(python-switch env)"

或直接重新 source ~/.zshrc

与原 switch-python() 的差异

  • 逻辑完全沿用原函数:记忆最近一次选择、PATH 去重、打印当前 python3/pip 信息
  • 不再需要在 .zshrc 中维护冗长函数,只需保留安装脚本追加的几行配置
  • CLI 风格与 whatcodec 等项目保持一致,方便版本控制与复用

卸载

  1. 删除 CLI:rm ~/.local/bin/python-switch
  2. ~/.zshrc 中移除安装脚本追加的 PATH 片段
  3. (可选)删除 ~/.python-switch/

即可恢复安装前状态。

About

switch python version between brew and macos

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages