python - python-mysql-connector 1.16、django 1.6 和 python 3.2.3 导入错误

标签 python mysql django python-3.x

我正在 Python 3.2.3 的 virtualenv 中的 Django 1.6 上运行 django-rest-framework,并通过 the source 新安装了 python-mysql-connector 1.1.6|在 Ubuntu 主机上。

当我运行 syncdb 并启动它时一切正常,但是当我到达任何不是根的 url 时,我从 python-mysql-connector 得到这个导入错误:

Exception Value:    

cannot import name zip_longest

Exception Location:     /usr/local/dev/python3.2.3/lib/python3.2/site-packages/mysql/connector/django/compiler.py in <module>, line 6

这是有问题的行:

from django.utils.six.moves import zip_longest

如果我激活我的 virtualenv python 二进制文件,我可以成功运行该导入语句。那么是什么给了?

>>>>from django.utils.six.moves import zip_longest
>>>>

这是我的 virtualenv 的 pip 列表输出,我计划在其中运行 Django 以验证我是否安装了所有正确的位:

(python3.2.3)thegooch@yrmomsapt:~/$ pip list
Django (1.6.2)
djangorestframework (2.3.13)
Markdown (2.4)
mysql-connector-python (1.1.6)
pip (1.5.4)
setuptools (2.2)
wsgiref (0.1.2)

最佳答案

在研究了代码之后,在引用该导入时似乎存在某种错误。我查看了/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

为此:

from itertools import zip_longest

关于python - python-mysql-connector 1.16、django 1.6 和 python 3.2.3 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22546836/

相关文章:

python - 这种 numpy 选择行为背后发生了什么?

python - 如何使用OpenCV裁剪圆形图像?

python - Python有方便解析二进制格式的工具吗?

php - 如何以降序重新排序整个sql语句?

django - Python/Django django-registration 添加一个额外的字段

Django:密码重置电子邮件主题行包含“example.com”

html - 在使用html表单上传的Django中读取Excel文件

javascript - 如何使用 PyV8 从 python 读取 javascript 值

mysql - SQL : Error at Union All in sql query

mysql - 如何在数据库中存储特定条目的访问者?