linux下多版本python環境配置

原文出自:http://www.178linux.com/87022

                  http://blog.csdn.net/lixintong1992/article/details/51586630

1. 依賴
pyenv安裝使用git
# yum install git -y

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

2. 創建用戶python
# useradd python

# passwd python

3. 使用python用戶登錄
su – python

4. 開始部署pyenv
pyenv安裝方式:
– pyenv git方式安裝 https://github.com/pyenv/pyenv
– pyenv-installer 腳本自動安裝 https://github.com/pyenv/pyenv-installer

git方式:

cd /home/python/
git clone https://github.com/pyenv/pyenv
cd /home/python/pyenv/plugins
git clone https://github.com/pyenv/pyenv-virtualenv.git
git clone https://github.com/pyenv/pyenv-doctor.git
git clone https://github.com/pyenv/pyenv-installer.git
git clone https://github.com/pyenv/pyenv-update.git
git clone https://github.com/pyenv/pyenv-which-ext.git

以下將介紹使用 pyenv-installer 方式安裝 pyenv
$ curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
在python用戶的~/.bash_profile中追加
export PYENV_ROOT="/home/python/pyenv"
export PATH="/home/python/pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
$ source ~/.bash_profile

開始使用 pyenv
$ python -V
$ pyenv versions

5. pyenv 使用介紹
$ pyenv # 顯示 pyenv 幫助
$ pyenv global x.x.x # 設置全局 python版本(應用到整個系統)
$ pyenv local x.x.x # 設置本地 python版本(子目錄下會繼承此設置)
$ pyenv shell x.x.x # 設置會話 python版本(作用於當前shell會話)
$ pyenv help install # 查看子命令幫助

$ pyenv install –list # 列出 pyenv 支持的所有版本

5.1 安裝特定的 python 版本
5.1.1 online 安裝指定 python 版本
$ pyenv install 3.5.3
$ pyenv versions

5.1.2. 使用緩存方式安裝指定 python 版本
$ pyenv install 3.5.3 -v

> cache目錄,如果目錄不存在,就自己創建,在~/.pyenv目錄下,新建cache目錄,放入下載好的 python 文件。
> 不確定要哪一個文件,把下載的3個文件都放進去。

6. pyenv 使用 virtualenv 虛擬環境設置
> pyenv已經自帶 Virtualenv插件,在plugins/pyenv-virtualenv
如果沒有virtualenv插件

利用virtualenv 創建虛擬python環境

(1).pyenv-virtualenv插件安裝:項目主頁:https://github.com/yyuu/pyenv-virtualenv
pyenv virtualenv是pyenv的插件,爲UNIX系統上的Python virtualenvs提供pyenv virtualenv命令。
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv   
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
source ~/.bash_profile

(2) 創建一個2.7.1的虛擬環境
pyenv virtualenv 2.7.1 env271
這條命令在本機上創建了一個名爲env271的python虛擬環境,這個環境的真實目錄位於:~/.pyenv/versions/
 

6.1 創建一個指定版本的虛擬環境空間
$ pyenv virtualenv 3.6.1 magedu361 # 創建出一個3.6.1版本的虛擬環境
$ pyenv versions # 真實目錄在.pyenv/versions/
* system (set by /home/python/.pyenv/version)
3.5.3
3.6.1
3.6.1/envs/magedu361
magedu361

6.2 使用虛擬環境空間
$ mkdir -p magedu361/projects/cmdb
[python@node ~]$ cd magedu361/projects/cmdb
[python@node cmdb]$ pyenv local magedu361
(magedu361) [python@node cmdb]$ cd ..
[python@node projects]$ cd cmdb/

7. 部署 ipython 與 jupyter
7.1 配置pip
vi ~/.pip/pip.conf
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com

在不同的虛擬環境中,安裝redis包,使用pip list看看效果。
$ pip -V

7.2 安裝ipython
$ pip install ipython
$ ipython

7.3 部署 jupyter
安裝Jupyter,也會自動安裝ipython
$ pip install jupyter
$ jupyter notebook help

生成配置文件
$ jupyter notebook –generate-config
$ jupyter notebook password # 設置 jupyter 登錄密碼(也可以寫入到配置文件中)
$ jupyter notebook –ip=0.0.0.0 –no-browser

生成密碼
$ ipython
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: ‘sha1:ce23d945972f:34769685a7ccd3d08c84a18c63968a41f1140274′ # 複製密碼‘sha:ce…’

修改默認配置文件
vim ~/.jupyter/jupyter_notebook_config.py
c.NotebookApp.ip=’*’ # 在所有IP上偵聽
# c.NotebookApp.password = ‘string’
# The string should be of the form type:salt:hashed-password
c.NotebookApp.password = ‘sha:ce…剛纔複製的那個密文’
c.NotebookApp.open_browser = False # 禁止自動打開瀏覽器
c.NotebookApp.port =8888 #隨便指定一個端口

啓動jupyter notebook
jupyter notebook

參考地址: <http://jupyter-notebook.readthedocs.io/en/latest/public_server.html>

python環境移植
pip freeze > requirement
pip install -r requirement

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
關於windows下安裝,大同小異。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章