python3 番外篇之Linux环境安装问题

问题一、Linux主机openSSL版本较老

[root@zabbix-server alertscripts]# python3 feishu.py
Traceback (most recent call last):
  File "feishu.py", line 3, in <module>
    import requests
  File "/usr/local/python3.8/lib/python3.8/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/usr/local/python3.8/lib/python3.8/site-packages/urllib3/__init__.py", line 41, in <module>
    raise ImportError(
ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips  26 Jan 2017'. See: https://github.com/urllib3/urllib3/issues/2168

解决办法:
  1、升级系统openssl
  2、重新编译安装python3

openssl升级脚本:

openssl下载官网:https://www.openssl.org/source/

#!/bin/bash
# 没有编译环境需要安装编译环境: yum groupinstall "Development Tools"
sslpkg=openssl-1.1.1v.tar.gz

if [ ! -f /usr/local/openssl ];then
   mkdir -pv /usr/local/openssl
fi

function downloadurl(){
    wget https://www.openssl.org/source/$sslpkg --no-check-certificate
}

function install(){
    tar xf $sslpkg
    ssldir=`echo $sslpkg | cut -f 1,2,3 -d"."`

    cd $ssldir
    ./config --prefix=/usr/local/openssl
    make
    make install
}

function changecfg(){
    # 备份原来的配置
    mv /usr/bin/openssl /usr/bin/openssl.bak
    mv /usr/include/openssl /usr/include/openssl.bak

    # 添加新的openssl软连接
    ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
    ln -s /usr/local/openssl/include/openssl /usr/include/openssl

    # 将新的库文件地址写入记录so库的配置文件
    echo "/usr/local/openssl/lib" >> /etc/ld.so.conf

    # 重载使其生效
    ldconfig -v
}

downloadurl
install

if [ $? -eq 0 ];then
changecfg
openssl version -a
fi

需要重新编译python3

# 如果之前编译过,需要执行make clean清除一下之前编译的包否则编译完成后会不生效。
./configure --prefix=/usr/local/python3.8 --with-openssl=/root/openssl-1.1.1v --with-openssl-rpath=auto
make
make install

问题二、SSLError

[root@zabbix-server write-bash]# pip3 install requests
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting requests
  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/requests/
  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/requests/
  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/requests/
  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/requests/
  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/requests/
  Could not fetch URL https://pypi.org/simple/requests/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/requests/ (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 requests (from versions: none)
ERROR: No matching distribution found for requests
WARNING: You are using pip version 19.2.3, however version 23.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

解决办法:

vim /root/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host = mirrors.aliyun.com

然后再执行 "python3 -m pip install --upgrade pip" 把pip升级一下就ok了。

问题三、ModuleNotFoundError: No module named '_ctypes'

解决办法:
  CentOS7: yum install libffi-devel
  Ubuntu: apt-get install libffi-dev
  再次重新编译安装python3

posted @ 2023-08-02 17:30  潇湘神剑  阅读(186)  评论(0编辑  收藏  举报