Pyenv 与 Python

Photo by Chris Ried / Unsplash

通过 pyenv 安装多版本 Python 环境,实现 Python 版本的灵活切换。

1 项目地址

pyenv/pyenv-installer: This tool is used to install pyenv and friends. (github.com)

2 安装

2.1 安装依赖环境

Common build problems · pyenv/pyenv Wiki (github.com)

Debian

apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

CentOS

yum install @development zlib-devel bzip2 bzip2-devel readline-devel sqlite \
sqlite-devel openssl-devel xz xz-devel libffi-devel findutils

2.2 安装 pyenv

curl https://pyenv.run | bash
cat >> ~/.bashrc <<EOF
export PATH="/home/admin/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
EOF
exec $SHELL
pyenv versions

2.3 安装 Python 版本

pyenv versions
pyenv install 3.9.1
pyenv global 3.9.1

使用镜像地址安装:

v=3.9.2;wget https://npm.taobao.org/mirrors/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v

v=3.8.8;wget https://npm.taobao.org/mirrors/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v

3 使用 Pyenv 创建虚拟环境

3.1 创建

pyenv virtualenv <version> <name>
pyenv virtualenv 3.9.1 test-python

3.2 激活

pyenv active <name>
pyenv active test-python

3.3 使用

pip install docker-compose
docker-compose up
IInfinity

IInfinity

大道虽简,知易行难。
CN