1. 程式人生 > 其它 >AttributeError: ‘str‘ object has no attribute ‘decode‘

AttributeError: ‘str‘ object has no attribute ‘decode‘

技術標籤:pythonpythondecode

AttributeError: 'str' object has no attribute 'decode'

今天在安裝execjs的時候遇到錯誤

莫名其妙的環境問題,意思就是原本是個str但是卻呼叫了decode,

說明pip基礎程式碼有問題,直接點選跳轉,自動定位到compat.py的98行,

decoded_data = data.decode(encoding)

就是這句程式碼報錯,很明顯,data原本就是str,而此段嘗試decode就錯了,直接修改原始碼:

try:
    if type(data) is str:
        decoded_data = data
    else:
        decoded_data = data.decode(encoding)

except UnicodeDecodeError:

這樣邏輯就嚴謹了,如果data是str則直接使用,而不進行decode,

程式變得更加健壯,完美