研究代码后,引用该导入时似乎存在某种错误。我查看了 /lib/python3.2/site-packages/django/utils/six.py 文件,找到了对 moves.zip_longest 模块的引用。
首先是这个参考:
moves = sys.modules[__name__ + ".moves"] = _MovedItems(__name__ + ".moves")
这意味着它正在调用 _MovedItems 类,而这里是对我的模块的引用,该模块被破坏了。
class _MovedItems(_LazyModule):
"""Lazy loading of moved objects"""
_moved_attributes = [
MovedAttribute("zip_longest", "itertools", "itertools", "izip_longest", "zip_longest")
class MovedAttribute(_LazyDescr):
def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
super(MovedAttribute, self).__init__(name)
if PY3:
if new_mod is None:
new_mod = name
self.mod = new_mod
if new_attr is None:
if old_attr is None:
new_attr = name
else:
new_attr = old_attr
self.attr = new_attr
然后它继承自 _LazyDescr 类,但那是一个简短的小对象。我不知道哪里出了问题,如果您将传递的元组映射到 MovedAttribute 构造函数中,它会将旧版本正确映射到新版本。我不确定它为什么失败,但如果您删除compiler.py 文件中的导入语句并直接调用itertools zip_longest,那么一切都会起作用。
看起来是这样的。如果您使用的是 Python 3,请编辑 /lib/python3.2/site-packages/mysql/connector/django/compiler.py 文件,并将第 6 行更改为:
from django.utils.six.moves import zip_longest as six_zip_longest
to this:
from itertools import zip_longest