首先去python官网下载python3的源码包,网址:https://www.python.org/

python 31 对应的pip版本 python3.6 pip_linux

 

 或者直接wget下载

wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz

 

#安装python时可能需要依赖包,安装之前最好先安装一下下面的依赖包,网上找到两条命令如下:
[root@qinhan bin]# yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel libffi-devel
           yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

 

没有网络的话,哥教你怎么手动安装,有网络可以用yum pip安装的话可以不看

------------------------------------------------zlib 安装开始--------------------------------------------------------------------------------------   

下载zlib1.2.11   http://www.zlib.net/

我下载的是1.2.11版本

./configure --prefix=/usr/local/zlib1_2_11

make 

make install

 ------------------------------------------------zlib 安装结束--------------------------------------------------------------------------------------
------------------------------------------------zlib devel 安装开始--------------------------------------------------------------------------------------
http://rpmfind.net/linux/rpm2html/search.php?query=zlib-devel
------------------------------------------------zlib devel 安装开始--------------------------------------------------------------------------------------

zlib-devel包可以到以下连接查找,那么如何找到对应的版本呢我们使用rpm -qi  zlib 可以找到对应的版本信息,随后zlib-devel rpm只要根据版本信息去找就可以了,接下来所有的rpm包安装方法参考zlib

http://rpmfind.net/linux/rpm2html/search.php?query=zlib-devel

 

[root@localhost software_download]# rpm -qi zlib
Name : zlib
Version : 1.2.7
Release : 17.el7
Architecture: x86_64
Install Date: 2018年03月13日 星期二 18时53分26秒
Group : System Environment/Libraries
Size : 185710
License : zlib and Boost
Signature : RSA/SHA256, 2016年11月21日 星期一 05时05分07秒, Key ID 24c6a8a7f4a80eb5
Source RPM : zlib-1.2.7-17.el7.src.rpm
Build Date : 2016年11月06日 星期日 02时09分15秒
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://www.zlib.net/
Summary : The compression and decompression library
Description :
Zlib is a general-purpose, patent-free, lossless data compression
library which is used by many different programs.

 

随后根据版本包找到对应的rpm包

[root@localhost software_download]# ls
zlib-1.2.11 zlib-1.2.11.tar.gz zlib-devel-1.2.7-17.el7.x86_64.rpm

执行rpm -ivh zlib-devel-1.2.7-17.el7.x86_64.rpm 安装

root@localhost software_download]# rpm -ivh zlib-devel-1.2.7-17.el7.x86_64.rpm
警告:zlib-devel-1.2.7-17.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:zlib-devel-1.2.7-17.el7 ################################# [100%]

------------------------------------------------zlib devel 安装结束--------------------------------------------------------------------------------------

 

------------------------------------------------bzip2 devel 安装开始--------------------------------------------------------------------------------------

 

bzip 系统已经安装了,如果没安装,请自行百度

[root@localhost software_download]# rpm -qi bzip2
Name : bzip2
Version : 1.0.6
Release : 13.el7
Architecture: x86_64
Install Date: 2018年03月13日 星期二 18时54分02秒
Group : Applications/File
Size : 83791
License : BSD
Signature : RSA/SHA256, 2015年11月25日 星期三 22时18分37秒, Key ID 24c6a8a7f4a80eb5
Source RPM : bzip2-1.0.6-13.el7.src.rpm
Build Date : 2015年11月20日 星期五 13时04分52秒
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://www.bzip.org/
Summary : A file compression utility
Description :
Bzip2 is a freely available, patent-free, high quality data compressor.
Bzip2 compresses files to within 10 to 15 percent of the capabilities
of the best techniques available. However, bzip2 has the added benefit
of being approximately two times faster at compression and six times
faster at decompression than those techniques. Bzip2 is not the
fastest compression utility, but it does strike a balance between speed
and compression capability.

Install bzip2 if you need a compression utility.

 

 

[root@localhost software_download]# rpm -ivh bzip2-devel-1.0.6-13.el7.x86_64.rpm
警告:bzip2-devel-1.0.6-13.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:bzip2-devel-1.0.6-13.el7 ################################# [100%]
[root@localhost software_download]#

------------------------------------------------bzip2 devel 安装结束--------------------------------------------------------------------------------------

 

------------------------------------------------openssl devel 安装开始--------------------------------------------------------------------------------------

 openssl已经安装

