Python版本管理-pyenv
1- 特点
-
pyenv更侧重在python 解释器版本管理上, 比包管理更大一个层级, 使用pyenv我可以方便的下载指定版本的python解释器, pypy, anaconda等, 可以随时自由的在shell环境中本地、全局切换python解释器
-
开发的时候不需要限定某个版本的虚拟环境, 只需要在部署的时候用pyenv指定某个版本就好了
-
pyenv切换解释器版本的时候, pip和ipython以及对应的包环境都是一起切换的, 所以如果你要同时运行ipython2.x和ipython3.x多个解释器验证一些代码时就很方便
-
pyenv也可以创建好指定的虚拟环境, 但不需要指定具体目录, 自由度更高, 使用也简单
2- 安装pyenv
2.1- Github在线安装
家目录里clone项目:
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
添加~.bash_profile配置文件中追加内容:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
生效:
source ~.bash_profile
2.2- 国内源访问安装[推荐]
安装依赖包:
## centos/redhat
yum install -y make libffi-devel zlib* openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++ openssl-devel xz xz-devel sqlite* patch git
## debian/ubuntu
apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm
apt-get install libc6-dev gcc -y
gitee克隆:
$ git clone https://gitee.com/laileman/pyenv ~/.pyenv
手动生效:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
source /root/.bashrc
3- 使用
3.1- 安装需要的python版本
查看可以安装的版本:
pyenv install -l
[root@all-demo ~]# pyenv install -l
Available versions:
***
2.7.2
2.7.3
2.7.4
2.7.5
2.7.6
3.3.6
3.3.7
3.4.0
3.4-dev
3.4.1
3.4.2
3.4.3
3.4.4
3.4.5
3.4.6
3.4.7
***
在线安装指定版本:
- 安装3.7.5版本
pyenv install 3.7.6
[root@all-demo ~]# pyenv install 3.7.6
Downloading Python-3.7.6.tar.xz...
-> https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
Installing Python-3.7.6...
Installed Python-3.7.6 to /root/.pyenv/versions/3.7.6
- 切换检查
[root@all-demo ~]# pyenv versions
3.7.6
[root@all-demo ~]# pyenv local 3.7.6
[root@all-demo ~]# python3 -V
Python 3.7.6
[root@all-demo ~]# pyenv version
3.7.6 (set by /root/.python-version)
3.2- 离线安装3.7.5版本
从python官网下载源码包:
官网地址:https://www.python.org/ftp/python/
## ## 下载包上传到主机
wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tar.xz
## 安装离线包之前先检查已经安装的版本
[root@all-demo ~]# pyenv versions
Warning: no Python detected on the system
离线编译安装:
[root@all-demo ~]# mkdir -p .pyenv/versions/3.7.5
[root@all-demo ~]# tar -xf Python-3.7.5.tar.xz
[root@all-demo ~]# cd Python-3.7.5/
[root@all-demo Python-3.7.5]# ./configure --enable-optimizations --prefix=/root/.pyenv/versions/3.7.5/
config.status: creating pyconfig.h
config.status: pyconfig.h is unchanged
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
[root@all-demo Python-3.7.5]# make
[root@all-demo Python-3.7.5]# make install
查看版本:
[root@all-demo Python-3.7.5]# pyenv versions
3.7.5
切换测试:
[root@all-demo Python-3.7.5]# pyenv local 3.7.5
[root@all-demo Python-3.7.5]# py
pydoc3 pyenv python3.7-config python3-config pyvenv-3.7
pydoc3.6 python3 python3.7m pyvenv
pydoc3.7 python3.7 python3.7m-config pyvenv-3.6
[root@all-demo Python-3.7.5]# python3
Python 3.7.5 (default, Dec 12 2020, 17:40:23)
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>>
[root@all-demo Python-3.7.5]# pyenv versions
* 3.7.5 (set by /root/Python-3.7.5/.python-version)
4- 常用命令详解
设置python环境变量的几个命令
配置当前用户的系统使用的python版本
pyenv global <version>
配置当前shell的python版本,退出shell则失效
pyenv shelll <version>
取消配置的使用python shell
pyenv shell --unset
配置所在项目(目录)的python版本
pyenv local <version>
pyenv --help:
[root@localhost ~]# pyenv --help
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
uninstall Uninstall a specific Python version
version Show the current Python version(s) and its origin
--version Display the version of pyenv
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
whence List all Python versions that contain the given executable
which Display the full path to an executable
专注运维开发和开源技术!!