python3.8 安装缺少ssl、_ctypes模块解决办法

问题

安装pyhton3.8安装默认不依赖ssl

运行Flask项目时报错:

Traceback (most recent call last):
  File "/usr/local/python3/bin/flask", line 8, in 
    sys.exit(main())
  File "/usr/local/python3/lib/python3.8/site-packages/flask/cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "/usr/local/python3/lib/python3.8/site-packages/flask/cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/usr/local/python3/lib/python3.8/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/usr/local/python3/lib/python3.8/site-packages/click/core.py", line 1686, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
  File "/usr/local/python3/lib/python3.8/site-packages/click/core.py", line 943, in make_context
    self.parse_args(ctx, args)
  File "/usr/local/python3/lib/python3.8/site-packages/click/core.py", line 1408, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "/usr/local/python3/lib/python3.8/site-packages/click/core.py", line 2400, in handle_parse_result
    value = self.process_value(ctx, value)
  File "/usr/local/python3/lib/python3.8/site-packages/click/core.py", line 2362, in process_value
    value = self.callback(ctx, self, value)
  File "/usr/local/python3/lib/python3.8/site-packages/flask/cli.py", line 742, in _validate_key
    is_context = isinstance(cert, ssl.SSLContext)
AttributeError: 'NoneType' object has no attribute 'SSLContext'

查看文件../site-packages/flask/cli.py,导入ssl包失败会捕获异常并将ssl赋值为None

自己import ssl也报错找不到ssl模块,确认是缺少ssl模块

python3.8 安装缺少ssl、_ctypes模块解决办法_第1张图片

解决

在编译安装python时候安装ssl模块
文件中 Modules/Setup去掉注释这五行

python3.8 安装缺少ssl、_ctypes模块解决办法_第2张图片

重新编译安装python

./configure --with-ssl --prefix=/usr/local/python3
make && make install

如果编译python过程中报错:

gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration  -I./Include/internal  -I. -I./Include    -DPy_BUILD_CORE_BUILTIN  -DUSE_SSL -I/usr/local/ssl/include -I/usr/local/ssl/include/openssl -c ./Modules/_ssl.c -o Modules/_ssl.o
./Modules/_ssl.c:57:10: fatal error: openssl/rsa.h: No such file or directory
 #include "openssl/rsa.h"
          ^~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:1932: Modules/_ssl.o] Error 1

python3.8 安装缺少ssl、_ctypes模块解决办法_第3张图片

则需要安装openssl:

下载地址

参见https://www.openssl.org/,包括以下版本:
https://www.openssl.org/source/openssl-3.1.0-alpha1.tar.gz
https://www.openssl.org/source/openssl-1.1.1s.tar.gz
https://www.openssl.org/source/openssl-3.0.7.tar.gz

编译安装

注:编译之前请先确保系统中安装了make以及gcc的软件包。,编译安装前确认/usr/include/openssl/为空

tar -zxf openssl-1.1.1s.tar.gz
cd openssl-1.1.1s/
./config -fPIC --prefix=/usr/include/openssl enable-shared
make
make install

其中:

  • l -fPIC: 位置无关代码
  • l --prefix=: 路径 一般选 /usr/include/openssl
  • l enbale-shared: 动态库

要在Debian、Ubuntu或者其他衍生版上安装OpenSSL:

sudo apt-get install libssl-dev

要在[Fedora][CentOS]或者RHEL上安装OpenSSL开发包:

sudo yum install openssl-devel

python3.8 安装缺少ssl、_ctypes模块解决办法_第4张图片

安装完后,尝试重新编译程序。

验证ssl模块安装成功:

python3.8 安装缺少ssl、_ctypes模块解决办法_第5张图片

使用pip安装bson的时候报错:”ModuleNotFoundError:No module named _ctypes“ 的解决方案

1、执行如下命令:

yum install libffi-devel 
2、从"./configure ..."重新安装

问题二:pip3 install时报错“pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.”

先安装openssl-dev,然后重新编译安装,只是在编译的过程中加入 --enable-optimizations

你可能感兴趣的:(ssl,网络协议,网络)