---#N: 2022/10/11 #U: future #S: writing #T: home #---
install pyenv on ubuntu¶
安装完 pyenv 后,请继续用 pyenv 安装 Python 主版本和虚拟环境。(see: pyenv)
repo¶
super guide¶
同时写了多个平台的安装使用方法 (centos, ubuntu, mac)
rules¶
Install Python in your user space ??? 指 home 吗? 安装位置默认为
~/.config/pyenvInstall multiple versions of Python
Specify the exact Python version you want
Switch between the installed versions
steps and notes¶
1. install build dependencies¶
如果依赖没有安装完整,在安装新版本的 python 时,会 build 不成功。
https://github.com/pyenv/pyenv/wiki#suggested-build-environment
找到 Ubuntu/Debian/Mint:
sudo apt-get update
sudo apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
安装 Git:
sudo apt update
sudo apt install git
解决 github 连不上的问题
如果没有解决被墙挡住的问题,后面会出现 Failed to connect to raw.githubusercontent.com ... , 表示 githubcontent.com 连接不上。此时在 /etc/hosts 文件中加入以下内容:
185.199.108.133 raw.githubusercontent.com
185.199.111.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
以上的 hosts 文件如果失效,需要找新的解析表,见 github 打不开(connect-github)。 同时添加 github.com 的解析表:
140.82.121.4 github.com
140.82.113.3 github.com
140.82.114.4 github.com
140.82.121.3 github.com
140.82.113.3 github.com
2. install pyenv¶
如果没有墙的问题,执行下面的脚本,即可下载安装:
curl https://pyenv.run | bash
但是,前面虽然添加了 dns,但是从 github 克隆代码仍可能失败。例如:`` Failed to git clone https://github.com/pyenv/pyenv-virtualenv.git``
此时要么回到 curl 重装,要么手工接着把后面的步骤装完。因为重装仍可能 git clone 失败(安装脚本没有重试机制)。下载打开 curl 回来的文件,在最后看到这几行:
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-installer.git" "${PYENV_ROOT}/plugins/pyenv-installer" "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"
checkout "${GITHUB}/pyenv/pyenv-which-ext.git" "${PYENV_ROOT}/plugins/pyenv-which-ext" "master"
(倒数第2行)有 pyenv-virtualenv.git,意味着从这一行开始后面的代码都没有安装成功。手工克隆最后几个没有成功的他库克隆到本地 .pyenv/plugins 目录
cd ~/.pyenv/plugins
git clone https://github.com/pyenv/pyenv-installer.git/
git clone https://github.com/pyenv/pyenv-update.git/
git clone https://github.com/pyenv/pyenv-virtualenv.git/
git clone https://github.com/pyenv/pyenv-which-ext.git/
注意:
如果哪个仓库克隆失败,就反复运行,多试几次,直到成功;
克隆完之后,
.pyenv/plugins/目录下5个仓库,一个 python-build 目录。
3. 添加环境变量¶
a) 运行完中断了的安装脚本¶
因为自动安装被中断了,所以在克隆完代码后,还需要完成所需设置。将 curl pyenv.run 回来的脚本文件 (实际是重定向到另一个文件)下载回本地保存,将后面的几行代码复制出来,保存为 post_install.run。 并在首行加入一行 export PYENV_ROOT="${HOME}/.pyenv", 得到内容如下的设置脚本:
export PYENV_ROOT="${HOME}/.pyenv"
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
运行:
$ post_install.run
b) 添加环境变量¶
运行的结果就是提示如何添加环境变量。按提示要求在 ~/.bashrc, ~/.bash_profile, ~/.profile 等三个文件中添加好环境变量:
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
c) 检验是否安装成功¶
再次运行:
$ post_install.run
再次运行该脚本,就没有任何提示了。
Reference¶
以上要添加的内容在下面的文档有说明 Managing Multiple Python Versions With pyenv – Real Python