[root@localhost software_download]# rpm -qi openssl
Name : openssl
Epoch : 1
Version : 1.0.1e
Release : 60.el7
Architecture: x86_64
Install Date: 2018年03月13日 星期二 18时54分55秒
Group : System Environment/Libraries
Size : 1611673
License : OpenSSL
Signature : RSA/SHA256, 2016年11月21日 星期一 03时48分52秒, Key ID 24c6a8a7f4a80eb5
Source RPM : openssl-1.0.1e-60.el7.src.rpm
Build Date : 2016年11月06日 星期日 11时49分26秒
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://www.openssl.org/
Summary : Utilities from the general purpose cryptography library with TLS implementation
Description :
The OpenSSL toolkit provides support for secure communications between
machines. OpenSSL includes a certificate management tool and shared
libraries which provide various cryptographic algorithms and
protocols.

这个openssl-devel比较麻烦,用Yum看了下需要各种更新,先不装

依赖关系解决

====================================================================================================================================================
Package 架构 版本 源 大小
====================================================================================================================================================
正在安装:
openssl-devel x86_64 1:1.0.2k-12.el7 base 1.5 M
为依赖而安装:
keyutils-libs-devel x86_64 1.5.8-3.el7 base 37 k
krb5-devel x86_64 1.15.1-19.el7 updates 269 k
libcom_err-devel x86_64 1.42.9-12.el7_5 updates 31 k
libselinux-devel x86_64 2.5-12.el7 base 186 k
libsepol-devel x86_64 2.5-8.1.el7 base 77 k
libverto-devel x86_64 0.2.5-4.el7 base 12 k
pcre-devel x86_64 8.32-17.el7 base 480 k
为依赖而更新:
e2fsprogs x86_64 1.42.9-12.el7_5 updates 699 k
e2fsprogs-libs x86_64 1.42.9-12.el7_5 updates 167 k
krb5-libs x86_64 1.15.1-19.el7 updates 747 k
krb5-workstation x86_64 1.15.1-19.el7 updates 814 k
libcom_err x86_64 1.42.9-12.el7_5 updates 41 k
libkadm5 x86_64 1.15.1-19.el7 updates 175 k
libselinux x86_64 2.5-12.el7 base 162 k
libselinux-python x86_64 2.5-12.el7 base 235 k
libselinux-utils x86_64 2.5-12.el7 base 151 k
libsepol x86_64 2.5-8.1.el7 base 297 k
libss x86_64 1.42.9-12.el7_5 updates 45 k
openssl x86_64 1:1.0.2k-12.el7 base 492 k
openssl-libs x86_64 1:1.0.2k-12.el7 base 1.2 M
pcre x86_64 8.32-17.el7 base 422 k

事务概要
====================================================================================================================================================
安装 1 软件包 (+ 7 依赖软件包)
升级 ( 14 依赖软件包)

------------------------------------------------openssl devel 安装结束--------------------------------------------------------------------------------------

 

 

------------------------------------------------ncurses devel 安装开始--------------------------------------------------------------------------------------

[root@localhost software_download]# rpm -qi ncurses
Name : ncurses
Version : 5.9
Release : 14.20130511.el7_4
Architecture: x86_64
Install Date: 2018年07月05日 星期四 11时52分01秒
Group : System Environment/Base
Size : 439378
License : MIT
Signature : RSA/SHA256, 2017年09月07日 星期四 20时43分11秒, Key ID 24c6a8a7f4a80eb5
Source RPM : ncurses-5.9-14.20130511.el7_4.src.rpm
Build Date : 2017年09月07日 星期四 06时08分29秒
Build Host : c1bm.rdu2.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://invisible-island.net/ncurses/ncurses.html
Summary : Ncurses support utilities
Description :
The curses library routines are a terminal-independent method of
updating character screens with reasonable optimization. The ncurses
(new curses) library is a freely distributable replacement for the
discontinued 4.4 BSD classic curses library.

This package contains support utilities, including a terminfo compiler
tic, a decompiler infocmp, clear, tput, tset, and a termcap conversion
tool captoinfo.

 

[root@localhost software_download]# rpm -ivh ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:ncurses-devel-5.9-14.20130511.el7################################# [100%]
[root@localhost software_download]#

------------------------------------------------ncurses devel 安装结束--------------------------------------------------------------------------------------

 

 

