linux python2 升級 python3

Joshua

Centos/Linux 下升級python2.7至3.5.0

(一) 安裝Python3.5

(1)在安裝python之前,因為linux系統(tǒng)下默認沒有安裝wget,gcc,首先安裝wget,gcc:

1[root@node6 python_scripts]#yuminstallwget2[root@node6 python_scripts]#yuminstallgcc

(2)安裝依賴:

!!!在編譯之前需要安裝一些必須的依賴,否則當報錯的時候還得重新編譯?

yuminstallopenssl-devel? -yyuminstallzlib-devel? -y

(3)利用wget下載python3.5.0的安裝文件并解壓縮:

1[root@node6 python_scripts]#wgethttps://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz2[root@node6 python_scripts]#ls3Python-3.5.0.tgz4[root@node6 python_scripts]#tarxvf Python-3.5.0.tgz 5[root@node6 python_scripts]#ls6Python-3.5.0Python-3.5.0.tgz7[root@node6 Python-3.5.0]# cd Python-3.5.0

(4)編譯:

1[root@node6 Python-3.5.0]# ./configure --prefix=/usr/local/python3.52[root@node6 Python-3.5.0]#make3[root@node6 Python-3.5.0]#makeinstall4[root@node6 Python-3.5.0]mv/usr/bin/python /usr/bin/python_bak5[root@node6 Python-3.5.0]ln-s /usr/local/python3.5/bin/python3.5/usr/bin/python

(5)此時執(zhí)行phthon可以看到3.5已經(jīng)運行:

1[root@node6 python_scripts]# python2Python3.5.0(default, Jul272016,09:34:49) 3[GCC4.8.520150623(Red Hat4.8.5-4)] on linux4Type"help","copyright","credits"or"license"formore information.5>>>

(6) 此時yum的運行會報錯,如下操作可以恢復yum:

修改yum文件

1、cd /usr/bin2、ls-lyum*

會顯示出“yum、yum-builddep、yum-config-manager、yum-debug-dump、yum-debug-restore、yumdownloader、yum-groups-manager”這7個yum開頭的文件,這7個文件都是腳本文件,其第一行為:

#!/usr/bin/python

#!/usr/bin/python -tt

將“python”改為“python2.7”。

我們試著安裝一個文件:

[root@vnode33 bin]# yum install tcpreplay

發(fā)現(xiàn)系統(tǒng)仍然報錯:

Is this ok [y/d/N]: y

Downloading packages:

? File "/usr/libexec/urlgrabber-ext-down", line28? ? except OSError, e:

? ? ? ? ? ? ? ? ? ^SyntaxError: invalid syntax

找到該文件[root@vnode33 libexec]# vi /usr/libexec/urlgrabber-ext-down

修改/usr/bin/python為/usr/bin/python2.7

可以安裝成功了:

Is this ok [y/d/N]: y

Downloading packages:

tcpreplay-4.1.1-1.el7.x86_64.rpm? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |298kB00:00:00? ?

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

? Installing : tcpreplay-4.1.1-1.el7.x86_641/1

? Verifying? : tcpreplay-4.1.1-1.el7.x86_641/1

Installed:

? tcpreplay.x86_64 0:4.1.1-1.el7? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

Complete!

?或者也可以用以下的方法:

./configure --prefix=/opt/python? ? #因為centos自帶python,為了不影響原有系統(tǒng),我這里是另外安裝到新目錄makeinstall? 進行安裝ln-s /opt/python/bin/python3.4/usr/bin/python3? #軟連接到系統(tǒng)path所指向的一個目錄中,使之成為系統(tǒng)命令ln-s /opt/python/bin/pip3.4/usr/bin/pip3ln-s /opt/python/bin/easy_install-3.4/usr/bin/easy_install3


(二)安裝pip3:

(1)首先下載,安裝依賴setuptools

wget--no-check-certificate? https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26tar-zxvf setuptools-19.6.tar.gz

cd setuptools-19.6.tar.gz

python3 setup.py build

python3 setup.py install

(2)安裝pip3

1wget--no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb2tar-zxvf pip-8.0.2.tar.gz3cd pip-8.0.24python3 setup.py build5python3 setup.pyinstall

安裝完成之后我們再來查看python3.5的bin目錄下都有什么東西:

1[root@vnode33 usr]# cd /usr/local/python3.5/bin/2[root@vnode33 bin]#ls32to3? ? ? easy_install? ? ? idle3? ? pip? pip3.5pydoc3.5python3.5python3.5m? ? ? ? python3-config? pyvenv-3.542to3-3.5easy_install-3.5idle3.5pip3? pydoc3? python3? python3.5-config? python3.5m-config? pyvenv

為pip3創(chuàng)建鏈接:

[root@vnode33 bin]#ln-s /usr/local/python3.5/bin/pip3 /usr/bin/pip

現(xiàn)在可以用了:

[root@vnode33 bin]# pip

Usage:?

? pip [options]

Commands:

? install? ? ? ? ? ? ? ? ? ? Install packages.

? download? ? ? ? ? ? ? ? ? ? Download packages.

