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,我确认过是可以正常安装的。

相关推荐
猿饵块12 分钟前
python--锁
java·jvm·python
星辰落满衣26 分钟前
股票实时交易数据之Python、Java等多种主流语言实例代码演示通过股票数据接口
java·开发语言·python
F_D_Z1 小时前
哈希表解Two Sum问题
python·算法·leetcode·哈希表
智算菩萨1 小时前
【实战】使用讯飞星火API和Python构建一套文本摘要UI程序
开发语言·python·ui
Groundwork Explorer1 小时前
异步框架+POLL混合方案应对ESP32 MPY多任务+TCP多连接
python·单片机
梦帮科技1 小时前
Scikit-learn特征工程实战:从数据清洗到提升模型20%准确率
人工智能·python·机器学习·数据挖掘·开源·极限编程
xqqxqxxq1 小时前
Java 集合框架之线性表(List)实现技术笔记
java·笔记·python
verbannung2 小时前
Python进阶: 元类与属性查找理解
python
想用offer打牌2 小时前
LLM参数: Temperature 与 Top-p解析
人工智能·python·llm
小智RE0-走在路上2 小时前
Python学习笔记(6)--列表,元组,字符串,序列切片
笔记·python·学习