linux(centos)安装python

下载pthon安装包
https://www.python.org/downloads/source/

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

创建安装目录

mkdir -p /usr/local/python3.8.3

解压

tar -zxvf Python-3.8.3.tgz

 编译安装 首先需要gcc环境和zlib库为了方向键等不出现乱码还需要 readline-devel 包

yum -y install gcc
yum -y install zlib*
yum install readline-devel
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel make
cd Python-3.8.3
./configure --prefix=/usr/local/python3.8.3 --enable-optimizations --enable-shared --with-ssl

关于./configure --prefix=/usr/local/python3.8.3 --enable-optimizations --enable-shared --with-ssl 命令说明

--prefix 指定安装的路径,不指定的话,安装过程中可能软件所需要的文件复制到其他不同目录,删除软件很不方便,复制软件也不方便.
--enable-optimizations 可以提高python10%-20%代码运行速度.
--enable-shared 是为了安装Vim的插件:YouCompleteMe ,不通过此参数编译的话,后面安装该插件会出错
--with-ssl 是为了安装pip需要用到ssl,后面报错会有提到.
#编译安装
make && make install
建立软链接
ln -s /usr/local/python3.8.3/bin/python3.8 /usr/bin/python
ln -s /usr/local/python3.8.3/bin/pip3.8 /usr/bin/pip
[root@fd07bda1fc63 bin]# python --version
Python 3.8.3

坑一

[root@198e6d7f2d48 Python-3.8.3]# python --version
python: error while loading shared libraries: libpython3.8.so.1.0: cannot open shared object file: No such file or directory

原因是因为python运行时没有加载到libpython3.6m.so.1.0 这个库文件,将其复制到响应目录OK,输入下列命令即可
解决办法:

 cp /usr/local/python3.8.3/lib/libpython3.8.so.1.0 /usr/lib64/ 

坑二

WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting Scrapy
  WARNING: 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/scrapy/
  WARNING: 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/scrapy/
  WARNING: 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/scrapy/
  WARNING: 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/scrapy/
  WARNING: 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/scrapy/
  Could not fetch URL https://pypi.org/simple/scrapy/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/scrapy/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
  ERROR: Could not find a version that satisfies the requirement Scrapy (from versions: none)
ERROR: No matching distribution found for Scrapy
WARNING: 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 –prefix=/usr/local/python3.8.3 –enable-optimizations –enable-shared –with-ssl


–with-ssl 是为了安装pip需要用到ssl,后面报错会有提到.

pip install --upgrade pip  -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
pip install scrapy    -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn