Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'StringType'

Tags:

python

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))
like image 533
InvictusManeoBart Avatar asked Aug 04 '17 13:08

InvictusManeoBart


1 Answers

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

like image 133
ABcDexter Avatar answered Oct 22 '22 17:10

ABcDexter