------------------------------------------------sqlite devel 安装开始--------------------------------------------------------------------------------------

[root@localhost software_download]# rpm -qi sqlite

Name : sqlite
Version : 3.7.17
Release : 8.el7
Architecture: x86_64
Install Date: 2018年03月13日 星期二 18时53分31秒
Group : Applications/Databases
Size : 814231
License : Public Domain
Signature : RSA/SHA256, 2015年11月25日 星期三 23时40分39秒, Key ID 24c6a8a7f4a80eb5
Source RPM : sqlite-3.7.17-8.el7.src.rpm
Build Date : 2015年11月20日 星期五 16时45分59秒
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://www.sqlite.org/
Summary : Library that implements an embeddable SQL database engine
Description :
SQLite is a C library that implements an SQL database engine. A large
subset of SQL92 is supported. A complete database is stored in a
single disk file. The API is designed for convenience and ease of use.
Applications that link against SQLite can enjoy the power and
flexibility of an SQL database without the administrative hassles of
supporting a separate database server. Version 2 and version 3 binaries
are named to permit each to be installed on a single host

[root@localhost software_download]# rpm -ivh sqlite-devel-3.7.17-8.el7.x86_64.rpm
警告:sqlite-devel-3.7.17-8.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:sqlite-devel-3.7.17-8.el7 ################################# [100%]

------------------------------------------------sqlite devel 安装结束--------------------------------------------------------------------------------------

 

------------------------------------------------readline devel 安装开始--------------------------------------------------------------------------------------

[root@localhost software_download]# rpm -qi readline
Name : readline
Version : 6.2
Release : 9.el7
Architecture: x86_64
Install Date: 2018年03月13日 星期二 18时53分31秒
Group : System Environment/Libraries
Size : 460408
License : GPLv3+
Signature : RSA/SHA256, 2014年07月04日 星期五 12时47分34秒, Key ID 24c6a8a7f4a80eb5
Source RPM : readline-6.2-9.el7.src.rpm
Build Date : 2014年06月10日 星期二 02时52分20秒
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html
Summary : A library for editing typed command lines
Description :
The Readline library provides a set of functions that allow users to
edit command lines. Both Emacs and vi editing modes are available. The
Readline library includes additional functions for maintaining a list
of previously-entered command lines for recalling or editing those
lines, and for performing csh-like history expansion on previous
commands.

 

网上只有6.2.10版本,把readline-6.2-10.el7.x86_64.rpm 下载下来进行更新操作

[root@localhost software_download]# rpm -Uvh readline-6.2-10.el7.x86_64.rpm 

 

[root@localhost software_download]# rpm -ivh readline-devel-6.2-10.el7.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:readline-devel-6.2-10.el7 ################################# [100%]

 

------------------------------------------------readline devel 安装结束--------------------------------------------------------------------------------------

 

------------------------------------------------tk devel 安装开始--------------------------------------------------------------------------------------

[root@localhost software_download]# rpm -qi tk
未安装软件包 tk

 

下载tk-8.5.13-6.el7.x86_64.rpm 

[root@localhost software_download]# rpm -ivh tk-8.5.13-6.el7.x86_64.rpm
错误:依赖检测失败:
libtcl8.5.so()(64bit) 被 tk-1:8.5.13-6.el7.x86_64 需要
tcl = 1:8.5.13 被 tk-1:8.5.13-6.el7.x86_64 需要
tcl >= 1:8.5.13-5 被 tk-1:8.5.13-6.el7.x86_64 需要

下载tcl-8.5.13-8.el7.x86_64.rpm

[root@localhost software_download]# rpm -ivh tcl-8.5.13-8.el7.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:tcl-1:8.5.13-8.el7 ################################# [100%]

 

[root@localhost software_download]# rpm -ivh tk-8.5.13-6.el7.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:tk-1:8.5.13-6.el7 ################################# [100%]

tk-devel依赖包太多,暂不安装或者用Yum安装吧

依赖关系解决

====================================================================================================================================================
Package 架构 版本 源 大小
====================================================================================================================================================
正在安装:
tk-devel x86_64 1:8.5.13-6.el7 base 488 k
为依赖而安装:
expat-devel x86_64 2.1.0-10.el7_3 base 57 k
fontconfig-devel x86_64 2.10.95-11.el7 base 128 k
freetype-devel x86_64 2.4.11-15.el7 base 356 k
libX11-devel x86_64 1.6.5-1.el7 base 980 k
libXau-devel x86_64 1.0.8-2.1.el7 base 14 k
libXft-devel x86_64 2.3.2-2.el7 base 19 k
libXrender-devel x86_64 0.9.10-1.el7 base 17 k
libxcb-devel x86_64 1.12-1.el7 base 1.0 M
tcl-devel x86_64 1:8.5.13-8.el7 base 165 k
xorg-x11-proto-devel noarch 7.7-20.el7 base 284 k
为依赖而更新:
expat x86_64 2.1.0-10.el7_3 base 81 k
fontconfig x86_64 2.10.95-11.el7 base 229 k
freetype x86_64 2.4.11-15.el7 base 392 k
libX11 x86_64 1.6.5-1.el7 base 606 k
libX11-common noarch 1.6.5-1.el7 base 164 k
libXrender x86_64 0.9.10-1.el7 base 26 k
libxcb x86_64 1.12-1.el7 base 211 k

------------------------------------------------tk devel 安装结束--------------------------------------------------------------------------------------

 

 

 

 开始正式安装python3.6

释放文件:

tar -xzvf Python-3.6.4.tgz -C  /tmp

进入目录:

cd /tmp/Python-3.6.4

配置安装目录,其他选项一般默认即可:

./configure --prefix=/usr/local/python3            --设立自己的python3目录,所有的python3 的bin  lib等文件都放在/usr/local/python3目录下

接下来编译源码:

make

执行安装:

make install

 

python3.6程序的执行文件:/usr/local/python3/bin/python3.6

python3.6应用程序目录:/usr/local/python3/lib/python3.6

pip3的执行文件:/usr/local/python3/bin/pip3

pyenv3的执行文件:/usr/local/python3/bin/pyenv-3.6

 

做一个python3的软链接

[root@localhost bin]# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
[root@localhost bin]# python3
Python 3.6.4 (default, Jul 5 2018, 12:35:43)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()

 

做一个pip3的软链接

[root@localhost bin]# ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3
[root@localhost bin]# pip3

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.
check Verify installed packages have compatible dependencies.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.

 

至此python3已经安装完成了 

 

 

以下这段看到的网上把原来的python2软链接连接到python3上了,在生产环境千万不要这么做,测试环境可以随便玩

cd /usr/bin

[root@qinhan bin]# ll *python*
lrwxrwxrwx. 1 root root 7 Dec 15 2016 python -> python2
lrwxrwxrwx. 1 root root 9 Dec 15 2016 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov 20 2015 python2.7

 

#更改/usr/bin/python链接

[root@qinhan bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python
[root@qinhan bin]# ln -s /usr/local/bin/python3.6 /usr/bin/python3
[root@qinhan bin]# ll *python*
lrwxrwxrwx 1 root root 24 Jan 11 10:10 python -> /usr/local/bin/python3.6
lrwxrwxrwx. 1 root root 9 Dec 15 2016 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov 20 2015 python2.7
lrwxrwxrwx 1 root root 24 Jan 11 10:10 python3 -> /usr/local/bin/python3.6
lrwxrwxrwx. 1 root root 7 Dec 15 2016 python.backup -> python2

 

#查看最新python版本

[root@qinhan bin]# python -V
Python 3.6.4

 

#更改yum脚本的python依赖

[root@qinhan bin]# cd /usr/bin

[root@qinhan bin]# view yum

修改#!/usr/bin/python为#!/usr/bin/python2

 

#修改urlgrabber配置文件

[root@qinhan bin]# view /usr/libexec/urlgrabber-ext-down

修改##! /usr/bin/python 为#! /usr/bin/python2

 

#查看pip是否可以正常使用

[root@qinhan bin]# pip
-bash: pip: command not found
[root@qinhan bin]# pip3

Usage:
pip <command> [options]

.......

 

 

#如果还是报command not found,可以做个软连接如下所示,这个是另外的服务器27.100上的

[root@qinhan /]# cd usr/bin
[root@qinhan bin]# pwd
/usr/bin
[root@qinhan bin]# ll pip*
lrwxrwxrwx 1 root root 27 Jan 10 16:45 pip3 -> /usr/local/python3/bin/pip3
[root@qinhan bin]#