
前言
之前分享過一篇《Linux系統自帶Python2&yum的卸載及重裝》,介紹了如何卸載及重裝Linux(CentOS)自帶的的Python2.7,今天主要介紹如何在Linux系統下通過shell腳本一鍵安裝Python3,以及如何臨時or永久更換鏡像源、管理虛擬環境,工欲善其事必先利其器,環境搭建是一切開發&自動化測驗繞不開的前提,
一、Linux安裝Python3
在介紹shell腳本一鍵安裝Python前,先來了解一下手工搭建的程序,或許更有助于我們理解自動化搭建的程序及原理,
1、手工安裝方式
1)安裝依賴環境
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
2)下載Python3
yum -y install wget # 沒安裝wget工具時運行此命令 wget https://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz
3)解壓
tar -xzvf Python-3.7.5.tgz
4)編譯安裝
yum install gcc -y # 安裝gcc編譯器 yum install libffi-devel -y # 3.7版本之后需要一個新的包libffi-devel cd Python-3.7.5 # 進入解壓后的檔案夾 ./configure --prefix=/home/python # 配置指定安裝目錄 make && make install # 編譯&編譯安裝
5)配置python環境變數
export PYTHON_HOME=/home/python
export PATH=${PATH}:${PYTHON_HOME}/bin
vi /etc/profile # 在/etc/profile檔案中加入上面兩行內容 source /etc/profile # 添加完需要重新加載配置
2.shell腳本方式安裝
1)撰寫shell腳本
shell腳本本質上就是將上述一步步手工操作步驟中的命令集成一個檔案中,再加入一些判斷或引數互動,從而自動順序執行、實作自動化操作,
#!/bin/sh yum -y install wget # 安裝wget工具 yum -y install gcc # 安裝gcc # 安裝一堆依賴 yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel yum -y install libffi-devel if [ ! -f "Python-3.7.5.tgz" ] # 判斷當前目錄是否存在Python安裝包 then echo Python-3.7.5.tgz not exist!!! exit -1 fi `tar -zxvf Python-3.7.5.tgz` # 解壓安裝包 if [ ! -d /home/python3 ] # 判斷home目錄下是否存在python3目錄,不存在就創建 then `mkdir -p /home/python3` cd Python-3.7.5 ./configure --prefix=/home/python3 # 配置安裝目錄為/home/python3 make && make install # 編譯&編譯安裝 fi # 添加Python環境變數到/etc/profile echo "export PYTHON_HOME=/home/python3" >>/etc/profile echo "export PATH=\${PATH}:\${PYTHON_HOME}\"/bin\"" >>/etc/profile # 重新加載組態檔,注意:執行腳本時需使用source命令而非./或sh命令,否則環境變數不生效 `source /etc/profile` # 設定Python安裝鏡像源為阿里云 `pip config set global.index-url https://mirrors.aliyun.com/pypi/simple` echo "=====`date`===== Python-3.7.5 install success !!!====="
注意事項:
執行腳本需使用source命令而非./或sh命令,否則環境變數不生效,當然也可以再通過“source /etc/profile”命令使其生效,具體原理詳見之前分享過的《解決shell腳本中"source /etc/profile"多載組態檔不生效的問題》
2)執行安裝
source python_install.sh
安裝成功后如下圖所示:

驗證一下python3是否安裝成功,以及腳本最后更改的pip鏡像源是否生效:

從上圖可以看出,python3安裝成功,python鏡像源已成功替換為阿里云鏡像源,
3.卸載python3
rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps # 卸載python3 whereis python3 |xargs rm -frv # 洗掉python3所有殘余檔案,注意是卸載Python3,千萬要帶3,否則會把系統自帶的Python2和yum(依賴Python2)也卸載了 whereis python3 # 查看是否存在python3 python3 # 進入python解釋器環境,若提示“-bash: python3: command not found”則說明卸載成功
三、Python虛擬環境管理
在日常專案開發程序中,由于每個專案所用到的依賴包版本可能不一致,尤其是一些老專案,因此,為了避免各個專案之間環境相互“污染”,我們可以針對各個專案分別建立各自的虛擬環境,
python3 -m venv auto_test # 新建名為auto_test的虛擬環境目錄 cd auto_test/bin # 進入虛擬環境所在檔案夾的bin目錄(Windows進入Scripts目錄) source activate # 激活虛擬環境(Linux&Mac系統用source命令,Windows用.\activate.bat命令激活
激活成功后,命令列最前面會顯示一個:(虛擬環境名稱)

四、配置pip安裝鏡像源
1.臨時指定安裝鏡像源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple(鏡像地址) module(模塊名稱)
國內鏡像源:
清華:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中國科技大學:https://pypi.mirrors.ustc.edu.cn/simple/
華中理工大學:http://pypi.hustunique.com/
山東理工大學:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
2.永久更改安裝鏡像源
細心的同學或許會發現,其實在上述shell腳本中已經配置了阿里云的鏡像源,不過如果沒有通過腳本配置,我們也可以手工配置,
1)Linux系統
如果不想每次安裝第三方包的時候都指定鏡像源,也可以使用如下方法永久指定安裝鏡像源,一勞永逸
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple # 永久配置國內鏡像源 pip config list # 查看下載源配置串列
配置好鏡像源后,可以使用“pip config list”命令查看下載源配置串列,具體寫入的組態檔路徑為:
/root/.config/pip/pip.conf

2)windows系統
windows系統設定和查看方法相同

五、Python包批量匯出&安裝
pip freeze > requirements.txt # 匯出當前環境下已安裝的依賴包串列到指定檔案 pip download -r requirements.txt -d . # 下載指定檔案中的所有依賴包,不安裝 pip install -r requirements.txt # 批量安裝
更多一手測驗開發技術實戰,歡迎掃碼關注!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/501476.html
標籤:Python
上一篇:元組(tuple)
