python編程中遇到的錯誤類型,原因及解決方法


一、TypeError
1.   user_t = username + time      #將username和time拼接在一起
     TypeError: must be str, not float
原因:time獲取到的time.time()當前時間戳爲float類型,float不能直接跟字符串進行拼接
解決方法:將time轉換成字符串,即str(time)
2. TypeError: 'dict' object is not callable
The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.
原因:接口return了字典類型的數據
      res_msg = {'username':username,'login_info':user_value}
                print('res_msg:',res_msg)
            return res_msg
解決方法:使用json.dump()將字典轉成json串,return json.dumps(res_msg,ensure_ascii=False)

說明:接口返回類型只能是一個string或者元組

2.    stu_info = res.get(stu_info)
TypeError: unhashable type: 'dict'



二、NameError
1.NameError: name 'time' is not defined
原因:
1)沒有使用函數中的形參,函數中的形參是login_time

2)沒有引入time模塊

2.File "F:/lp_test/besttest/auto_test/homework/syz_automatic_code/day7/day7_homework/day7_hw_1_modify_excel.py", line 67, in add_col_isgratudate
    old_sheet = open_excel( file_path )  # 調用打開excel的函數
NameError: name 'file_path' is not defined

原因:

定義函數時的位置參數寫成file_name,如右所示: [  def add_col_isgratudate(file_name): ]但實際使用的是file_path

解決方法:把file_name改爲file_path

三、AttributeError

1.AttributeError: 'NoneType' object has no attribute 'encode'

原因:沒有判斷輸入的參數是否爲空,就進行了md5加密
      pwd = db_operater_tools.my_md5(flask.request.values.get('passwd').strip()) 
解決方法:

      先判斷密碼非空後再加密

四、語法錯誤
1. File "F:/lp_test/besttest/auto_test/homework/syz_automatic_code/day7/day7_homework/bin/start_sever.py", line 5
    host = '0.0.0.0',       ^
SyntaxError: invalid syntax
原因:run方法的run與()中間加了=號,如下所示:
day7_hw_modify_reg_interface.server.run =(
    host = '0.0.0.0',
    port = SERVER_PORT,
    debug = True
)
解決方法:把=號去掉,如果設置正確host,port,debug都是橘紅色

2.File "F:\lp_test\besttest\auto_test\homework\syz_automatic_code\day7\day7_homework\lib\db_operater_tools.py", line 14
    if r.keys(k1:k)[0].decode()==key_com:        ^
SyntaxError: invalid syntax
原因:if r.keys(k1:k)[0].decode()==key_com:  不能寫成k1:k

解決方法:提前把k1和k拼接成字符串即用key_com(接收k1 + ':' + k)替換k1:k

五、列表越界
1.    if r.keys(key_com)[0].decode() == key_com:   #表示用戶
IndexError: list index out of range
原因:用戶名還沒有插入的時候r.keys()返回的是空的list,所以獲取不到[0]的數值

解決方法:將if r.keys(key_com)[0].decode() == key_com: 改爲 if r.keys(key_com) 列表空表示沒有檢索到

六、UnboundLocalError

1.File"F:\lp_test\besttest\auto_test\homework\syz_automatic_code\day7\day7_homework\lib\day7_hw_modify_reg_interface.py", line 78, in login
    return json.dumps(res_msg,ensure_ascii=False,indent=1)   #縮進爲1
UnboundLocalError: local variable 'res_msg' referenced before assignment

原因:判斷條件if redis_username == username and redis_passwd == pwd:沒有配對的else 輸出res_msg ={}
導致if條件爲假時,沒有了res_msg參數,所以導致以上錯誤

解決方法:增加配對的else: res_msg = {'',''}

七、ImportError

Traceback (most recent call last):
  File "start.py", line 1, in <module>
    from libs import interface

ImportError: cannot import name 'interface'

原因:因爲項目目錄不再環境變量中了

解決方法:手動加環境變量

os.path.dirname(os.path.dirname(__file__)) 使用此方法添加環境變量,運行錯誤,原因__file__獲取文件路徑分隔符錯誤
解決方法:轉成絕對路徑os.path.abspath(__file__)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章