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

发布于:2024-12-18 ⋅ 阅读:(33) ⋅ 点赞:(0)

在这里插入图片描述

首先声明,你可以选择使用 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

配置环境变量

# 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 -)"

一些常用的指令

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