Centos/Linux 下升级python2.7至3.5.0

(一) 安装Python3.5

(1)在安装python之前,因为linux系统下默认没有安装wget,gcc,首先安装wget,gcc:

1 [root@node6 python_scripts]# yum install wget
2 [root@node6 python_scripts]# yum install gcc

(2)安装依赖:

!!!在编译之前需要安装一些必须的依赖,否则当报错的时候还得重新编译 

yum install openssl-devel   -y
yum install zlib-devel  -y

(3)利用wget下载python3.5.0的安装文件并解压缩:

1 [root@node6 python_scripts]#wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
2 [root@node6 python_scripts]# ls
3 Python-3.5.0.tgz
4 [root@node6 python_scripts]# tar xvf Python-3.5.0.tgz 
5 [root@node6 python_scripts]# ls
6 Python-3.5.0 Python-3.5.0.tgz
7 [root@node6 Python-3.5.0]# cd Python-3.5.0

(4)编译:

1 [root@node6 Python-3.5.0]# ./configure --prefix=/usr/local/python3.5 
2 [root@node6 Python-3.5.0]# make
3 [root@node6 Python-3.5.0]# make install
4 [root@node6 Python-3.5.0]mv /usr/bin/python /usr/bin/python_bak
5 [root@node6 Python-3.5.0]ln -s /usr/local/python3.5/bin/python3.5 /usr/bin/python

(5)此时执行phthon可以看到3.5已经运行:

1 [root@node6 python_scripts]# python
2 Python 3.5.0 (default, Jul 27 2016, 09:34:49) 
3 [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>>

(6) 此时yum的运行会报错,如下操作可以恢复yum:

修改yum文件

1、cd /usr/bin
2ls -l yum*

会显示出“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

发现系统仍然报错:

Is this ok [y/d/N]: y
Downloading packages:
  File "/usr/libexec/urlgrabber-ext-down", line 28
    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                                                                                                                | 298 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : tcpreplay-4.1.1-1.el7.x86_64                                                                                                                        1/1 
  Verifying  : tcpreplay-4.1.1-1.el7.x86_64                                                                                                                        1/1 

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

Complete!

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

./configure --prefix=/opt/python    #因为centos自带python,为了不影响原有系统,我这里是另外安装到新目录

make install  进行安装

ln -s /opt/python/bin/python3.4 /usr/bin/python3  #软连接到系统path所指向的一个目录中,使之成为系统命令

ln -s /opt/python/bin/pip3.4 /usr/bin/pip3

ln -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=c607dd118eae682c44ed146367a17e26
tar -zxvf setuptools-19.6.tar.gz
cd setuptools-19.6.tar.gz
python3 setup.py build
python3 setup.py install

(2)安装pip3

1 wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
2 tar -zxvf pip-8.0.2.tar.gz
3 cd pip-8.0.2
4 python3 setup.py build
5 python3 setup.py install

安装完成之后我们再来查看python3.5的bin目录下都有什么东西:

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

为pip3创建链接:

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

现在可以用了:

[root@vnode33 bin]# pip

Usage:   
  pip <command> [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并安装:

本机因从网络下载包时需要代理,故需要如下命令:

1 export https_proxy=135.251.33.31:8080
2 export http_proxy=135.251.33.31:8080
3 export ftp_proxy=135.251.33.31:8080

然后下载安装:

1 wget https://pypi.python.org/packages/source/T/Twisted/Twisted-15.2.1.tar.bz2
2 tar -xjvf Twisted-15.2.1.tar.bz2
3 cd Twisted-15.2.1/
4 python setup.py install

检查是否安装成功:

1 [root@vnode33 ~]# python
2 Python 3.5.0 (default, Feb 16 2017, 12:30:39) 
3 [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import twisted
6 >>> 

 

(4)安装Scrapy

pip install Scrapy

这个时候安装成功了,但是运行scrapy时报错

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

1 wget https://twistedmatrix.com/Releases/Twisted/17.1/Twisted-17.1.0.tar.bz2
2 tar -xjvf Twisted-17.1.0.tar.bz2
3 cd Twisted-17.1.0/
4 python setup.py install

再运行scrapy发现已经好了:

[root@vnode33 bin]# ./scrapy 
Scrapy 1.3.2 - no active project

Usage:
  scrapy <command> [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 <command> -h" to see more info about a command

还需要做一个软链接让scrapy命令全局可用:

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

试试在Python中导入scrapy:

1 Python 3.5.0 (default, Feb 16 2017, 12:30:39) 
2 [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
3 Type "help", "copyright", "credits" or "license" for more information.
4 >>> from scrapy.spider import BaseSpider
5 __main__:1: ScrapyDeprecationWarning: Module `scrapy.spider` is deprecated, use `scrapy.spiders` instead
6 >>> from scrapy.spiders import BaseSpider

 

安装scrapy过程中有三步不确定是否需要,附录如下:

1,libxml2

wget ftp://xmlsoft.org/libxml2/libxml2-git-snapshot.tar.gz
tar -zxvf libxml2-git-snapshot.tar.gz
cd libxml2-2.9.2/
./configure
make
make install

2,libxlst

wget http://xmlsoft.org/sources/libxslt-1.1.28.tar.gz
tar -zxvf libxslt-1.1.28.tar.gz
cd libxslt-1.1.28/
./configure
make
make install

3,cryptography

wget ftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gz
tar -zxvf libffi-3.2.1.tar.gz
cd libffi-3.2.1
./configure
make
make install
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
wget ftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gz
tar -zxvf libffi-3.2.1.tar.gz
cd libffi-3.2.1
./configure
make
make install

 

Windows7下安装scrapy:

先下载python-3.5.2.exe 双击文件安装成功:

然后直接安装scrapy,找到pip文件的目录:

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

安装成功后测试下:

C:\Users\xxx>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from scrapy.spiders import BaseSpider
>>> exit()

 

(三)IDLE支持方向键:

(1)安装readline

pip install readline

但是有如下报错:

gcc: error: readline/libreadline.a: No such file or directory

解决方法:

yum install readline-devel
yum install patch
再重新configure、makemake install python3.5

 

posted on 2016-07-27 12:56  _Joshua  阅读(2719)  评论(0编辑  收藏  举报

导航