ImportError: Unable to import name 'StringType'

I took the sample code that was made in version 1.8.4 of django and like Python 2.7 when it is ported to 3 python everybody flew by and generated an error like this, 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 that uses 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))

      

+3


source to share


2 answers


Python3 doesn't StringType

.

Try this instead:

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

      

To check the usage type of an object:



type(x) is str

      

which gives: True

in this case.


Also change the code suggested in the comments on iFlo: https://docs.python.org/3/howto/pyporting.html

+5


source


StringType

deprecated in python 3, UnicodeType

no longer available as it is built in str

from python3 right now.



0


source







All Articles