Python多版本神器-Pyenv

简介:

Python多版本神器-Pyenv

wKioL1mdHVzTyzC7AACPPyWvaAE993.jpg

一、简介

使用pyenv可以在一个操作系统上使用多个python版本,自由切换,无比顺畅。它的使用不依赖于Python,是一个简单、独立的纯 shell 脚本工具。

二、Pyenv安装

1 切换到root用户

1
sudo  su  -

2 如果说没有git,请先安装git

1
yum  install  -y git

3 安装python依赖环境

1
yum  install  -y gcc  make  patch gdbm-devel openssl-devel sqlite-devel zlib-devel  bzip2 -devel readline-devel

4 设置python root环境

1
export  PYENV_ROOT= /opt/pyenv

5 安装pyenv 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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的环境变量

1
2
3
4
  vi  /etc/profile .d /pyenv .sh
export  PATH= "/opt/pyenv/bin:$PATH"
eval  "$(pyenv init -)"
eval  "$(pyenv virtualenv-init -)"

7 加载新的环境变量

1
source  /etc/profile .d /pyenv .sh

8 初始化pyenv 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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常用的用法 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pyenv help
Usage: pyenv < command > [<args>]
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 < command >'  for  information on a specific  command .
For full documentation, see: https: //github .com /pyenv/pyenv #readme

10 列出可安装的python版本

1
2
pyenv  install  --list |  less
#包含很多的解释器:官方的python解释器 运行在.net平台的python解释器 运行在java平台python解释器 pip解释器 stackless解释器

11 安装pyenv

1
2
3
4
5
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版本

1
2
3
pyenv versions
* system ( set  by  /opt/pyenv/version )
   3.6.2

注意:生产环境不会需要多版本,通常来说开发需要多个版本

三、pyenv的使用

命令1:pyenv local

    作用:切换当前目录及其子目录的python版本

1
[root@python ~] # pyenv local 3.6.2

删除.python-version就可以恢复默认的python版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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版本

1
2
3
[root@python ~] # pyenv versions
   system
* 3.6.2 ( set  by  /root/ .python-version)

 命令3:python -V            

     作用:查看当前python版本

1
2
python -V 
Python 3.6.2

 命令4:pyenv global

 作用:切换全局的python环境变量

 注意:谨慎使用global命令,最好不要使用



本文转自 PowerMichael 51CTO博客,原文链接:http://blog.51cto.com/huwho/1958635,如需转载请自行联系原作者

相关文章
|
2月前
|
测试技术 程序员 数据库
Python 3.9 beta2 版本发布了,看看这 7 个新的 PEP 都是什么?
Python 3.9 beta2 版本发布了,看看这 7 个新的 PEP 都是什么?
27 0
|
2月前
|
安全 小程序 数据安全/隐私保护
aes加密算法python版本
aes加密算法python版本
37 0
|
2月前
|
Python
Python 3.10 版本采纳了首个 PEP,中文翻译即将推出
Python 3.10 版本采纳了首个 PEP,中文翻译即将推出
18 3
|
2月前
|
开发者 Python
开发者请注意:Python2 的最后版本将于 4 月发布,但它确实是在 1 月 1 日就寿命终止了!
开发者请注意:Python2 的最后版本将于 4 月发布,但它确实是在 1 月 1 日就寿命终止了!
28 1
|
2月前
|
机器学习/深度学习 PyTorch 算法框架/工具
Python冷知识:如何找出新版本增加或删除了哪些标准库?
Python冷知识:如何找出新版本增加或删除了哪些标准库?
24 0
|
4月前
|
JSON TensorFlow 算法框架/工具
Windows下安装Anaconda5.3.1+Python3.8+TensorFlow2.13.0-CPU版本总结
Windows下安装Anaconda5.3.1+Python3.8+TensorFlow2.13.0-CPU版本总结
113 0
|
3天前
|
Python
IDA3.12版本的python,依旧报错IDAPython: error executing init.py.No module named ‘impRefer to the message win
IDA3.12版本的python,依旧报错IDAPython: error executing init.py.No module named ‘impRefer to the message win
|
17天前
|
Python Windows
【Python】Windows如何在cmd中切换python版本
【Python】Windows如何在cmd中切换python版本
|
1月前
|
算法 NoSQL JavaScript
常见的限流算法-python版本
常见的限流算法-python版本
21 0
常见的限流算法-python版本
|
2月前
|
Java 程序员 PHP
Python 如何移除旧的版本特性,如何迎接新的特性?
Python 如何移除旧的版本特性,如何迎接新的特性?
21 0