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

相关推荐
2501_941111511 小时前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
bitbrowser2 小时前
哪些指纹浏览器支持模拟SSL指纹
网络·python·网络协议·ssl
limenga1023 小时前
TensorFlow Keras:快速搭建神经网络模型
人工智能·python·深度学习·神经网络·机器学习·tensorflow
心软小念4 小时前
用Python requests库玩转接口自动化测试!测试工程师的实战秘籍
java·开发语言·python
sanggou5 小时前
【Python爬虫】手把手教你从零开始写爬虫,小白也能轻松学会!(附完整源码)
开发语言·爬虫·python
geng_zhaoying6 小时前
在VPython中使用向量计算3D物体移动
python·3d·vpython
半tour费6 小时前
TextCNN-NPU移植与性能优化实战
python·深度学习·分类·cnn·华为云
普通网友6 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
百锦再6 小时前
第17章 模式与匹配
开发语言·后端·python·rust·django·内存·抽象
普通网友7 小时前
Python函数定义与调用:编写可重用代码的基石
jvm·数据库·python