? uninstall? ? ? ? ? ? ? ? ? Uninstall packages.

? freeze? ? ? ? ? ? ? ? ? ? ? Output installed packages in requirements format.

? list? ? ? ? ? ? ? ? ? ? ? ? List installed packages.

? show? ? ? ? ? ? ? ? ? ? ? ? Show information about installed packages.

? search? ? ? ? ? ? ? ? ? ? ? Search PyPI for packages.

? wheel? ? ? ? ? ? ? ? ? ? ? Build wheels from your requirements.

? hash? ? ? ? ? ? ? ? ? ? ? ? Compute hashes of package archives.

? help? ? ? ? ? ? ? ? ? ? ? ? Show help for commands.

...


(3)安裝Twisted

Linux上設置https proxy并安裝:

本機因從網(wǎng)絡下載包時需要代理,故需要如下命令:

1export https_proxy=135.251.33.31:80802export http_proxy=135.251.33.31:80803export ftp_proxy=135.251.33.31:8080

然后下載安裝:

1wgethttps://pypi.python.org/packages/source/T/Twisted/Twisted-15.2.1.tar.bz22tar-xjvf Twisted-15.2.1.tar.bz23cd Twisted-15.2.1/4python setup.pyinstall

檢查是否安裝成功:

1[root@vnode33 ~]# python2Python3.5.0(default, Feb162017,12:30:39) 3[GCC4.8.520150623(Red Hat4.8.5-4)] on linux4Type"help","copyright","credits"or"license"formore information.5>>> import twisted6>>>


(4)安裝Scrapy

pipinstallScrapy

這個時候安裝成功了,但是運行scrapy時報錯

再重新安裝下Twisted最新的版本:

1wgethttps://twistedmatrix.com/Releases/Twisted/17.1/Twisted-17.1.0.tar.bz22tar-xjvf Twisted-17.1.0.tar.bz23cd Twisted-17.1.0/4python setup.pyinstall

再運行scrapy發(fā)現(xiàn)已經(jīng)好了:

[root@vnode33 bin]# ./scrapy

Scrapy 1.3.2- no active project

Usage:

? scrapy [options] [args]

Available commands:

? bench? ? ? ? Run quick benchmark test

? commands? ? ?

? fetch? ? ? ? Fetch a URL using the Scrapy downloader

? genspider? ? Generate new spider using pre-defined templates

? runspider? ? Run a self-contained spider (without creating a project)

? settings? ? ? Get settings values

? shell? ? ? ? Interactive scraping console

? startproject? Create new project

? version? ? ? Print Scrapy version

? view? ? ? ? ? Open URL in browser, as seen by Scrapy

? [ more ]? ? ? More commands available when run from project directory

Use "scrapy -h"to seemoreinfoabout a command

還需要做一個軟鏈接讓scrapy命令全局可用:

ln-s /usr/local/python3.5/bin/scrapy /usr/bin/scrapy

試試在Python中導入scrapy:

1Python3.5.0(default, Feb162017,12:30:39) 2[GCC4.8.520150623(Red Hat4.8.5-4)] on linux3Type"help","copyright","credits"or"license"formore information.4>>> from scrapy.spider import BaseSpider5__main__:1: ScrapyDeprecationWarning: Module `scrapy.spider` is deprecated, use `scrapy.spiders` instead6>>> from scrapy.spiders import BaseSpider


安裝scrapy過程中有三步不確定是否需要,附錄如下:

1,libxml2

wgetftp://xmlsoft.org/libxml2/libxml2-git-snapshot.tar.gztar-zxvf libxml2-git-snapshot.tar.gz

cd libxml2-2.9.2/./configuremakemakeinstall

2,libxlst

wgethttp://xmlsoft.org/sources/libxslt-1.1.28.tar.gztar-zxvf libxslt-1.1.28.tar.gz

cd libxslt-1.1.28/./configuremakemakeinstall

3,cryptography

wgetftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gztar-zxvf libffi-3.2.1.tar.gz

cd libffi-3.2.1./configuremakemakeinstall

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

wgetftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gztar-zxvf libffi-3.2.1.tar.gz

cd libffi-3.2.1./configuremakemakeinstall


Windows7下安裝scrapy:

先下載python-3.5.2.exe 雙擊文件安裝成功:

然后直接安裝scrapy,找到pip文件的目錄:

cd C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\Scripts

pipinstallscrapy

安裝成功后測試下:

C:\Users\xxx>python

Python 3.5.2(v3.5.2:4def2a2901a5, Jun252016,22:01:18) [MSC v.190032 bit (In

tel)] on win32

Type "help","copyright","credits"or"license"formore information.>>> from scrapy.spiders import BaseSpider>>> exit()


(三)IDLE支持方向鍵:

(1)安裝readline

pipinstallreadline

但是有如下報錯:

gcc: error: readline/libreadline.a: No suchfileor directory

解決方法:

yuminstallreadline-develyuminstallpatch

再重新configure、make、makeinstallpython3.5


出處:https://www.cnblogs.com/z-joshua/p/5710698.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容