pyenv使用教程

By 刘志军 , 2023-04-14, 分类: Python

pyenv

这里以centos为例

1、安装下载 pyenv

预装的一些依赖包,根据不同系统选择安装

https://github.com/pyenv/pyenv/wiki#suggested-build-environment

yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel xz-devel
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

2、更新配置

执行以下命令:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

3、重启shell

exec $SHELL

安装请参考:https://github.com/pyenv/pyenv#installation

4、pyenv使用

[root@xxxxxx .pyenv]# pyenv
pyenv 2.2.2-10-g943c5f9
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   exec        Run an executable with the selected Python version
   global      Set or show the global Python version(s)
   help        Display help for a command
   hooks       List hook scripts for a given pyenv command
   init        Configure the shell environment for pyenv
   install     Install a Python version using python-build
   local       Set or show the local application-specific Python version(s)
   prefix      Display prefix for a Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   root        Display the root directory where versions and shims are kept
   shell       Set or show the shell-specific Python version
   shims       List existing pyenv shims

查看已安装版本

pyenv versions

删除指定版本

pyenv uninstall -f saletraing

查看可安装的python版本

pyenv install -l

安装指定版本

pyenv install 3.7.12

安装时,可能出现错误

python-build: line 1736: patch: command not found

这时候需要安装一个patch

yum install patch

创建虚拟环境时,需要先下载指定的版本

5、使用 virtualenv

下载virtualenv 插件到 pyenv根目录下的plugins目录

git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc

重启shell

exec "$SHELL"

装了vritualenv 后,就可以使用pyenv结合virtualenv使用虚拟环境了。 安装参考 https://github.com/pyenv/pyenv-virtualenv

# 创建一个3.6.5版本的虚拟环境, 命名为v365env, 然后激活虚拟环境
$ pyenv virtualenv 3.6.5 v365env
$ pyenv activate v365env
# 关闭虚拟环境
$ pyenv deactivate v365env

列出当前虚拟环境

pyenv virtualenvs

常见错误

/root/.pyenv/plugins/python-build/bin/python-build: line 1646: patch: command not found

yum install patch

The Python ssl extension was not compiled. Missing the OpenSSL lib

CPPFLAGS=-I/usr/include/openssl11 \
LDFLAGS=-L/usr/lib64/openssl11 \
pyenv install -v 3.10.6

安装python慢

设置淘宝镜像

 export PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM=1
 export PYTHON_BUILD_MIRROR_URL="https://npm.taobao.org/mirrors/python/"

更多问题请参考:https://github.com/pyenv/pyenv/wiki/Common-build-problems#error-the-python-ssl-extension-was-not-compiled-missing-the-openssl-lib

pyenv 安装慢问题

命令行一键安装如下:

version="2.7.16"; echo $version; wget "https://mirrors.huaweicloud.com/python/$version/Python-$version.tar.xz" -P ~/.pyenv/cache/;pyenv install $version

关注公众号「Python之禅」,回复「1024」免费获取Python资源

python之禅