前言

自己在Linux安装过很多次Python,每次都会由于各种各样的原因出现很多问题,很无奈

安装流程

  • 这里先看看有哪些可用版本可用下载
  • 下载
sudo wget http://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
  • 解压
sudo tar -xzvf Python-3.7.0.tgz
  • 自定义安装目录
sudo mkdir /usr/local/python3 
  • 编译、安装
cd Python-3.7.0
sudo ./configure --prefix=/usr/local/python3
sudo make 
sudo make install
  • 预安装所需要的库
sudo yum -y install zlib zlib-devel
sudo yum -y install bzip2 bzip2-devel
sudo yum -y install ncurses ncurses-devel
sudo yum -y install readline readline-devel
sudo yum -y install openssl openssl-devel
sudo yum -y install openssl-static
sudo yum -y install xz lzma xz-devel
sudo yum -y install sqlite sqlite-devel
sudo yum -y install gdbm gdbm-devel
sudo yum -y install tk tk-devel

这些包安装好,后面出现的问题就比较少了
源码安装程序主要出现问题的就是make install这一步

出现的问题(如果上面预安装的软件都安装了,下面好多问题就没有了)

  • Python build finished, but the necessary bits to build these modules were _bz2 _curses _curses_panel _dbm _gdbm _hashlib _lzma _sqlite3 _ssl _tkinter _uuid readline zlib
    解决方法:
sudo yum install _bz2  _curses  _curses_panel  _dbm    _gdbm  _hashlib _lzma  _sqlite3  _ssl  _tkinter  _uuid   readline zlib  

然后再sudo make install,一如既往的报错

  • zipimport.ZipImportError: can’t decompress data; zlib not available
    我在上面明显安装了,但是还报没找到,那就用老套路
sudo yum install zlib* -y

此处有可能会报

Error:  Multilib version problems found. This often means that the root
       cause is something else and multilib version checking is just
       pointing out that there is a problem.

解决方法:

sudo yum install  -y zlib* --setopt=protected_multilib=false

然后再sudo make install,一如既往的报错

  • ModuleNotFoundError: No module named ‘_ctypes’
    网上大部分人都是通过如下解决的:
sudo yum install libffi-devel -y

但是我就不行,可能是yum源的问题,于是我又使用了惯用的招数

sudo yum install libffi* -y

总算成功了

注意:每次yum完,无论失败与否都要执行make install

编译、安装完成后,建立软连接

sudo ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
sudo ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3

测试Python3.7

[analysis@master01 ~]$ python3
Python 3.7.0 (default, May 14 2019, 16:35:35) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

[analysis@master01 ~]$ pip3 -V
pip 10.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
[analysis@master01 ~]$ 

卸载

先找到所有的python3相关的文件

whereis python3*

然后删除

#移除Python3
rm -rf /usr/local/python37
#移除Python3软链接
rm -rf /usr/bin/python3

Require TLS/SSL

本来以为很开心的就可以玩耍了,谁知道,使用pip3 install pandas竟然报错:

   pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting virtualenv
  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/virtualenv/
  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/virtualenv/
  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/virtualenv/
  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/virtualenv/
  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/virtualenv/
  Could not fetch URL https://pypi.org/simple/virtualenv/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/virtualenv/ (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 virtualenv (from versions: )
No matching distribution found for virtualenv
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

原因

系统版本centos6.5,其中openssl的版本为OpenSSL 1.0.1e-fips 11 Feb 2013,而python3.7需要的openssl的版本为1.0.2或者1.1.x,需要对openssl进行升级,并重新编译python3.7.0。yum 安装的openssl 版本都比较低。

升级openssl

# 1.下贼openssl
sudo wget https://www.openssl.org/source/openssl-1.1.1a.tar.gz
sudo tar -zxvf openssl-1.1.1a.tar.gz
cd openssl-1.1.1a
# 2.编译安装
sudo ./config --prefix=/usr/local/openssl no-zlib #不需要zlib
sudo make
sudo make install
# 3.备份原配置(如果报错:没有这个/usr/include/openssl文件,那么先执行第4部,然后再执行第3步)
sudo mv /usr/bin/openssl /usr/bin/openssl.bak
sudo mv /usr/include/openssl /usr/include/openssl.bak
# 4.新版配置
sudo ln -s /usr/local/openssl/include/openssl /usr/include/openssl
sudo ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
sudo ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
# 5.修改系统配置
## 写入openssl库文件的搜索路径(注意,需要修改/etc/ld.so.conf文件的权限,不然无法追加进去)
sudo echo "/usr/local/openssl/lib" >>  /etc/ld.so.conf
## 使修改后的/etc/ld.so.conf生效 
sudo ldconfig -v
# 6.查看openssl版本
openssl version

重新安装python

sudo ./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
sudo make 
sudo make install

安装需要的库

sudo pip3 install pandas
sudo pip3 install numpy
sudo pip3 install matplotlib
sudo pip3 install scikit-learn

回退键和上下左右四个箭头的问题

安装好以后,我们发现当使用backspace键删除字符的时候会显示:

>>> ^H

而使用上下左右箭头的时候会显示:

>>> ^[[A^[[B^[[D^[[C

这根本原因就是readline库的问题

如何解决
sudo yum install readline-devel.*
sudo ./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
sudo make 
sudo make install

后记

由于环境配置不同,每个人遇到的问题都不同,只有对症下药才能解决问题

Logo

更多推荐