Python3 源码安装记录 和 pip3 报错

本文重点在解决报错,不是安装

1、Python3下载

wget http://npm.taobao.org/mirrors/python/3.8.0/Python-3.8.0.tgz

2、安装

tar -zxvf Python-3.8.0.tgz
cd Python-3.8.0/
./configure --prefix=/usr/
make clean
make && make altinstall 

这样安装出来默认会带个 pip3.8 程序,可以认为就是 pip3

做个软链接

ln -s /usr/bin/python3.8 /usr/bin/python3

3、使用Python时可能出现的报错

下面的报错解决后,均需要重新编译 Python !!不然无法生效

报错1

ModuleNotFoundError: No module named '_ctypes'

参考

python - Python3: ImportError: No module named '_ctypes' when using Value from module multiprocessing - Stack Overflow

缺 libffi-dev 库,Ubuntu 是安装下面这个,Centos 库名可能不叫下面这个

apt install libffi-dev

重新编译 Python

报错2

pip 安装模块报错

WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
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.tuna.tsinghua.edu.cn/simple/requests/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', 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

首先这个明显跟 ssl 有关,你可以选择不带 https 的pip 源

pip源_淘小欣的博客-CSDN博客_pip源

源很多有些不带 https,是 http 的,使用不带 s 的源的话,用起来可能报错

WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.
ERROR: Could not find a version that satisfies the requirement Pyexcel (from versions: none)

需要 pip install 的时候加上 --trusted-host 表示信任,就能正常使用了

pip3.8 install meson -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

如果你需要带 s 的,那就往下看

我们注意到报错的关键信息是没有安装 ssl 模块,导致不能使用 https,接下来解决问题也是围绕 ssl 展开

WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

参考

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available - Stack Overflow

Ubuntu下,是 libssl-dev 库的问题,Centos可能叫别的名字

Ubuntu 确认下面的库是否安装 (主要是 libssl-dev)

apt install libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev libtk8.6 libgdm-dev libdb4o-cil-dev libpcap-dev

重新编译 Python,一般情况下,这样就能解决了

如果安装之后,重新编译 Python 还不行,说明你的系统比较特殊,比如我的是凝思,那可能需要考虑手动编译 openssl 了,参考 make 编译时报错的一句话

Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().

configure 编译的时候,留意一下,最后几行 checking,是不是都是 yes

checking for openssl/ssl.h in /usr/local/openssl-1.1.1... yes
checking whether compiling and linking against OpenSSL works... yes
checking for X509_VERIFY_PARAM_set1_host in libssl... yes

回到正题,我重新编译了一个 openssl 1.1.1 在 /usr/local/openssl-1.1.1/ 目录下

编译命令需要改成 (加上 openssl 库的路径)

./configure --prefix=/usr/ --with-openssl=/usr/local/openssl-1.1.1
make clean
make && make altinstall 

结果 make 编译过程中还是报错

*** WARNING: renaming "_ssl" since importing it failed: libssl.so.1.1: cannot open shared object file: No such file or directory
*** WARNING: renaming "_hashlib" since importing it failed: libssl.so.1.1: cannot open shared object file: No such file or directory

再次查阅资料

Issue 34028: Python 3.7.0 wont compile with SSL Support 1.1.0 > alledged missing X509_VERIFY_PARAM_set1_host() support - Python tracker

ssl/hashlib: Add configure option to set or auto-detect rpath to OpenSSL libs · Issue #87632 · python/cpython · GitHub

说要修改编译运行时库的路径,比如我的在 /usr/local/openssl-1.1.1/lib/ ,我要先执行下面这个命令才能编译 Python3 (我怀疑是设计漏了)

export LD_RUN_PATH=/usr/local/openssl-1.1.1/lib/

最后是终于编译没有报错了

此外 

--with-ssl 在后面的 configure 编译选项是无效的,编译的时候会报错 (应该是新版的Python编译舍弃了)

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

关于 configure 的编译选项可以在后面加 -help 查看,里面还有一个安装 pip 的选项,默认应该是开启的,下面选项来自 -help

--with(out)-ensurepip=[=upgrade]
                          "install" or "upgrade" using bundled pip

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值