centos7安装wget_Centos7系统切换Python2到Python3.x
我們先來查看一下安裝的CentOS 7操作系統(tǒng)中python的版本情況。直接在命令行上輸入python:
[cao@localhost ~]$ python Python 2.7.5 (default, Apr 2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>可以看到,Centos7系統(tǒng)目前自帶python的版本為2.7.5。
同時可以使用linux命令whereis python來查看python安裝路徑:
[cao@localhost ~]$ whereis python python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz鑒于python2版本已經得不到python社區(qū)的支持,而且大部分第三方庫均支持python3(具體的說,有些庫要求python3.5及其以上版本),因此在linux中使用python時還是需要升級到python3。
2. Python linux下載安裝介質
這里的python3泛指python3.0以上版本,也就是非python2版本。不過受python語言版本的不斷升級,在使用python3版本時我們一方面要使用較新的版本,同時也考慮其穩(wěn)定性,因此這里我們將下載安裝和使用python3.7版本。
下載方式有兩種:可以直接在虛擬機里使用linux命令wget來下載,或者先從python官網上下載python linux版本,然后ftp上傳到虛擬機里。兩種方式對應的python程序下載地址均為:https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz。
需要提示的是,python的下載安裝建議都使用超級管理員root賬戶來操作。
步驟1:先切換為root用戶,使用yum工具安裝wget
[cao@localhost ~]$ su - Password: Last login: Sat Jun 13 14:02:57 CST 2020 on tty1 [root@localhost ~]# yum install -y wget Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile* base: mirrors.huaweicloud.com* extras: mirrors.huaweicloud.com* updates: mirrors.huaweicloud.com …… (省略中間過程) Package wget-1.14-18.el7_6.1.x86_64 already installed and latest version步驟2:使用wget來下載python3.7
[root@localhost ~]# wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz --2020-06-14 05:06:17-- https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz Resolving www.python.org (www.python.org)... 151.101.228.223, 2a04:4e42:1a::223 Connecting to www.python.org (www.python.org)|151.101.228.223|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 22897802 (22M) [application/octet-stream] Saving to: ‘Python-3.7.2.tgz’ 100%[======================================>] 22,897,802 73.2KB/s in 3m 59s 2020-06-14 05:10:18 (93.8 KB/s) - ‘Python-3.7.2.tgz’ saved [22897802/22897802] [root@localhost ~]# ll total 22368 -rw-------. 1 root root 1431 Jun 10 21:05 anaconda-ks.cfg -rw-r--r--. 1 root root 22897802 Dec 24 2018 Python-3.7.2.tgz如果執(zhí)行wget沒有響應,說明是yum源的問題,需要替換centos默認自帶的源。
首先備份自帶的repo:
[root@localhost yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak然后使用curl命令下載網易鏡像源:
[root@localhost yum.repos.d]# curl http://mirrors.163.com/.help/CentOS7-Base-163.repo -o CentOS7-Base-163.repo% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed 100 1572 100 1572 0 0 13979 0 --:--:-- --:--:-- --:--:-- 14035接下來運行makecache命令生成緩存:
[root@localhost yum.repos.d]# yum clean all Loaded plugins: fastestmirror Cleaning repos: base extras updates Cleaning up list of fastest mirrors [root@localhost yum.repos.d]# yum makecache升級yum操作:
[root@localhost yum.repos.d]# yum update –y然后重新下載安裝wget:
[root@localhost yum.repos.d]# yum install -y wget3. Centos系統(tǒng)安裝python3
首先解壓python安裝包:
[root@localhost ~]# tar -zxf Python-3.7.2.tgz [root@localhost ~]# ll total 22372 -rw-------. 1 root root 1431 Jun 10 21:05 anaconda-ks.cfg drwxr-xr-x. 18 501 501 4096 Dec 24 2018 Python-3.7.2 -rw-r--r--. 1 root root 22897802 Dec 24 2018 Python-3.7.2.tgz然后進入解壓目錄進行配置:
[root@localhost Python-3.7.2]# ./configure --with-ssl執(zhí)行編譯和安裝:
[root@localhost Python-3.7.2]# make [root@localhost Python-3.7.2]# make install上述四步一步步執(zhí)行即可,正常情況下不會報錯,如果在make install執(zhí)行后出現了 ModuleNotFoundError: No module named ‘_ctypes’ 錯誤,則安裝一下依賴庫:
yum –y install libffi-devel tk-devel安裝好后,重新執(zhí)行make intall即可。
4. Python3配置連接
4.1 設置python3的軟連接
可以先使用whereis來查看python3通過編譯安裝后存放的路徑??梢钥闯龆荚趗sr/local目錄下:
[root@localhost ~]# whereis python3 python3: /usr/lib/python3.7 /usr/local/bin/python3.7m /usr/local/bin/python3.7 /usr/local/bin/python3.7m-config /usr/local/bin/python3 /usr/local/bin/python3.7-config /usr/local/lib/python3.7然后進入/usr/bin目錄先刪除原有指向python2的軟連接python,然后將python3.7與python建立軟連接。
進入/usr/bin目錄:
[root@ localhost bin]# pwd /usr/bin將原有python執(zhí)行程序備份:
[root@ localhost bin]# mv python python-bk添加python3.7的軟連接:
[root@ localhost bin]# ln -s /usr/local/bin/python3.7 /usr/bin/python4.2 驗證python版本
驗證python現在的版本:
[root@ localhost bin]# python -V Python 3.7.2退出root賬戶,在普通用戶終端輸入python:
[cao@localhost ~]$ python Python 3.7.2 (default, Jun 14 2020, 05:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print("hello") hello可以看到python3已經可以正常使用了。
4.3 解決yum安裝解析問題
因為yum采用python作為命令解釋器,現在設置了python3.7為/usr/bin/python的軟連接,因此再運行yum時會報錯:
[root@big01 ~]# yumFile "/bin/yum", line 30except KeyboardInterrupt, e:^ SyntaxError: invalid syntax此時需要取修改yum對應的解釋器:進入root賬戶,編輯 vi /usr/bin/yum后:
將第一行語句#!/usr/bin/python 修改為 #!/usr/bin/python2.7
同時修改urlgrabber-ext-down 文件即: vi /usr/libexec/urlgrabber-ext-down,同樣將第一行語句#!/usr/bin/python 修改為 #!/usr/bin/python2.7即可。
上述兩個文件修改后,再來運行yum應該就正常了。
5. pip工具無法使用的問題
如果在安裝好python后,想使用pip工具來安裝第三方庫,突然發(fā)現:
-bash: pip3: command not found此時需要使用yum來安裝一下,具體執(zhí)行代碼如下:
[root@big01 Python-3.7.2]# yum install epel-release [root@big01 Python-3.7.2]# yum install -y python-pip執(zhí)行結束后,pip就可以正常使用了。
總結
以上是生活随笔為你收集整理的centos7安装wget_Centos7系统切换Python2到Python3.x的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 66319d电源使用说明书_你用对电暖器
- 下一篇: 服务器2003蓝屏A5修复,求助wind