Mac上基于pyenv管理Python多版本的最佳实践

首先声明,你可以选择使用 Homebrew 来安装pyenv。我这里主要是想和我 Linux 设备上一致,所以选择使用脚本来安装pyenv。

准备安装脚本

这个安装的脚本来源于官方的的github仓库

关于安装脚本的解读请看《pyenv 安装脚本解读》。

pyenv-installer.sh

复制代码
#!/usr/bin/env bash

set -e
[ -n "$PYENV_DEBUG" ] && set -x

if [ -z "$PYENV_ROOT" ]; then
  if [ -z "$HOME" ]; then
    printf "$0: %s\n" \
      "Either \$PYENV_ROOT or \$HOME must be set to determine the install location." \
      >&2
    exit 1
  fi
  export PYENV_ROOT="${HOME}/.pyenv"
fi

colorize() {
  if [ -t 1 ]; then printf "\e[%sm%s\e[m" "$1" "$2"
  else echo -n "$2"
  fi
}

# Checks for `.pyenv` file, and suggests to remove it for installing
if [ -d "${PYENV_ROOT}" ]; then
  { echo
    colorize 1 "WARNING"
    echo ": Can not proceed with installation. Kindly remove the '${PYENV_ROOT}' directory first."
    echo
  } >&2
    exit 1
fi

failed_checkout() {
  echo "Failed to git clone $1"
  exit -1
}

checkout() {
  [ -d "$2" ] || git -c advice.detachedHead=0 clone --branch "$3" --depth 1 "$1" "$2" || failed_checkout "$1"
}

if ! command -v git 1>/dev/null 2>&1; then
  echo "pyenv: Git is not installed, can't continue." >&2
  exit 1
fi

# Check ssh authentication if USE_SSH is present
if [ -n "${USE_SSH}" ]; then
  if ! command -v ssh 1>/dev/null 2>&1; then
    echo "pyenv: configuration USE_SSH found but ssh is not installed, can't continue." >&2
    exit 1
  fi

  # `ssh -T git@github.com' returns 1 on success
  # See https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection
  ssh -T git@github.com 1>/dev/null 2>&1 || EXIT_CODE=$?
  if [[ ${EXIT_CODE} != 1 ]]; then
      echo "pyenv: github ssh authentication failed."
      echo
      echo "In order to use the ssh connection option, you need to have an ssh key set up."
      echo "Please generate an ssh key by using ssh-keygen, or follow the instructions at the following URL for more information:"
      echo
      echo "> https://docs.github.com/en/repositories/creating-and-managing-repositories/troubleshooting-cloning-errors#check-your-ssh-access"
      echo
      echo "Once you have an ssh key set up, try running the command again."
    exit 1
  fi
fi

if [ -n "${USE_SSH}" ]; then
  GITHUB="git@github.com:"
else
  GITHUB="https://github.com/"
fi

checkout "${GITHUB}pyenv/pyenv.git"            "${PYENV_ROOT}"                           "${PYENV_GIT_TAG:-master}"
checkout "${GITHUB}pyenv/pyenv-doctor.git"     "${PYENV_ROOT}/plugins/pyenv-doctor"      "master"
checkout "${GITHUB}pyenv/pyenv-update.git"     "${PYENV_ROOT}/plugins/pyenv-update"      "master"
checkout "${GITHUB}pyenv/pyenv-virtualenv.git" "${PYENV_ROOT}/plugins/pyenv-virtualenv"  "master"

if ! command -v pyenv 1>/dev/null; then
  { echo
    colorize 1 "WARNING"
    echo ": seems you still have not added 'pyenv' to the load path."
    echo
  } >&2

  { # Without args, `init` commands print installation help
    "${PYENV_ROOT}/bin/pyenv" init || true
    "${PYENV_ROOT}/bin/pyenv" virtualenv-init || true
  } >&2
fi

执行安装脚本

复制代码
# 使用ssh链接github下载源代码
export USE_SSH=1
# 自定义安装路径
export PYENV_ROOT=~/app/pyenv
sh ./pyenv-installer.sh

配置环境变量

sh 复制代码
# vim .zshrc
# pyenv
export PYENV_ROOT=~/app/pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

一些常用的指令

sh 复制代码
source .zshrc

# 查看当前使用的 Python 版本
pyenv version

# 列出 pyenv 支持的所有版本,从 Python 2.x 到最新的 Python 版本,包括那些尚未安装的
pyenv install --list

# 安装 Python 环境 
pyenv install -v 3.10.16
# 生成虚拟环境
pyenv virtualenv 3.10.16 testenv
# pyenv versions 查看环境列表

# 查看当前的虚拟环境列表
pyenv virtualenvs

# 激活虚拟环境
pyenv activate testenv
# 退出虚拟环境
pyenv deactivate

# 另一种激活和退出的方法
# 使用 source 命令激活环境 
source ~/app/pyenv/versions/3.10.16/envs/myenv/bin/activate
# 使用 deactivate 命令退出环境
deactivate

# 删除指定的(虚拟)环境,跳过确认
pyenv uninstall -f testenv  # 删除虚拟环境

特别提醒

M芯片上Mac用pyenv老版本的Python有可能编译失败。你再安装时尽量选择同一个次版本下的比较新的版本,比如3.6.x中你选择安装3.6.153.8.x中你选择安装3.8.20,我确认过是可以正常安装的。

相关推荐
站大爷IP1 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户8356290780517 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
c8i7 小时前
python中类的基本结构、特殊属性于MRO理解
python
liwulin05067 小时前
【ESP32-CAM】HELLO WORLD
python
Doris_20238 小时前
Python条件判断语句 if、elif 、else
前端·后端·python
Doris_20238 小时前
Python 模式匹配match case
前端·后端·python
这里有鱼汤8 小时前
Python量化实盘踩坑指南:分钟K线没处理好,小心直接亏钱!
后端·python·程序员
大模型真好玩9 小时前
深入浅出LangGraph AI Agent智能体开发教程(五)—LangGraph 数据分析助手智能体项目实战
人工智能·python·mcp
测试老哥9 小时前
Selenium 使用指南
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
百锦再9 小时前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame