redhat 安装python3

 

一、首先,官网下载python3的所需版本。

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

想下载到那个文件夹下就先进入到那个文件夹下——cd /home/download

二、然后,解压缩文件》

tar -xvf Python-3.6.0.tgz

三、创建安装文件的路径。

mkdir /usr/local/python3

四、编译。

进入到解压目录下

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

五、安装。

1、make

2、make install

 make install 报错:zipimport.ZipImportError: can’t decompress data

a、打开终端,输入一下命令安装zlib相关依赖包:

    yum -y install zlib*
  
  redhat 配置yum源:
  

    进入到 /etc/yum.repos.d, 其中有个rhel-source.repo, 先改名为rhel-source.repo.bak,做个备份

    然后新建个 rhel-source.repo

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[updates]
name=CentOS-6 - Updates - 163.com
baseurl=http://mirrors.163.com/centos/6/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6
  
#additional packages that may be useful
[extras]
name=CentOS-6 - Extras - 163.com
baseurl=http://mirrors.163.com/centos/6/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

  

 
     清理并重建缓存
    yum clean all
    yum makecache
 
    更新yum
    yum update

  

b、进入 python安装包,修改Module路径的setup文件:

vim Module/Setup 

找到一下一行代码,去掉注释:

#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
去掉注释
     zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

重新安装

3、完毕

六、创建新版本的软连接。

1、修改旧版本

mv /usr/bin/python /usr/bin/python_bak

2、创建新的软连接

ln -s /usr/local/python3/bin/python3 /usr/bin/python

3、检查python的版本

python -V

python-3.6.0

软连接创建成功

七、配置成功后,pip3用不了,需进一步配置。

1、export PATH=$PATH:$HOME/bin:

2、export PATH=$PATH:$HOME/bin:/usr/local/python3/bin

3、完成

这时pip3就可以使用了。

注:升级完成后,yum无法使用,需更改yum指向源2.7版本

  vim /usr/bin/yum,第一句更改为原版本路径即可

后续问题:

pip3安装包时报错,pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

解决方案:

>>> import ssl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python3/lib/python3.6/ssl.py", line 60, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: No module named _ssl
>>>

2. 查看openssl安装包,发现缺少openssl-devel包
[root@localhost ~]# rpm -aq|grep openssl
openssl-1.0.1e-57.el6.x86_64

3. yum安装openssl-devel
[root@localhost ~]# yum install openssl-devel -y


#查看安装结果
[root@localhost ~]# rpm -aq|grep openssl

openssl-1.0.1e-57.el6.x86_64
openssl-devel-1.0.1e-57.el6.x86_64

4. 重新编译python
#修改Setup文件
vi /usr/software/Python-2.7.5/Modules/Setup
#修改结果如下:
# Socket module helper for socket(2)
_socket socketmodule.c timemodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
_ssl _ssl.c
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl
-L$(SSL)/lib -lssl -lcrypto


5. 重新编译
make
make install

6. 测试,已可正常使用。
[root@localhost ~]# python
Python 3.6.0 (default, Jan 25 2018, 22:31:55)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>

原文地址:https://www.cnblogs.com/ityunwei/p/13750100.html