[Python]pyenv安装

1. 安装依赖

yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel ncurses-devel libffi-devel xz-devel

2. 部署pyenv github被屏蔽

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
# 如果可以使用git
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

3. 离线安装

curl -O https://github.com/pyenv/pyenv/archive/refs/tags/v2.2.0.tar.gz
tar -zxvf pyenv-2.2.0.tar.gz

mv pyenv-2.2.0 .pyenv
mkdir .pyenv/cache

cat >>/root/.bashrc<<'EOF'
### pyenv ###
export PYENV_ROOT="$HOME/.pyenv" 
export PATH="$PYENV_ROOT/bin:$PATH"
export PATH="$PYENV_ROOT/shims:$PATH"
eval "$(pyenv init -)"
EOF

source /root/.bashrc

pyenv --version
pyenv 2.2.0

4. 查询可安装的Python版本

pyenv install --list

5. 安装Python版本(在线跟慢)

pyenv install -v 3.9.14

6. 国内源下载

它安装 Python 的流程就是先将把包下载到 ~/.pyenv/cache 目录,然后在进行安装,慢也就慢在下载的这个过程。
先手动将包下载到指定目录,在执行 pyenv install 即可。

v=3.9.14; curl -L https://npm.taobao.org/mirrors/python/$v/Python-$v.tar.xz -o ~/.pyenv/cache/Python-$v.tar.xz; pyenv install $v

安装报错
ModuleNotFoundError: No module named '_lzma'
WARNING: The Python lzma extension was not compiled. Missing the lzma lib?

yum install -y xz-devel

7. 查看已经安装的版本

[root@itop ~]# pyenv versions
* system (set by /root/.pyenv/version)
  3.6.5
  3.9.14

8. pyenv local 指定文件夹Python版本

pyenv local 3.9.14
pyenv versions
  system
  3.6.5
* 3.9.14 (set by /opt/python3914/.python-version)

9. pyenv-virtualenv-1.1.5

curl -O https://github.com/pyenv/pyenv-virtualenv/archive/refs/tags/v1.1.5.tar.gz
mv pyenv-virtualenv-1.1.5 .pyenv/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
# git可用
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
exec "$SHELL"

10. 创建项目环境

pyenv virtualenv 3.9.14 telnet_switch

11. 切换虚拟环境

 pyenv local telnet_switch
(telnet_switch) [root@Python telnet_switch]# 

12. 查看当前虚拟环境

pyenv virtualenvs

  3.9.14/envs/telnet_switch (created from /root/.pyenv/versions/3.9.14)
* telnet_switch (created from /root/.pyenv/versions/3.9.14)

14. 更换pip源

mkdir ~/.pip
cat > ~/.pip/pip.conf <<EOF
[global] 
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn
EOF

14. 升级pip

/root/.pyenv/versions/3.9.14/envs/20230403VUE3ZaixianxuexiAPI/bin/python3.9 -m pip install --upgrade pip

15. 删除当前环境

pyenv uninstall telnet_switch 

END

posted @ 2021-11-17 14:35  LeoShi2020  阅读(2518)  评论(1编辑  收藏  举报