2017-08-04 157 views
2

我拿了一些在django 1.8.4版本中創建的示例代碼,並且像Python 2.7一樣轉移到3 python時全都飛走了,併產生了這樣的錯誤,如何解決它?的代碼ImportError:無法導入名稱'StringType'

\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)) 
+0

你能提供你的代碼嗎?謝謝! –

+0

您是否看到他所在的位置? – InvictusManeoBart

+1

其實,你可能需要修改你的代碼(移植到python 3)。爲了幫助你:https://docs.python.org/3/howto/pyporting.html – iFlo

回答

4

有一個在Python3沒有StringType

嘗試此代替:

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

要檢查的對象使用的類型:

type(x) is str 

其給出:True在給定的情況下。


此外,改變你的代碼由IFLO的問題意見建議:https://docs.python.org/3/howto/pyporting.html

+0

是的,這是工作,但UnicodeType呢,如何解決? – InvictusManeoBart

+0

由於某些Python庫沒有「向後兼容性」,因此應該重構代碼。請閱讀提示的鏈接,它可以幫助你。我也試圖找到有關這個特定問題的東西:) – ABcDexter

+1

tnx !!!!!!!!!!!!!! – InvictusManeoBart

0

StringType是在Python 3過時,UnicodeType已不存在,因爲它是內置str從python3現在。

相關問題