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

相关推荐
web135085886352 小时前
Python大数据可视化:基于python的电影天堂数据可视化_django+hive
python·信息可视化·django
东方芷兰2 小时前
伯克利 CS61A 课堂笔记 11 —— Mutability
笔记·python
不会Hello World的小苗5 小时前
Java——列表(List)
java·python·list
m0_748235957 小时前
Python大数据可视化:基于Python的王者荣耀战队的数据分析系统设计与实现_flask+hadoop+spider
hadoop·python·flask
Dyan_csdn7 小时前
【Python项目】基于Python的Web漏洞挖掘系统
网络·python·安全·web安全
Minner-Scrapy7 小时前
DApp 开发入门指南
开发语言·python·web app
&小刘要学习&8 小时前
anaconda不显示jupyter了?
python·jupyter
jerry-898 小时前
jupyterhub_config配置文件内容
python
奔跑吧邓邓子8 小时前
【Python爬虫(36)】深挖多进程爬虫性能优化:从通信到负载均衡
开发语言·爬虫·python·性能优化·负载均衡·多进程
学长学姐我该怎么办8 小时前
年前集训总结python
python