我带了一些在Django版本1.8.4中制作的示例代码,就像Python 2.7一样,当转移到3个python时,它们全部飞走了,并产生了这样的错误,如何解决?

\lib\site-packages\config.py", line 91, in <module>
        from types import StringType, UnicodeType
    ImportError: cannot import name 'StringType'

一段使用stringtype(config.py)的代码(在站点程序包中)
def writeValue(self, value, stream, indent):
        if isinstance(self, Mapping):
            indstr = ' '
        else:
            indstr = indent * '  '
        if isinstance(value, Reference) or isinstance(value, Expression):
            stream.write('%s%r%s' % (indstr, value, NEWLINE))
        else:
            if (type(value) is StringType): # and not isWord(value):
                value = repr(value)
            stream.write('%s%s%s' % (indstr, value, NEWLINE))

最佳答案

Python3中没有StringType

尝试以下方法:

 from types import *
 x=type('String')

要检查对象的类型,请使用:
type(x) is str

在给定的情况下给出:True

另外,按照iFlo的问题注释中的建议更改您的代码:https://docs.python.org/3/howto/pyporting.html

关于python - ImportError : cannot import name 'StringType' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45508380/

10-12 17:51
查看更多