1. 准备工作
1.1 安装依赖包
yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel xz-devel
1.2. 如果没有git环境的话,还需要下载git
yum install -y git
2. 下载pyenv(git方式)
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
3. 将环境变量添加至配置文件中
[root@VM-0-12-centos ~]# cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
[root@VM-0-12-centos ~]#
4. pyenv的基本使用
4.1 查看pyenv可下载的python版本,同时测试pyenv是否安装成功
[root@VM-0-12-centos ~]# pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
2.4.0
...
[root@VM-0-12-centos ~]#
4.2 下载所需要的python版本
pyenv install 3.9.1 -v
// 3.9.1代表python的版本号
// -v 表示安装过程可视化
4.3 下载完成之后,查看所有已安装的python版本
[root@VM-0-12-centos ~]# pyenv versions
* system (set by /root/.pyenv/version)
3.6.8
3.7.2
3.8.6
3.9.1
[root@VM-0-12-centos ~]#
5. 配置虚拟环境(pyenv-virtualenv)
5.1 下载(git方式)
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
5.2 指定python版本创建虚拟环境
[root@VM-0-12-centos ~]# pyenv virtualenv 3.9.1 env391
Looking in links: /tmp/tmpzbwbypcj
Requirement already satisfied: setuptools in /root/.pyenv/versions/3.9.1/envs/env391/lib/python3.9/site-packages (49.2.1)
Requirement already satisfied: pip in /root/.pyenv/versions/3.9.1/envs/env391/lib/python3.9/site-packages (20.2.3)
[root@VM-0-12-centos ~]#
5.3 列出虚拟环境
[root@VM-0-12-centos ~]# pyenv virtualenvs
3.8.6/envs/env386 (created from /root/.pyenv/versions/3.8.6)
3.9.1/envs/env391 (created from /root/.pyenv/versions/3.9.1)
env386 (created from /root/.pyenv/versions/3.8.6)
env391 (created from /root/.pyenv/versions/3.9.1)
[root@VM-0-12-centos ~]#
5.4 进入虚拟环境并查看python版本
[root@VM-0-12-centos ~]# pyenv activate env391
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(env391) [root@VM-0-12-centos ~]# python -V
Python 3.9.1
(env391) [root@VM-0-12-centos ~]# pyenv activate env386
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(env386) [root@VM-0-12-centos ~]# python -V
Python 3.8.6
(env386) [root@VM-0-12-centos ~]#
5.5 退出虚拟环境
(env386) [root@VM-0-12-centos ~]# pyenv deactivate
[root@VM-0-12-centos ~]#