InvictusManeoBart
InvictusManeoBart

Reputation: 383

ImportError: cannot import name 'StringType'

I took some sample code that was made in the django version 1.8.4, and like Python 2.7 when transferred to 3 python all flown away and produced such an error, how to fix it?

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

one piece of code where using stringtype (config.py)(in site-packages)

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

Upvotes: 10

Views: 15519

Answers (2)

ABcDexter
ABcDexter

Reputation: 2941

There is no StringType in Python3.

Try this instead:

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

To check the type of an object use:

type(x) is str

which gives : True in the given case.


Also, alter you code as suggested in the question comments by iFlo : https://docs.python.org/3/howto/pyporting.html

Upvotes: 14

Carlos Afonso
Carlos Afonso

Reputation: 1957

StringType is obsolete in python 3, UnicodeType is not available anymore as it's the built-in str from python3 right now.

Upvotes: 0

Related Questions