一、pyenv介绍

        CentOS6系统会自带一个较低版本的python,一般不使用系统自带的python版本,因为系统很多组件依赖于python比如yum,如果我们随意升级或者安装了些有冲突包可能会影响系统环境;我们需要再安装较高版本的python,而且在开发多个项目时,可能需要多个版本的Python,此时在进行Python版本切换时会比较麻烦,pyenv就提供了一种简单的方式。

pyenv是一个能简易地在多个Python版本中进行切换的工具,它简单而优雅。

项目地址https://github.com/yyuu/pyenv

pyenv的功能:

  • 安装python解释器

  • 进行全局的Python版本切换

  • 为单个项目提供对应的Python版本

  • 使用环境变量能让你重写Python版本

  • 能在同一时间在不同版本间进行命令搜索

拥有以下特点:

  • 只依赖python本身

  • 将目录添加进$PATH即可使用

  • 能够进行virtualenv管理(通过插件)


二、安装pyenv

1、pyenv通过bash编写,不依赖任何程序;安装程序只依赖git

[[email protected] ~]# yum install git

2、安装pyenv

[[email protected] ~]# curl -L https://raw.githubusercontent.com/yyuu/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 --:--:--  0:00:01 --:--:--      0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     98  2099   98  1969    0     0    767      0  0:00:02  0:00:02 --:--:--  13104  2099  104  2099    0     0    818      0  0:00:02  0:00:02 --:--:--  1425
Initialized empty Git repository in /root/.pyenv/.git/
remote: Counting objects: 4060, done.
remote: Compressing objects: 100% (1658/1658), 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="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"

      pyenv的安装路径是由$PYENV_ROOT这个环境变量设定的(默认没设定),如果没设定默认安装路径为~/.pyenv

[[email protected] ~]# ls -a
.                .bash_history  .bashrc      install.log.syslog  .tcshrc
..               .bash_logout   .cshrc       .pki                .viminfo
anaconda-ks.cfg  .bash_profile  install.log  .pyenv              .Xauthority
[[email protected] ~]# ls .pyenv/           #卸载pyenv只需要删掉这个目录就可以
bin           completions  LICENSE   pyenv.d    src
CHANGELOG.md  CONDUCT.md   Makefile  README.md  test
COMMANDS.md   libexec      plugins   shims      versions

3、配置环境变量

[[email protected] ~]# vi /etc/profile.d/pyenv.sh

[[email protected] ~]# cat /etc/profile.d/pyenv.sh
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

[[email protected] ~]# . /etc/profile.d/pyenv.sh 
[[email protected] ~]# pyenv                           #pyenv就可以使用了
pyenv 1.0.3
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/yyuu/pyenv#readme


三、安装python

    使用pyenv安装python,pyenv会下载安装python最新稳定版,在安装Python之前我们还需要安装编译工具和python的依赖包

1、安装编译工具

[[email protected] ~]# yum install gcc make patch

2、安装python依赖包

[[email protected] ~]# yum install gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel

3、安装python

[[email protected] .pyenv]# pyenv install --list   # 列出所有可以安装的python解释器
Available versions:
  2.1.3
  
  3.5.1
  3.5.2          # 没带字母的是python原生自带的解释器,我们使用这个最新稳定版的
  3.6.0b3
  3.6-dev
  3.7-dev
  anaconda-1.4.0
  anaconda-1.5.0
  anaconda-1.5.1

  anaconda3-4.1.1
  ironpython-dev

  ironpython-2.7.6.3
  jython-dev
  jython-2.5.0
 
  jython-2.7.1b3
  miniconda-latest
  miniconda-2.2.2

  miniconda3-4.0.5
  miniconda3-4.1.11
  
  pypy-c-jit-latest
  pypy-c-nojit-latest
  pypy-dev
  pypy-stm-2.3
  pypy-portable-2.3.1
  
  pypy-portable-5.4
  pypy-portable-5.4.1
  pypy-1.5-src
  pypy-1.5
  pypy-1.6
  
  pypy2-5.4.1-src
  pypy2-5.4.1
  pypy3-dev
  pypy3-portable-2.3.1
 
  pypy3.3-5.5-alpha-src
  pypy3.3-5.5-alpha
  pyston-0.5.1
  stackless-dev

  stackless-3.4.2

