导入错误与python-mysql连接器1.16,django 1.6和python 3.2.3 [英] import error with python-mysql-connector 1.16, django 1.6, and python 3.2.3

查看:218
本文介绍了导入错误与python-mysql连接器1.16,django 1.6和python 3.2.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django 1.6中运行django-rest-framework,在Python 3.2.3的virtualenv中,新安装的python-mysql-connector 1.1.6通过源代码在Ubuntu主机上。



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

 异常值:

无法导入名称zip_longest

异常位置:/usr/local/dev/python3.2.3/lib/python3.2/site-packages/mysql/连接器/ django / compiler.py在< module>,第6行

  from django.utils.six.moves import zip_longest 

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

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

这是从我的virtualenv的点数列表输出,我计划运行Django来验证我拥有所有权利bit安装:

 (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文件,并发现引用的是move.zip_longest模块。



首先是这个参考:

  moves = sys.modules [__ name__ +.moves] = _MovedItems(__ name__ +。移动)

这意味着它正在调用_MovedItems类,这里是引用我的

  class _MovedItems(_LazyModule):
懒惰加载移动的对象

_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)
如果PY3 :
如果new_mod为None:
new_mod = name
self.mod = new_mod
如果new_attr为None:
如果old_attr为None:
new_attr = name
else:
new_attr = old_attr
self.attr = new_attr

然后它继承自_LazyDescr类,但这是一个很短的小对象。我不知道发生错误的地方,如果您将传入的元组映射到MovedAttribute构造函数中,它将旧版本正确映射到新版本。我不知道为什么它是失败的,但是如果你删除在compile.py文件中的import语句,并直接调用itertools zip_longest,这一切都有效。



这是什么好像。如果您使用的是Python 3,请编辑/lib/python3.2/site-packages/mysql/connector/django/compiler.py文件,并从中更改第6行:



来自django.utils.six.moves import zip_longest作为six_zip_longest



  

到:

  from itertools import zip_longest 


I'm running django-rest-framework on Django 1.6 in a virtualenv of Python 3.2.3 with a newly installed python-mysql-connector 1.1.6 via the source on a Ubuntu host.

Everything works fine when I run syncdb and start it up, but when I got to any url that isn't the root I get this import error from 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

Here's the line in question:

from django.utils.six.moves import zip_longest

If I activate my virtualenv python binary, I can successfully run that import statement. So what gives?

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

Here's my pip list output from my virtualenv where I plan on running Django to verify I have all the right bits installed:

(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)

解决方案

After studying the code it looks like there is some kind of error when referencing that import. I looked into the /lib/python3.2/site-packages/django/utils/six.py file and found where the reference was to the moves.zip_longest module.

First was this reference:

moves = sys.modules[__name__ + ".moves"] = _MovedItems(__name__ + ".moves")

Which means it was calling the _MovedItems class, and here it is with the reference to my module that was breaking.

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

Which then inherits from the _LazyDescr class, but that is a short little object. I don't know where things went wrong, if you map the tuple being passed into the MovedAttribute constructor it maps correctly the old version to the new one. I'm not sure why it's failing but if you remove the import statement in the compiler.py file and simply call itertools zip_longest directly, it all works.

Here's what that looks like. If you are using Python 3, edit the /lib/python3.2/site-packages/mysql/connector/django/compiler.py file, and change line 6 from this:

from django.utils.six.moves import zip_longest as six_zip_longest

to this:

from itertools import zip_longest

这篇关于导入错误与python-mysql连接器1.16,django 1.6和python 3.2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