首页 > 解决方案 > 如何修复 'AttributeError: cffi library '_constant_time' has no function, constant or global variable named '__spec__'' 在 django 中的错误”

问题描述

我想导入请求库(在 views.py 中)以从其他网站请求一些信息。但是有一个像标题这样的错误

在 ivews.py

from django.http import HttpResponse

from django.http import JsonResponse

from django.core import serializers

import requests

import json

AttributeError:cffi 库“_constant_time”没有名为“ spec ”的函数、常量或全局变量

上述异常是以下异常的直接原因:

Traceback(最近一次调用最后一次):文件“C:/Users/49646/Desktop/auto_order/manage.py”,第 21 行,在
.....如果不是 isinstance(module, ModuleType) 或 getattr(module, ' spec ', None) is None: SystemError: 返回一个带有错误集的结果

标签: python-3.xpython-requestsdjango-2.2

解决方案


正如@John-Doe 在评论中指出的那样,升级cffi到最新版本(现在是 1.14.5)修复了这个完全相同的错误。我已经在 MacOS X 10.14 上使用 Django 3.x 亲自证实了这一点。


推荐阅读