[[email protected] ~]# pyenv install 3.5.2    #安装python对应的版本
Downloading Python-3.5.2.tar.xz...
-> https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz  
   # 因为要从外国网站上下载所有会很慢,我们也可以在~/.pyenv目录下新建一个cache目录,
     自己下载好Python的安装包(需要和上面download 的版本格式一样),放在chache目录下,pyenv就不会再去网站上下载了 
Installing Python-3.5.2...
patching file Lib/venv/scripts/posix/activate.fish
Installed Python-3.5.2 to /root/.pyenv/versions/3.5.2   #python被安装在这个目录下

[[email protected] ~]# pyenv versions                #列出所有可用python版本
* system (set by /root/.pyenv/version)
  3.5.2


四、pyenv的使用

[[email protected] ~]# 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
               #在当前路径创建一个.python-version,以后进入这个目录自动切换为该版本 
   global      Set or show the global Python version       
               #设置全局默认的python版本
   shell       Set or show the shell-specific Python version  
               #在当前shell的session中启用某个Python版本,优先级高于global,local 
   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  
               #显示当前正在使用Python版本
   versions    List all Python versions available to pyenv   
               #显示所有可用的Python版本
   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/yyuu/pyenv#readme

pyenv local 版本号:

       在当前路径下创建一个.python-version的文件,以后进入这个目录或子目录会自动把python切换的为该版本;可以通过删除 `.python-version`恢复默认Python版本

[[email protected] ~]# cd /tmp
[[email protected] tmp]# python --version
Python 2.6.6
[[email protected] tmp]# pyenv local 3.5.2     #切换为3.5.2的版本
[[email protected] tmp]# python --version      #成功
Python 3.5.2
[[email protected] tmp]# pyenv version
3.5.2 (set by /tmp/.python-version)
[[email protected] tmp]# cat .python-version
3.5.2

[[email protected] tmp]# cd ..            
[[email protected] /]# pyenv version        #在别的目录python的版本还是系统自带的
system (set by /root/.pyenv/version)

[[email protected] /]# cd /tmp
[[email protected] tmp]# mkdir test          
[[email protected] tmp]# cd test
[[email protected] test]# pyenv version      #Pyenv local对子目录也有效
3.5.2 (set by /tmp/.python-version)
[[email protected] test]# python -V
Python 3.5.2

[[email protected] tmp]# pyenv local system     #切换回系统自带的版本
[[email protected] tmp]# python --version
Python 2.6.6

注意:第一次设置pyenv local需要重新读取PATH环境变量才生效


pyenv global 版本号:设置全局默认的python版本,注意永远不要使用这个命令


pyenv virtualenv 版本号 项目名称或虚拟环境名:为此项目设置一个虚拟环境

[[email protected] ~]# pyenv virtualenv 3.5.2 magedu
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools in /root/.pyenv/versions/3.5.2/envs/magedu/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /root/.pyenv/versions/3.5.2/envs/magedu/lib/python3.5/site-packages

[[email protected] ~]# pyenv versions
* system (set by /root/.pyenv/version)
  3.5.2
  3.5.2/envs/magedu        #
  magedu                   #为了兼容旧版本的pyenv

pyenv uninstall 版本号或虚拟环境名或项目名称:卸载某个版本, 包括虚拟环境

            用来创建隔离的python环境,处理python环境的多版本和模块依赖。

[email protected] ~]# pyenv uninstall magedu
pyenv-virtualenv: remove /root/.pyenv/versions/3.5.2/envs/magedu? y   
[[email protected] ~]# pyenv versions
  system
* 3.5.2 (set by /root/.python-version)

我们需要搞清楚pyenv 和virtualenv分别是干什么的。

       pyenv可以帮助你在一台开发机上建立多个版本的python环境, 并提供方便的切换方法。

       virtualenv则提供了一种功能, 就是将一个目录建立为一个虚拟的python环境, 这样的话, 用户可以建立多个虚拟环境, 每个环境里面的python版本可以是不同的, 也可以是相同的, 而且环境之间相互独立。

