天天看點

使用pyenv搭建多python版本共存,便于開發

使用實體機win7,虛拟機使用VM WareStation

在虛拟機上使用centos7.2帶桌面版版的系統;

本次實驗使用pyenv來管理python的版本,好處的在開發中使用各個版本靈活切換。

安裝pyenv:

1.安裝git

  ]# yum -y install git

2.安裝pyenv

  ]# curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

3.配置環境變量

 編輯~/.bash_profile裡面增加如下内容:

vim ~/.bash_profile

export PATH="~/.pyenv/bin:$PATH"

eval "$(pyenv init -)"

eval "$(pyenv virtualenv-init -)"

<a href="http://s4.51cto.com/wyfs02/M00/8B/0E/wKiom1hCoETxMlaxAAAh33a6TOU997.png" target="_blank"></a>

]$ source ~/.bash_profile

]$ . ~/.bash_profile

安裝python

安裝編譯工具

]# yum -y install gcc make patch

安裝依賴

]# yum -y install gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel

安裝python3.5.2

pyenv install 3.5.2

第3步,如果沒有網絡,可事先下載下傳至本地,再進行安裝:

手動建立cache目錄,預設沒有此目錄:

]# mkdir ~/.pyenv/cache

把python3.5.2的源碼包Python-3.5.2.tar.xz放在該目錄下,再執行pyenv install 3.5.2即可。

安裝成功:

<a href="http://s3.51cto.com/wyfs02/M00/8B/0A/wKioL1hCn7ryk6BCAAAx5ZckRZo185.png" target="_blank"></a>

預設安裝在

]$ ls ~/.pyenv/versions/3.5.2/目錄下。

安裝後可檢視版本:

<a href="http://s3.51cto.com/wyfs02/M00/8B/0A/wKioL1hCncDT2zaEAAAvShAlbp4670.png" target="_blank"></a>

]$ pyenv versions

]$ pyenv version

二者差別在于versions是檢視所有python版本,version是檢視預設python版本。

而python -V是檢視系統自帶的python版本,也是pyenv version檢視的預設版本。

pyenv的使用

local指令

設定某目錄及其子目錄使用指定的python版本

前提是先cd進入某目錄

]$ pyenv local 3.5.2

<a href="http://s4.51cto.com/wyfs02/M01/8B/0A/wKioL1hCow7CRSq3AAAffT_Hwis005.png" target="_blank"></a>

global指令

設定全局python版本,一般不用此指令。

virtualenv指令

建立虛拟環境,為開發環境使用多個python版本;

例如:使用python3.5.2為基礎,建立虛拟環境magedu:

恢複系統預設python版本:pyenv local system

]$ mkdir py3

]$ cd py3

]$ pyenv virtualenv 3.5.2 py3.5.2

]$ pyenv local py3.5.2

以後,直接cd進入py3目錄,自動切換為python3.5.2版本,多版本建立同理;

安裝ipython

手動建立:~/.pip/pip.conf

[global]

index-url = http://mirrors.aliyun.com/pypi/simple/

trusted-host = mirrors.aliyun.com

]$ pip install ipython

進入python和ipython指令行對比:

<a href="http://s5.51cto.com/wyfs02/M02/8B/0B/wKioL1hCykCSVJNZAABLA9U6EQo970.png" target="_blank"></a>

<a href="http://s5.51cto.com/wyfs02/M00/8B/0F/wKiom1hCykCAeXdBAAApBqJPvuc165.png" target="_blank"></a>

安裝jupyter:是python的web界面

]$ pip install jupyter

此時,可通過指令行啟動web界面:

]$ jupyter notebook --ip=0.0.0.0

會彈出x11轉發的web視窗:

注意:要在xshell中設定

<a href="http://s1.51cto.com/wyfs02/M02/8B/0F/wKiom1hC0KXDZ7ogAADK6DgQW4g727.png" target="_blank"></a>

彈出:

<a href="http://s5.51cto.com/wyfs02/M00/8B/0C/wKioL1hC0NijKp6MAADbqZZac94401.png" target="_blank"></a>

因為,此界面是通過x11轉發的,速度較慢,可直接關閉;然後在win7上直接打開浏覽器,輸入虛拟機ip:8888,如下:

<a href="http://s2.51cto.com/wyfs02/M01/8B/0F/wKiom1hC0bvBg1_kAADSJBEXFj8526.png" target="_blank"></a>

選new--》python3

<a href="http://s4.51cto.com/wyfs02/M00/8B/0C/wKioL1hC0aTx5vPdAADUfcc5dtI603.png" target="_blank"></a>

<a href="http://s4.51cto.com/wyfs02/M02/8B/0F/wKiom1hC0aWQXupoAAEGDW--zTg172.png" target="_blank"></a>

類似ipython的指令行;

<a href="http://s2.51cto.com/wyfs02/M02/8B/0C/wKioL1hC0ljTwP74AADYbgdUzZI342.png" target="_blank"></a>

用來實驗、示範非常友善。

ipython指令行可實驗bash的指令。

python指令行使用ctrl+d或exit()退出。

退出jupyter指令行,使用ctrl+c。

注意:

1、在不同的centos版本,可能在指令行執行後,不會自動打開web頁面,無需關心,直接web上手動輸入IP:8888打開既可;

2、在指令行觀察,有可能會登陸jupyter密碼,如下:

<a href="https://s3.51cto.com/wyfs02/M01/8E/4A/wKioL1i8thuBCBLBAAAakLcxMvo285.png" target="_blank"></a>

token=後面的就是密碼,需要在登陸jupyter時,輸入;

如果覺得每次登陸jupyter輸入密碼麻煩,可以取消密碼登陸:步驟如下:

1、生成jupyter配置檔案:

]# jupyter notebook --generate-config

2、執行後,會在指令行顯示,配置檔案所在路徑;顯示為/root/.jupyter/jupyter_notebook_config.py

3、更改配置檔案如下:

vim /root/.jupyter/jupyter_notebook_config.py

<a href="https://s4.51cto.com/wyfs02/M01/8E/4C/wKiom1i8thnBOaFFAAAKJCnX92Q777.png" target="_blank"></a>

改為:

<a href="https://s5.51cto.com/wyfs02/M00/8E/4C/wKiom1i8t5vSOgElAAAJyrxEImA800.png" target="_blank"></a>

重新開機jupyter後,登陸web頁面就無需密碼登陸了。

本文轉自 crystaleone 51CTO部落格,原文連結:http://blog.51cto.com/linsj/1879236,如需轉載請自行聯系原作者

上一篇: Net-網卡綁定

繼續閱讀