1. 程式人生 > >pyenv安裝及其與pip的相關操作

pyenv安裝及其與pip的相關操作

"""

<axiner>宣告:
(錯了另刂扌丁我)
(如若有誤,請記得指出喲,謝謝了!!!)

"""

=====================
pyenv與pyenv-virtualenv的安裝
==========
pyenv是個多版本Python管理器,可以同時管理多個Python版本共存。
pyenv-virtualenv 是pyenv的一個plugin(外掛),可以用來建立基於不同Python版本的乾淨的虛擬環境。(與virtualenv類似)

1-1 安裝開發工具
# yum groupinstall Development tools

1-2 安裝必需元件
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

1-3 安裝pyenv與pyenv-virtualenv
線上安裝方式一:
# wget https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer --no-check-certificate
# chmod +x pyenv-installer
# ./pyenv-installer

線上安裝方式二:
# curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
(需保證系統有 git ,否則需要新安裝git工具。)

線上安裝方式三:
# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
# git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
(需保證系統有 git ,否則需要新安裝git工具。)

===========
另:離線安裝
從以下連結獲取:
https://github.com/yyuu/pyenv.git
https://github.com/yyuu/pyenv-virtualenv.git
如:安裝包為`pyenv-master.zip`,`pyenv-virtualenv-master.zip`
則執行以下命令
# chmod +x pyenv-master.zip pyenv-virtualenv-master.zip
# unzip pyenv-master.zip
# unzip pyenv-virtualenv-master.zip
# mv pyenv-master ~/.pyenv
# mv pyenv-virtualenv-master ~/.pyenv/plugins/pyenv-virtualenv

1-4 環境變數配置
# vi ~/.bash_profile
新增以下:

export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

儲存退出後,執行以下命令生效配置:
# source ~/.bash_profile



=====================
pyenv相關操作:


==========
1、列出可安裝的版本
# pyenv install --list


2、安裝2.7.12版本        
# pyenv install 2.7.12

附:(官網下載速度很慢)
方法一:下載國內原始碼再安裝   --推薦使用
(python搜狐映象源加速:http://mirrors.sohu.com/python/)

--下載國內python版本原始碼到~/.pyenv/cache資料夾($v換成所下的版本號)(cache不存在則自行建立)
# wget http://mirrors.sohu.com/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/

--安裝
# pyenv install 版本號


方法二:更改預設的pyenv下載源地址   --不推薦此方法
--相關pyenv映象源沒有對這個專案進行更新,所以python版本沒有得到更新

# export PYTHON_BUILD_MIRROR_URL="http://pyenv.qiniudn.com/pythons/"
# pyenv install 版本號


以上兩方法安裝完成後,需要對資料進行更新
# pyenv rehash
        

3、切換Python版本
3-1 切換到2.7.12
# pyenv global 2.7.12  # Python的全域性設定,整個系統生效
# pyenv local 2.7.12  # Python的區域性設定,當前目錄生效--取消設定 # pyenv local --unset  

3-2 切換到system(即預設)
# pyenv global system  


4、列出當前可用的python(包含建立的virtualenv)
# pyenv versions
說明:
    system        --表系統自帶的
    * 2.7.12    --*表當前正使用的

    
5、解除安裝
# pyenv uninstall 2.7.12  


6、安裝virtualenv環境

6-1 制定版本建立virtualenv(虛擬環境版本號為已安裝的版本)
# pyenv virtualenv 2.7.12 venv2712
(完成後提示:Requirement already satisfied...)

6-2 列出現有virtualenvs(顯示現有virtualenv和conda環境的列表)
# pyenv virtualenvs

6-3 手動啟用和退出pyenv virtualenv:
啟用:
# pyenv activate virtualenv的名稱
退出當前:
# pyenv deactivate


6-4 刪除某virtualenv
# pyenv uninstall virtualenv的名稱


7、其它
檢視pyenv 的相關命令通過
# pyenv commands

獲取virtualenv的幫助
# pyenv virtualenv --help



=====================
pip相關操作:

==========
1、更新:
# pip install --upgrade pip/pip install -U pip
# python -m pip install --upgrade pip

下載最新原始碼包,安裝更新

    
2、搜尋包:
# pip search '包名'


3、安裝包
預設安裝最新:
# pip install SomePackage
指定版本:
# pip install SomePackage==1.1.1
最小版本:
# pip install SomePackage>=1.1.1
最大版本:
# pip install SomePackage&lt;=1.1.1


4、freeze匯出與安裝
匯出(若存在,則會覆蓋之前的):
# pip freeze > requirements.txt

安裝(若存在,則會跳過之前的):
# pip install -r requirements.txt

解除安裝requirements.txt所指定的
# pip uninstall -r requirements.txt -y


5、wheel打包與安裝

檢視wheel命令:pip help wheel

打包(若存在,打包則會跳過存在的)
# pip wheel --wheel-dir=wheelhouse -r requirements.txt
# pip wheel --wheel-dir=wheelhouse-linux -r requirements.txt

安裝(若存在,安裝則會跳過存在的)
# pip install --no-index --find-links=wheelhouse -r requirements.txt
# pip install --no-index --find-links=wheelhouse-linux -r requirements.txt
(說明:是忽略包索引(僅僅從--find-links指定的連結地址中去查詢包))


6、pip其它
線上安裝:(會安裝依賴包)
# pip install  模組名


.whl安裝:
# pip install xxx.whl


原始碼安裝:
# python setup.py install


7、easy_install的使用
安裝庫:
# easy_install PackageName

解除安裝庫:
# easy_install -m PackageName