如果解释得还不是很清楚的话,我们举例说明。

       首先我们可以用pyenv 安装多个python 版本, 比如安装了2.5, 2.6, 3.3 三个版本。 用户可以随意切换当前默认的python版本。 但这时候, 每个版本的环境仍是唯一的, 如果我们想在环境中安装一些库的话, 还是会导致这个版本的环境被修改。 这个时候, 如果我们用virtual env去建立虚拟环境, 就可以完全保证系统路径的干净。无论你在虚拟环境中安装了什么程序, 都不会影响已安装版本的系统环境


五、python IDE(集成开发环境)

PyCharm推荐,(写大型项目时)

Idea


1、ipython

        ipython是一个 python 的交互式 shell,比默认的python shell 好用得多,支持命令自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函数。

[[email protected] ~]# mkdir magedu        #安装包时进入一个虚拟环境,保持原版本python环境的干净
[[email protected] ~]# cd magedu
[[email protected] magedu]# pyenv local magedu
(magedu) [[email protected] magedu]# pyenv version
magedu (set by /root/magedu/.python-version)
(magedu) [[email protected] magedu]# pyenv versions
  system
  3.5.2
  3.5.2/envs/magedu
* magedu (set by /root/magedu/.python-version)     

(magedu) [[email protected] magedu]# pip install ipython

    此时发现pip安装软件包也比较慢,因为pip会去pypi官网下载软件包,我们取消,配置pip使用国内的镜像站。

(magedu) [[email protected] magedu]# mkdir ~/.pip
(magedu) [[email protected] magedu]# vim ~/.pip/pip.conf

(magedu) [[email protected] magedu]# cat ~/.pip/pip.conf
[global]         #全局配置    
timeout = 6000   #超时时间
index-url = http://mirrors.aliyun.com/pypi/simple/   #镜像地址

[install]
trusted-host=mirrors.aliyun.com    #信任该网站上的包

配置完成后再安装ipython速度飞快。

(magedu) [[email protected] magedu]# ipython
Python 3.5.2 (default, Nov 11 2016, 21:25:01) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:

2、jupyter

         可以让浏览器使用ipython的环境    

(magedu) [[email protected] magedu]# pip install jupyter

(magedu) [[email protected] magedu]# jupyter --help
usage: jupyter [-h] [--version] [--config-dir] [--data-dir] [--runtime-dir]
               [--paths] [--json]
               [subcommand]

Jupyter: Interactive Computing

positional arguments:
  subcommand     the subcommand to launch

optional arguments:
  -h, --help     show this help message and exit
  --version      show the jupyter command's version and exit
  --config-dir   show Jupyter config dir
  --data-dir     show Jupyter data dir
  --runtime-dir  show Jupyter runtime dir
  --paths        show all Jupyter paths. Add --json for machine-readable
                 format.
  --json         output paths as machine-readable json

Available subcommands: console kernelspec migrate nbconvert nbextension
notebook qtconsole serverextension troubleshoot trust

运行jupyter noterbook:

(magedu) [[email protected] magedu]# jupyter notebook --ip=0.0.0.0  #让notebook监听在0.0.0.0,默认监听localhost,默认端口为8888
[I 10:56:18.364 NotebookApp] Serving notebooks from local directory: /root/magedu
[I 10:56:18.365 NotebookApp] 0 active kernels 
[I 10:56:18.365 NotebookApp] The Jupyter Notebook is running at: http://0.0.0.0:8888/
[I 10:56:18.365 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 10:56:18.365 NotebookApp] No web browser found: could not locate runnable browser.
[I 10:56:28.516 NotebookApp] 302 GET / (192.168.10.10) 1.19ms   #客户端浏览器访问信息

现在用浏览器访问:192.168.10.3:(8888)     #192.168.10.3为Node3的ip

【Python】03、python多版本管理工具pyenv【Python】03、python多版本管理工具pyenv

分别看下这些功能:

 1)新建文档

【Python】03、python多版本管理工具pyenv

                功能强大的编辑器

2)远程终端

【Python】03、python多版本管理工具pyenv

                                     网页版的远程终端

3)网页版的ipython

【Python】03、python多版本管理工具pyenv

               网页版的ipython方便做演示

这个jupyter notebook的功能有点高上大的感觉。