之前用了些碎片时间稍微学习了点Python皮毛,然后用Django写了个简单的CURD,
打算尝试部署上VPS,第一步就是先将默认的Python2.7升级到3.7

安装过程很简单,百度出来十有八九都是一样的。
除了处理安装时报错之外还有一些系统原有依赖Python2.7的软件,比如说yum。

简单说下安装

官网下载页
选择Gzipped source tarball 或者 XZ compressed source tarball
建议直接vps上 wget

[root@vultr ~]# wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz

解压

[root@vultr ~]# tar -xvf Python-3.7.0.tar.xz

安装

[root@vultr ~]# cd Python-3.7.0/
[root@vultr Python-3.7.0]# ./configure --prefix=/usr/local/python3.7
[root@vultr Python-3.7.0]# make && make install

问题1

zipimport.ZipImportError: can't decompress data; zlib not available

装一个就完事了

[root@vultr Python-3.7.0]# yum -y install zlib*

问题2

ModuleNotFoundError: No module named '_ctypes'

也是安装完事

[root@vultr Python-3.7.0]# yum -y install libffi-devel

替换原有Python软链接

第一行的 /usr/bin/python 就是你直接输入 python 时调用的文件
后面-> python2 是软链接,前面没有路径就是./
所以是指向第二行的 /usr/bin/python2 这里第二行又指向了 python2.7

[root@vultr Python-3.7.0]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root    7 May 16 06:07 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 May 16 06:07 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Apr  9 22:31 /usr/bin/python2.7

所以先把第一行的软连删掉

[root@vultr Python-3.7.0]# rm -rf /usr/bin/python

然后把方才安装好的python3.7挂上去

[root@vultr Python-3.7.0]# ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python

把pip也挂上去吧

[root@vultr Python-3.7.0]# ln -s /usr/local/python3.7/bin/pip3 /usr/bin/pip

解决yum问题

此时用yum会报错,主要改两个地方

/usr/bin/yum
/usr/libexec/urlgrabber-ext-down

使用vi、vim打开将

#!/usr/bin/python 

改成

#!/usr/bin/python2.7

最后执行下

[root@vultr Python-3.7.0]# yum clean all

这里主要是将一些yum用到python的地方指引向旧版本的python
因为/usr/bin/python已经被我们通过上面的步骤替换成python3.7

pip遇到问题

安装好Python,配置好pip后开始安装django,在执行pip install Django时提示下面错误:

[root@vultr /]# pip install Django
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting Django
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/django/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/django/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/django/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/django/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/django/
  Could not fetch URL https://pypi.org/simple/django/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/django/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
  Could not find a version that satisfies the requirement Django (from versions: )
No matching distribution found for Django
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

首先考虑到可能是configure时可能需要带上ssl模块的配置项
然后也搜索了下python3.7所需要的openssl版本是1.0.2
查看版本命令

openssl version

因为我服务器已经是1.0.2,按道理是不需要更新的

正打算从configure开始重新安装python3.7,这次带上--with-openssl=/usr/local/openssl
然后发现我的/usr/local/openssl里面啥都没 但是/usr/bin/倒有openssl
不纠结,直接重新装个最新的1.1.1

[root@vultr ~]# wget https://www.openssl.org/source/openssl-1.1.1-pre8.tar.gz
[root@vultr ~]# tar -zxvf ./openssl-1.1.1-pre8.tar.gz
[root@vultr ~]# cd openssl-1.1.1-pre8
[root@vultr openssl-1.1.1-pre8]# ./config --prefix=/usr/local/openssl no-zlib #不需要zlib
[root@vultr openssl-1.1.1-pre8]# make
[root@vultr openssl-1.1.1-pre8]# make install

备份下原来的openssl

[root@vultr openssl-1.1.1-pre8]# mv /usr/bin/openssl /usr/bin/openssl.bak
[root@vultr openssl-1.1.1-pre8]# mv /usr/include/openssl/ /usr/include/openssl.bak

重新挂下软链

#将安装好的openssl 的openssl命令软连到/usr/bin/openssl
[root@vultr openssl-1.1.1-pre8]# ln -s /usr/local/openssl/include/openssl /usr/include/openssl

#软链到升级后的libssl.so
[root@vultr openssl-1.1.1-pre8]# ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so

#将安装好的openssl命令软连到/usr/bin/openssl
[root@vultr openssl-1.1.1-pre8]# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl

修改系统配置

#写入openssl库文件的搜索路径
[root@vultr openssl-1.1.1-pre8]# echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
#使修改后的/etc/ld.so.conf生效 
[root@vultr openssl-1.1.1-pre8]# ldconfig -v

查看版本

[root@vultr openssl-1.1.1-pre8]# openssl version
OpenSSL 1.1.1-pre8 (beta) 20 Jun 2018

没毛病

最后重新配置、安装python3.7

回到python3.7文件夹里

[root@vultr Python-3.7.0]# ./configure --prefix=/usr/local/python3.7 --with-openssl=/usr/local/openssl
[root@vultr Python-3.7.0]# make && make install

安装Django

[root@vultr ~] pip install Django
Collecting Django
  Downloading https://files.pythonhosted.org/packages/39/b0/2138c31bf13e17afc32277239da53e9dfcce27bac8cb68cf1c0123f1fdf5/Django-2.2.3-py3-none-any.whl (7.5MB)
    100% |████████████████████████████████| 7.5MB 2.3MB/s 
Collecting pytz (from Django)
  Downloading https://files.pythonhosted.org/packages/3d/73/fe30c2daaaa0713420d0382b16fbb761409f532c56bdcc514bf7b6262bb6/pytz-2019.1-py2.py3-none-any.whl (510kB)
    100% |████████████████████████████████| 512kB 9.5MB/s 
Collecting sqlparse (from Django)
  Downloading https://files.pythonhosted.org/packages/ef/53/900f7d2a54557c6a37886585a91336520e5539e3ae2423ff1102daf4f3a7/sqlparse-0.3.0-py2.py3-none-any.whl
Installing collected packages: pytz, sqlparse, Django
Successfully installed Django-2.2.3 pytz-2019.1 sqlparse-0.3.0

以上

标签: none

添加新评论