Centos7.4 python2.7 升级到python3.7

it2022-05-05  156

在学习django 过程中,看了好多教程全是2.0以上版本,系统自带的python2.7 并不支持。只好升级下python版本

原有版本

[root@localhost HelloWorld]# python -V Python 2.7.5

下载

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

解压安装

# tar zxf Python-3.7.4.tgz # cd Python-3.7.4 // 编译前安装gcc,已安装忽略 # yum -y install gcc # ./configure // 执行完会到最后会提示如下信息。 If you want a release build with all stable optimizations active (PGO, etc), please run ./configure --enable-optimizations // 根据提示建议重新执行 # ./configure --enable-optimizations // 安装前先安装一下依赖包,已安装忽略 # yum install libffi-devel zlib* -y //3.7版本需要安装libffi-devel包否则报错,ModuleNotFoundError: No module named '_ctypes' # make && make install

配置python环境

# cd /usr/bin/ # mv python python.bak # ln -sf /usr/local/bin/python3.7 /usr/bin/python # ll python lrwxrwxrwx. 1 root root 24 Jul 18 05:34 python -> /usr/local/bin/python3.7 # python -V Python 3.7.4

修改依赖python的文件

# vim /usr/bin/yum # vim /usr/libexec/urlgrabber-ext-down // 将以上两个文件开头 /usr/bin/python 修改为 /usr/bin/python2.7

在安装django的过程中又报错 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")':

原因在编译python3.7的时候没有加入--with-ssl 参数 加入重新编译即可

# cd /root/Python-3.7.4 # make clean # ./configure --enable-optimizations --with-ssl

编译完成后会出现如下警告;

configure: WARNING: unrecognized options: --with-ssl

新的版本可能已经不支持这个参数了

查看setup.py 源码

# Find OpenSSL includes ssl_incs = find_file( 'openssl/ssl.h', inc_dirs, openssl_includes ) if ssl_incs is None: return None, None # OpenSSL 1.0.2 uses Kerberos for KRB5 ciphers krb5_h = find_file( 'krb5.h', inc_dirs, ['/usr/kerberos/include'] ) if krb5_h: ssl_incs.extend(krb5_h)

可以看到是在查找‘openssl/ssh.h’   而且还需要krb5

来看一下 inc_dirs 是哪个目录 

system_include_dirs = ['/usr/include']         # lib_dirs and inc_dirs are used to search for files;         # if a file is found in one of those directories, it can         # be assumed that no additional -I,-L directives are needed.         if not cross_compiling:             lib_dirs = self.compiler.library_dirs + system_lib_dirs             inc_dirs = self.compiler.include_dirs + system_include_dirs         else:             # Add the sysroot paths. 'sysroot' is a compiler option used to             # set the logical path of the standard system headers and             # libraries.             lib_dirs = (self.compiler.library_dirs +                         sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs))             inc_dirs = (self.compiler.include_dirs +                         sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'),                                       system_include_dirs))

综合看出 ssl.h 的路径应该为

/usr/includ/openssl/ssl.h

查找一下看

find / -name ssl.h

然而并没有

看了好多文档找了一个命令完美解决

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

然后重新编译完美解决

这次没有加--with-ssl,仔细看编译过程的提示,3.7版本已经默认编译ssl了,看上面源码也可以看到如果没有则返回空。没有任何提示

# ./configure --enable-optimizations hecking for X509_VERIFY_PARAM_set1_host in libssl... yes checking for --with-ssl-default-suites... python

重新安装

# make && make install

检查一下

[root@localhost bin]# python Python 3.7.4 (default, Jul 18 2019, 23:47:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ssl >>> # python -m pip install django

 

参考文献:

https://www.cnblogs.com/minglee/p/9232673.html

https://chowyi.com/源码编译安装Python3及问题解决/


最新回复(0)