Python多版本神器-Pyenv
一、简介
使用pyenv可以在一个操作系统上使用多个python版本,自由切换,无比顺畅。它的使用不依赖于Python,是一个简单、独立的纯 shell 脚本工具。
二、Pyenv安装
1 切换到root用户
sudo su -
2 如果说没有git,请先安装git
yum install -y git
3 安装python依赖环境
yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel readline-devel
4 设置python root环境
export PYENV_ROOT=/opt/pyenv
5 安装pyenv
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 100 2099 100 2099 0 0 2861 0 --:--:-- --:--:-- --:--:-- 2863 Cloning into '/opt/pyenv'... remote: Counting objects: 584, done. remote: Compressing objects: 100% (408/408), done. remote: Total 584 (delta 267), reused 271 (delta 86), pack-reused 0 Receiving objects: 100% (584/584), 256.40 KiB | 224.00 KiB/s, done. Resolving deltas: 100% (267/267), done. Cloning into '/opt/pyenv/plugins/pyenv-doctor'... remote: Counting objects: 11, done. remote: Compressing objects: 100% (8/8), done. remote: Total 11 (delta 1), reused 6 (delta 1), pack-reused 0 Unpacking objects: 100% (11/11), done. Cloning into '/opt/pyenv/plugins/pyenv-installer'... remote: Counting objects: 17, done. remote: Compressing objects: 100% (15/15), done. remote: Total 17 (delta 2), reused 10 (delta 0), pack-reused 0 Unpacking objects: 100% (17/17), done. Cloning into '/opt/pyenv/plugins/pyenv-update'... remote: Counting objects: 7, done. remote: Compressing objects: 100% (5/5), done. remote: Total 7 (delta 1), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (7/7), done. Cloning into '/opt/pyenv/plugins/pyenv-virtualenv'... remote: Counting objects: 54, done. remote: Compressing objects: 100% (48/48), done. remote: Total 54 (delta 11), reused 16 (delta 0), pack-reused 0 Unpacking objects: 100% (54/54), done. Cloning into '/opt/pyenv/plugins/pyenv-which-ext'... remote: Counting objects: 10, done. remote: Compressing objects: 100% (6/6), done. remote: Total 10 (delta 1), reused 5 (delta 0), pack-reused 0 Unpacking objects: 100% (10/10), done. WARNING: seems you still have not added 'pyenv' to the load path. # Load pyenv automatically by adding # the following to ~/.bash_profile: export PATH="/opt/pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
6 设置pyenv的环境变量
vi /etc/profile.d/pyenv.sh export PATH="/opt/pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
7 加载新的环境变量
source /etc/profile.d/pyenv.sh
8 初始化pyenv
pyenv init - export PATH="/opt/pyenv/shims:${PATH}" export PYENV_SHELL=bash source '/root/.pyenv/libexec/../completions/pyenv.bash' command pyenv rehash 2>/dev/null pyenv() { local command command="$1" if [ "$#" -gt 0 ]; then shift fi case "$command" in activate|deactivate|rehash|shell) eval "$(pyenv "sh-$command" "$@")";; *) command pyenv "$command" "$@";; esac }
9 查看pyenv常用的用法
pyenv help Usage: pyenv[ ] Some useful pyenv commands are: commands List all available pyenv commands local Set or show the local application-specific Python version global Set or show the global Python version shell Set or show the shell-specific Python version install Install a Python version using python-build uninstall Uninstall a specific Python version rehash Rehash pyenv shims (run this after installing executables) version Show the current Python version and its origin versions List all Python versions available to pyenv which Display the full path to an executable whence List all Python versions that contain the given executable See `pyenv help ' for information on a specific command. For full documentation, see: https://github.com/pyenv/pyenv#readme
10 列出可安装的python版本
pyenv install --list | less #包含很多的解释器:官方的python解释器 运行在.net平台的python解释器 运行在java平台python解释器 pip解释器 stackless解释器
11 安装pyenv
pyenv install 3.6.2 Downloading Python-3.6.2.tar.xz... -> https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz Installing Python-3.6.2... Installed Python-3.6.2 to /opt/pyenv/versions/3.6.2
12 查看可用的python版本
pyenv versions * system (set by /opt/pyenv/version) 3.6.2
注意:生产环境不会需要多版本,通常来说开发需要多个版本
三、pyenv的使用
命令1:pyenv local
作用:切换当前目录及其子目录的python版本
[root@python ~]# pyenv local 3.6.2
删除.python-version就可以恢复默认的python版本
[root@python ~]# ls -al total 44 dr-xr-x---. 4 root root 198 Aug 23 19:05 . dr-xr-xr-x. 20 root root 259 Aug 23 18:37 .. -rw-------. 1 root root 1643 Jun 26 23:20 anaconda-ks.cfg -rw-------. 1 root root 4261 Aug 23 18:43 .bash_history -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile -rw-r--r--. 1 root root 240 Jun 26 23:32 .bashrc -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc drwxr-----. 3 root root 19 Aug 23 18:47 .pki -rw-r--r--. 1 root root 6 Aug 23 19:05 .python-version drwx------. 2 root root 25 Jun 26 23:42 .ssh -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc -rw-------. 1 root root 5644 Aug 23 18:50 .viminfo
命令2:pyenv versions
作用:查看已安装的所有python的版本,*代表当前目录正使用的python版本
[root@python ~]# pyenv versions system * 3.6.2 (set by /root/.python-version)
命令3:python -V
作用:查看当前python版本
python -V Python 3.6.2
命令4:pyenv global
作用:切换全局的python环境变量
注意:谨慎使用global命令,最好不要使用
参考链接:
[1]https://github.com/pyenv/pyenv
[2]https://github.com/pyenv/pyenv-installer