[{"data":1,"prerenderedAt":1676},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes":3},{"id":4,"title":5,"body":6,"description":1669,"extension":1670,"meta":1671,"navigation":410,"path":1672,"seo":1673,"stem":1674,"__hash__":1675},"content\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes.md","RuntimeError in Python: Causes and Fixes",{"type":7,"value":8,"toc":1647},"minimark",[9,13,26,38,44,49,135,138,151,155,165,168,197,200,213,217,222,225,245,251,257,261,264,288,291,372,375,384,387,389,442,445,449,454,457,569,572,586,589,600,603,725,727,762,766,769,841,848,855,906,909,913,916,999,1005,1008,1084,1086,1095,1099,1105,1108,1125,1128,1143,1147,1159,1162,1186,1189,1211,1218,1222,1225,1246,1249,1277,1280,1323,1326,1431,1434,1441,1445,1451,1453,1523,1526,1537,1540,1542,1560,1567,1571,1575,1584,1588,1591,1595,1598,1602,1609,1613,1643],[10,11,5],"h1",{"id":12},"runtimeerror-in-python-causes-and-fixes",[14,15,16,20,21,25],"p",{},[17,18,19],"code",{},"RuntimeError"," is a general Python exception that happens ",[22,23,24],"strong",{},"while your program is running",".",[14,27,28,29,34,35,25],{},"For beginners, this error can feel vague because the name does not tell you exactly what went wrong. The important clue is usually the ",[22,30,31,32],{},"message after ",[17,33,19],{}," and the ",[22,36,37],{},"traceback line that points to the problem",[14,39,40,41,43],{},"This guide explains what ",[17,42,19],{}," means, why it happens, and how to debug it using simple real examples.",[45,46,48],"h2",{"id":47},"quick-fix","Quick fix",[50,51,56],"pre",{"className":52,"code":53,"language":54,"meta":55,"style":55},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","try:\n    # code that may fail at runtime\n    do_something()\nexcept RuntimeError as e:\n    print('RuntimeError:', e)\n","python","",[17,57,58,71,78,88,107],{"__ignoreMap":55},[59,60,63,67],"span",{"class":61,"line":62},"line",1,[59,64,66],{"class":65},"sVHd0","try",[59,68,70],{"class":69},"sP7_E",":\n",[59,72,74],{"class":61,"line":73},2,[59,75,77],{"class":76},"sutJx","    # code that may fail at runtime\n",[59,79,81,85],{"class":61,"line":80},3,[59,82,84],{"class":83},"slqww","    do_something",[59,86,87],{"class":69},"()\n",[59,89,91,94,98,101,105],{"class":61,"line":90},4,[59,92,93],{"class":65},"except",[59,95,97],{"class":96},"sZMiF"," RuntimeError",[59,99,100],{"class":65}," as",[59,102,104],{"class":103},"su5hD"," e",[59,106,70],{"class":69},[59,108,110,114,117,121,125,127,130,132],{"class":61,"line":109},5,[59,111,113],{"class":112},"sptTA","    print",[59,115,116],{"class":69},"(",[59,118,120],{"class":119},"sjJ54","'",[59,122,124],{"class":123},"s_sjI","RuntimeError:",[59,126,120],{"class":119},[59,128,129],{"class":69},",",[59,131,104],{"class":83},[59,133,134],{"class":69},")\n",[14,136,137],{},"Catching the error can help you inspect the message, but the real fix is to find the operation that fails during program execution.",[14,139,140,141,146,147,25],{},"If you are new to exceptions, see ",[142,143,145],"a",{"href":144},"\u002Flearn\u002Fpython-errors-and-exceptions-explained\u002F","Python errors and exceptions explained"," and ",[142,148,150],{"href":149},"\u002Fhow-to\u002Fhow-to-use-try-except-blocks-in-python\u002F","how to use try-except blocks in Python",[45,152,154],{"id":153},"what-runtimeerror-means","What RuntimeError means",[14,156,157,158,160,161,164],{},"A ",[17,159,19],{}," means a problem happened ",[22,162,163],{},"during execution",", not before the program started.",[14,166,167],{},"Key points:",[169,170,171,175,178,191],"ul",{},[172,173,174],"li",{},"It happens while the program is running",[172,176,177],{},"Python or a library detected a runtime problem",[172,179,180,181,187,188],{},"It is more general than errors like ",[142,182,184],{"href":183},"\u002Ferrors\u002Fvalueerror-in-python-causes-and-fixes\u002F",[17,185,186],{},"ValueError"," or ",[17,189,190],{},"TypeError",[172,192,193,194,196],{},"The message after ",[17,195,19],{}," is often the most useful clue",[14,198,199],{},"For example:",[169,201,202,208],{},[172,203,204,207],{},[17,205,206],{},"SyntaxError"," means the code is written incorrectly",[172,209,210,212],{},[17,211,19],{}," means the code started running, but failed during execution",[45,214,216],{"id":215},"why-this-error-happens","Why this error happens",[14,218,219,221],{},[17,220,19],{}," often appears when the problem does not fit a more specific built-in exception.",[14,223,224],{},"Common reasons include:",[169,226,227,233,236,239,242],{},[172,228,229,230,232],{},"A library raises ",[17,231,19],{}," for a general runtime problem",[172,234,235],{},"Your code changes something while Python is still using it",[172,237,238],{},"A generator or iterator stops in an unexpected way",[172,240,241],{},"Recursion or environment state causes execution to fail",[172,243,244],{},"A framework reports that something happened in the wrong state",[14,246,247,248,250],{},"In short, ",[17,249,19],{}," usually means:",[252,253,254],"blockquote",{},[14,255,256],{},"\"The program reached a bad state while running.\"",[45,258,260],{"id":259},"simple-example-that-raises-runtimeerror","Simple example that raises RuntimeError",[14,262,263],{},"Programmers and libraries can raise this error directly.",[50,265,267],{"className":52,"code":266,"language":54,"meta":55,"style":55},"raise RuntimeError(\"something went wrong\")\n",[17,268,269],{"__ignoreMap":55},[59,270,271,274,276,278,281,284,286],{"class":61,"line":62},[59,272,273],{"class":65},"raise",[59,275,97],{"class":96},[59,277,116],{"class":69},[59,279,280],{"class":119},"\"",[59,282,283],{"class":123},"something went wrong",[59,285,280],{"class":119},[59,287,134],{"class":69},[14,289,290],{},"Output:",[50,292,294],{"className":52,"code":293,"language":54,"meta":55,"style":55},"Traceback (most recent call last):\n  File \"example.py\", line 1, in \u003Cmodule>\n    raise RuntimeError(\"something went wrong\")\nRuntimeError: something went wrong\n",[17,295,296,309,345,362],{"__ignoreMap":55},[59,297,298,301,303,306],{"class":61,"line":62},[59,299,300],{"class":83},"Traceback ",[59,302,116],{"class":69},[59,304,305],{"class":83},"most recent call last",[59,307,308],{"class":69},"):\n",[59,310,311,314,316,319,321,323,326,330,332,336,339,342],{"class":61,"line":73},[59,312,313],{"class":103},"  File ",[59,315,280],{"class":119},[59,317,318],{"class":123},"example.py",[59,320,280],{"class":119},[59,322,129],{"class":69},[59,324,325],{"class":103}," line ",[59,327,329],{"class":328},"srdBf","1",[59,331,129],{"class":69},[59,333,335],{"class":334},"smGrS"," in",[59,337,338],{"class":334}," \u003C",[59,340,341],{"class":103},"module",[59,343,344],{"class":334},">\n",[59,346,347,350,352,354,356,358,360],{"class":61,"line":80},[59,348,349],{"class":65},"    raise",[59,351,97],{"class":96},[59,353,116],{"class":69},[59,355,280],{"class":119},[59,357,283],{"class":123},[59,359,280],{"class":119},[59,361,134],{"class":69},[59,363,364,366,369],{"class":61,"line":90},[59,365,19],{"class":96},[59,367,368],{"class":69},":",[59,370,371],{"class":103}," something went wrong\n",[14,373,374],{},"The important part is the message:",[50,376,378],{"className":52,"code":377,"language":54,"meta":55,"style":55},"something went wrong\n",[17,379,380],{"__ignoreMap":55},[59,381,382],{"class":61,"line":62},[59,383,377],{"class":103},[14,385,386],{},"In real programs, the message should be more specific.",[14,388,199],{},[50,390,392],{"className":52,"code":391,"language":54,"meta":55,"style":55},"user_logged_in = False\n\nif not user_logged_in:\n    raise RuntimeError(\"Cannot load dashboard before login\")\n",[17,393,394,406,412,425],{"__ignoreMap":55},[59,395,396,399,402],{"class":61,"line":62},[59,397,398],{"class":103},"user_logged_in ",[59,400,401],{"class":334},"=",[59,403,405],{"class":404},"s39Yj"," False\n",[59,407,408],{"class":61,"line":73},[59,409,411],{"emptyLinePlaceholder":410},true,"\n",[59,413,414,417,420,423],{"class":61,"line":80},[59,415,416],{"class":65},"if",[59,418,419],{"class":334}," not",[59,421,422],{"class":103}," user_logged_in",[59,424,70],{"class":69},[59,426,427,429,431,433,435,438,440],{"class":61,"line":90},[59,428,349],{"class":65},[59,430,97],{"class":96},[59,432,116],{"class":69},[59,434,280],{"class":119},[59,436,437],{"class":123},"Cannot load dashboard before login",[59,439,280],{"class":119},[59,441,134],{"class":69},[14,443,444],{},"This is much more helpful because it tells you what state was invalid.",[45,446,448],{"id":447},"common-real-world-cases","Common real-world cases",[450,451,453],"h3",{"id":452},"dictionary-changed-size-during-iteration","Dictionary changed size during iteration",[14,455,456],{},"A common runtime problem is changing a dictionary while looping through it.",[50,458,460],{"className":52,"code":459,"language":54,"meta":55,"style":55},"data = {\"a\": 1, \"b\": 2, \"c\": 3}\n\nfor key in data:\n    if key == \"b\":\n        del data[key]\n",[17,461,462,515,519,535,553],{"__ignoreMap":55},[59,463,464,467,469,472,474,476,478,480,483,485,488,491,493,495,498,500,502,505,507,509,512],{"class":61,"line":62},[59,465,466],{"class":103},"data ",[59,468,401],{"class":334},[59,470,471],{"class":69}," {",[59,473,280],{"class":119},[59,475,142],{"class":123},[59,477,280],{"class":119},[59,479,368],{"class":69},[59,481,482],{"class":328}," 1",[59,484,129],{"class":69},[59,486,487],{"class":119}," \"",[59,489,490],{"class":123},"b",[59,492,280],{"class":119},[59,494,368],{"class":69},[59,496,497],{"class":328}," 2",[59,499,129],{"class":69},[59,501,487],{"class":119},[59,503,504],{"class":123},"c",[59,506,280],{"class":119},[59,508,368],{"class":69},[59,510,511],{"class":328}," 3",[59,513,514],{"class":69},"}\n",[59,516,517],{"class":61,"line":73},[59,518,411],{"emptyLinePlaceholder":410},[59,520,521,524,527,530,533],{"class":61,"line":80},[59,522,523],{"class":65},"for",[59,525,526],{"class":103}," key ",[59,528,529],{"class":65},"in",[59,531,532],{"class":103}," data",[59,534,70],{"class":69},[59,536,537,540,542,545,547,549,551],{"class":61,"line":90},[59,538,539],{"class":65},"    if",[59,541,526],{"class":103},[59,543,544],{"class":334},"==",[59,546,487],{"class":119},[59,548,490],{"class":123},[59,550,280],{"class":119},[59,552,70],{"class":69},[59,554,555,558,560,563,566],{"class":61,"line":109},[59,556,557],{"class":65},"        del",[59,559,532],{"class":103},[59,561,562],{"class":69},"[",[59,564,565],{"class":103},"key",[59,567,568],{"class":69},"]\n",[14,570,571],{},"This raises:",[50,573,575],{"className":52,"code":574,"language":54,"meta":55,"style":55},"RuntimeError: dictionary changed size during iteration\n",[17,576,577],{"__ignoreMap":55},[59,578,579,581,583],{"class":61,"line":62},[59,580,19],{"class":96},[59,582,368],{"class":69},[59,584,585],{"class":103}," dictionary changed size during iteration\n",[14,587,588],{},"Why it happens:",[169,590,591,594,597],{},[172,592,593],{},"Python is iterating over the dictionary",[172,595,596],{},"At the same time, the dictionary is being changed",[172,598,599],{},"That makes the iteration unsafe",[14,601,602],{},"One fix is to loop over a copy of the keys:",[50,604,606],{"className":52,"code":605,"language":54,"meta":55,"style":55},"data = {\"a\": 1, \"b\": 2, \"c\": 3}\n\nfor key in list(data.keys()):\n    if key == \"b\":\n        del data[key]\n\nprint(data)\n",[17,607,608,652,656,680,696,708,713],{"__ignoreMap":55},[59,609,610,612,614,616,618,620,622,624,626,628,630,632,634,636,638,640,642,644,646,648,650],{"class":61,"line":62},[59,611,466],{"class":103},[59,613,401],{"class":334},[59,615,471],{"class":69},[59,617,280],{"class":119},[59,619,142],{"class":123},[59,621,280],{"class":119},[59,623,368],{"class":69},[59,625,482],{"class":328},[59,627,129],{"class":69},[59,629,487],{"class":119},[59,631,490],{"class":123},[59,633,280],{"class":119},[59,635,368],{"class":69},[59,637,497],{"class":328},[59,639,129],{"class":69},[59,641,487],{"class":119},[59,643,504],{"class":123},[59,645,280],{"class":119},[59,647,368],{"class":69},[59,649,511],{"class":328},[59,651,514],{"class":69},[59,653,654],{"class":61,"line":73},[59,655,411],{"emptyLinePlaceholder":410},[59,657,658,660,662,664,667,669,672,674,677],{"class":61,"line":80},[59,659,523],{"class":65},[59,661,526],{"class":103},[59,663,529],{"class":65},[59,665,666],{"class":96}," list",[59,668,116],{"class":69},[59,670,671],{"class":83},"data",[59,673,25],{"class":69},[59,675,676],{"class":83},"keys",[59,678,679],{"class":69},"()):\n",[59,681,682,684,686,688,690,692,694],{"class":61,"line":90},[59,683,539],{"class":65},[59,685,526],{"class":103},[59,687,544],{"class":334},[59,689,487],{"class":119},[59,691,490],{"class":123},[59,693,280],{"class":119},[59,695,70],{"class":69},[59,697,698,700,702,704,706],{"class":61,"line":109},[59,699,557],{"class":65},[59,701,532],{"class":103},[59,703,562],{"class":69},[59,705,565],{"class":103},[59,707,568],{"class":69},[59,709,711],{"class":61,"line":710},6,[59,712,411],{"emptyLinePlaceholder":410},[59,714,716,719,721,723],{"class":61,"line":715},7,[59,717,718],{"class":112},"print",[59,720,116],{"class":69},[59,722,671],{"class":83},[59,724,134],{"class":69},[14,726,290],{},[50,728,730],{"className":52,"code":729,"language":54,"meta":55,"style":55},"{'a': 1, 'c': 3}\n",[17,731,732],{"__ignoreMap":55},[59,733,734,737,739,741,743,745,747,749,752,754,756,758,760],{"class":61,"line":62},[59,735,736],{"class":69},"{",[59,738,120],{"class":119},[59,740,142],{"class":123},[59,742,120],{"class":119},[59,744,368],{"class":69},[59,746,482],{"class":328},[59,748,129],{"class":69},[59,750,751],{"class":119}," '",[59,753,504],{"class":123},[59,755,120],{"class":119},[59,757,368],{"class":69},[59,759,511],{"class":328},[59,761,514],{"class":69},[450,763,765],{"id":764},"generator-raised-stopiteration-in-the-wrong-place","Generator raised StopIteration in the wrong place",[14,767,768],{},"Generators can also lead to runtime problems.",[50,770,772],{"className":52,"code":771,"language":54,"meta":55,"style":55},"def broken_generator():\n    raise StopIteration(\"stop here\")\n    yield 1\n\nfor item in broken_generator():\n    print(item)\n",[17,773,774,787,805,813,817,830],{"__ignoreMap":55},[59,775,776,780,784],{"class":61,"line":62},[59,777,779],{"class":778},"sbsja","def",[59,781,783],{"class":782},"sGLFI"," broken_generator",[59,785,786],{"class":69},"():\n",[59,788,789,791,794,796,798,801,803],{"class":61,"line":73},[59,790,349],{"class":65},[59,792,793],{"class":96}," StopIteration",[59,795,116],{"class":69},[59,797,280],{"class":119},[59,799,800],{"class":123},"stop here",[59,802,280],{"class":119},[59,804,134],{"class":69},[59,806,807,810],{"class":61,"line":80},[59,808,809],{"class":65},"    yield",[59,811,812],{"class":328}," 1\n",[59,814,815],{"class":61,"line":90},[59,816,411],{"emptyLinePlaceholder":410},[59,818,819,821,824,826,828],{"class":61,"line":109},[59,820,523],{"class":65},[59,822,823],{"class":103}," item ",[59,825,529],{"class":65},[59,827,783],{"class":83},[59,829,786],{"class":69},[59,831,832,834,836,839],{"class":61,"line":710},[59,833,113],{"class":112},[59,835,116],{"class":69},[59,837,838],{"class":83},"item",[59,840,134],{"class":69},[14,842,843,844,847],{},"In modern Python, this becomes a runtime error because raising ",[17,845,846],{},"StopIteration"," directly inside a generator is not the correct pattern.",[14,849,850,851,854],{},"A better approach is to use ",[17,852,853],{},"return"," to stop the generator:",[50,856,858],{"className":52,"code":857,"language":54,"meta":55,"style":55},"def good_generator():\n    return\n    yield 1\n\nfor item in good_generator():\n    print(item)\n",[17,859,860,869,874,880,884,896],{"__ignoreMap":55},[59,861,862,864,867],{"class":61,"line":62},[59,863,779],{"class":778},[59,865,866],{"class":782}," good_generator",[59,868,786],{"class":69},[59,870,871],{"class":61,"line":73},[59,872,873],{"class":65},"    return\n",[59,875,876,878],{"class":61,"line":80},[59,877,809],{"class":65},[59,879,812],{"class":328},[59,881,882],{"class":61,"line":90},[59,883,411],{"emptyLinePlaceholder":410},[59,885,886,888,890,892,894],{"class":61,"line":109},[59,887,523],{"class":65},[59,889,823],{"class":103},[59,891,529],{"class":65},[59,893,866],{"class":83},[59,895,786],{"class":69},[59,897,898,900,902,904],{"class":61,"line":710},[59,899,113],{"class":112},[59,901,116],{"class":69},[59,903,838],{"class":83},[59,905,134],{"class":69},[14,907,908],{},"This ends the generator cleanly.",[450,910,912],{"id":911},"operation-called-in-the-wrong-program-state","Operation called in the wrong program state",[14,914,915],{},"Sometimes your own code should only run when something has already happened.",[50,917,919],{"className":52,"code":918,"language":54,"meta":55,"style":55},"connected = False\n\ndef send_data():\n    if not connected:\n        raise RuntimeError(\"Cannot send data before connecting\")\n    print(\"Data sent\")\n\nsend_data()\n",[17,920,921,930,934,943,954,972,987,991],{"__ignoreMap":55},[59,922,923,926,928],{"class":61,"line":62},[59,924,925],{"class":103},"connected ",[59,927,401],{"class":334},[59,929,405],{"class":404},[59,931,932],{"class":61,"line":73},[59,933,411],{"emptyLinePlaceholder":410},[59,935,936,938,941],{"class":61,"line":80},[59,937,779],{"class":778},[59,939,940],{"class":782}," send_data",[59,942,786],{"class":69},[59,944,945,947,949,952],{"class":61,"line":90},[59,946,539],{"class":65},[59,948,419],{"class":334},[59,950,951],{"class":103}," connected",[59,953,70],{"class":69},[59,955,956,959,961,963,965,968,970],{"class":61,"line":109},[59,957,958],{"class":65},"        raise",[59,960,97],{"class":96},[59,962,116],{"class":69},[59,964,280],{"class":119},[59,966,967],{"class":123},"Cannot send data before connecting",[59,969,280],{"class":119},[59,971,134],{"class":69},[59,973,974,976,978,980,983,985],{"class":61,"line":710},[59,975,113],{"class":112},[59,977,116],{"class":69},[59,979,280],{"class":119},[59,981,982],{"class":123},"Data sent",[59,984,280],{"class":119},[59,986,134],{"class":69},[59,988,989],{"class":61,"line":715},[59,990,411],{"emptyLinePlaceholder":410},[59,992,994,997],{"class":61,"line":993},8,[59,995,996],{"class":83},"send_data",[59,998,87],{"class":69},[14,1000,1001,1002,1004],{},"This raises a ",[17,1003,19],{}," because the program state is invalid.",[14,1006,1007],{},"Fix it by changing the state first:",[50,1009,1011],{"className":52,"code":1010,"language":54,"meta":55,"style":55},"connected = True\n\ndef send_data():\n    if not connected:\n        raise RuntimeError(\"Cannot send data before connecting\")\n    print(\"Data sent\")\n\nsend_data()\n",[17,1012,1013,1022,1026,1034,1044,1060,1074,1078],{"__ignoreMap":55},[59,1014,1015,1017,1019],{"class":61,"line":62},[59,1016,925],{"class":103},[59,1018,401],{"class":334},[59,1020,1021],{"class":404}," True\n",[59,1023,1024],{"class":61,"line":73},[59,1025,411],{"emptyLinePlaceholder":410},[59,1027,1028,1030,1032],{"class":61,"line":80},[59,1029,779],{"class":778},[59,1031,940],{"class":782},[59,1033,786],{"class":69},[59,1035,1036,1038,1040,1042],{"class":61,"line":90},[59,1037,539],{"class":65},[59,1039,419],{"class":334},[59,1041,951],{"class":103},[59,1043,70],{"class":69},[59,1045,1046,1048,1050,1052,1054,1056,1058],{"class":61,"line":109},[59,1047,958],{"class":65},[59,1049,97],{"class":96},[59,1051,116],{"class":69},[59,1053,280],{"class":119},[59,1055,967],{"class":123},[59,1057,280],{"class":119},[59,1059,134],{"class":69},[59,1061,1062,1064,1066,1068,1070,1072],{"class":61,"line":710},[59,1063,113],{"class":112},[59,1065,116],{"class":69},[59,1067,280],{"class":119},[59,1069,982],{"class":123},[59,1071,280],{"class":119},[59,1073,134],{"class":69},[59,1075,1076],{"class":61,"line":715},[59,1077,411],{"emptyLinePlaceholder":410},[59,1079,1080,1082],{"class":61,"line":993},[59,1081,996],{"class":83},[59,1083,87],{"class":69},[14,1085,290],{},[50,1087,1089],{"className":52,"code":1088,"language":54,"meta":55,"style":55},"Data sent\n",[17,1090,1091],{"__ignoreMap":55},[59,1092,1093],{"class":61,"line":62},[59,1094,1088],{"class":103},[450,1096,1098],{"id":1097},"framework-or-package-reports-a-runtime-problem","Framework or package reports a runtime problem",[14,1100,1101,1102,1104],{},"Many third-party libraries use ",[17,1103,19],{}," when something is technically wrong at runtime, but not a perfect match for another error type.",[14,1106,1107],{},"For example, a package may raise errors like:",[169,1109,1110,1115,1120],{},[172,1111,1112],{},[17,1113,1114],{},"RuntimeError: event loop is already running",[172,1116,1117],{},[17,1118,1119],{},"RuntimeError: cannot schedule new futures after shutdown",[172,1121,1122],{},[17,1123,1124],{},"RuntimeError: model is not initialized",[14,1126,1127],{},"In these cases:",[169,1129,1130,1133,1140],{},[172,1131,1132],{},"Read the full message carefully",[172,1134,1135,1136,1139],{},"Check the line in ",[22,1137,1138],{},"your code"," that called the library",[172,1141,1142],{},"Look for setup steps that were missed or called in the wrong order",[45,1144,1146],{"id":1145},"how-to-fix-runtimeerror","How to fix RuntimeError",[14,1148,1149,1150,1152,1153,34,1156,25],{},"When you see ",[17,1151,19],{},", do not focus only on the error name. Focus on the ",[22,1154,1155],{},"traceback",[22,1157,1158],{},"message",[14,1160,1161],{},"Use this process:",[1163,1164,1165,1168,1171,1174,1177,1180,1183],"ol",{},[172,1166,1167],{},"Read the full traceback",[172,1169,1170],{},"Look at the last line first",[172,1172,1173],{},"Find the exact file and line number in your code",[172,1175,1176],{},"Check what the program was doing at that line",[172,1178,1179],{},"Inspect the values of nearby variables",[172,1181,1182],{},"See whether you changed a collection during iteration",[172,1184,1185],{},"Check whether the code ran in the wrong state",[14,1187,1188],{},"A few common fixes:",[169,1190,1191,1194,1197,1200,1208],{},[172,1192,1193],{},"Avoid changing a dictionary or set while looping over it",[172,1195,1196],{},"Move state-changing code before or after the loop",[172,1198,1199],{},"Make sure setup happens before dependent operations",[172,1201,1202,1203,1205,1206],{},"Replace direct ",[17,1204,846],{}," in generators with ",[17,1207,853],{},[172,1209,1210],{},"Check whether a library expects a different calling order",[14,1212,1213,1214,25],{},"If you need to recover from errors, see ",[142,1215,1217],{"href":1216},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python\u002F","how to handle exceptions in Python",[45,1219,1221],{"id":1220},"debugging-checklist","Debugging checklist",[14,1223,1224],{},"Use this checklist when you are stuck:",[169,1226,1227,1234,1237,1240,1243],{},[172,1228,1229,1230,1233],{},"Read the ",[22,1231,1232],{},"last line"," of the traceback first",[172,1235,1236],{},"Then read upward to find your file and line number",[172,1238,1239],{},"Check variable values near the failing line",[172,1241,1242],{},"Reduce the code to a small reproducible example",[172,1244,1245],{},"Test one fix at a time",[14,1247,1248],{},"These commands and tools can help:",[50,1250,1254],{"className":1251,"code":1252,"language":1253,"meta":55,"style":55},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python your_script.py\npython -m pdb your_script.py\n","bash",[17,1255,1256,1264],{"__ignoreMap":55},[59,1257,1258,1261],{"class":61,"line":62},[59,1259,54],{"class":1260},"sbgvK",[59,1262,1263],{"class":123}," your_script.py\n",[59,1265,1266,1268,1272,1275],{"class":61,"line":73},[59,1267,54],{"class":1260},[59,1269,1271],{"class":1270},"stzsN"," -m",[59,1273,1274],{"class":123}," pdb",[59,1276,1263],{"class":123},[14,1278,1279],{},"Useful checks inside your code:",[50,1281,1283],{"className":52,"code":1282,"language":54,"meta":55,"style":55},"print(variable_name)\nprint(type(variable_name))\nhelp(RuntimeError)\n",[17,1284,1285,1296,1312],{"__ignoreMap":55},[59,1286,1287,1289,1291,1294],{"class":61,"line":62},[59,1288,718],{"class":112},[59,1290,116],{"class":69},[59,1292,1293],{"class":83},"variable_name",[59,1295,134],{"class":69},[59,1297,1298,1300,1302,1305,1307,1309],{"class":61,"line":73},[59,1299,718],{"class":112},[59,1301,116],{"class":69},[59,1303,1304],{"class":96},"type",[59,1306,116],{"class":69},[59,1308,1293],{"class":83},[59,1310,1311],{"class":69},"))\n",[59,1313,1314,1317,1319,1321],{"class":61,"line":80},[59,1315,1316],{"class":112},"help",[59,1318,116],{"class":69},[59,1320,19],{"class":96},[59,1322,134],{"class":69},[14,1324,1325],{},"Example:",[50,1327,1329],{"className":52,"code":1328,"language":54,"meta":55,"style":55},"data = {\"a\": 1, \"b\": 2}\n\nfor key in data:\n    print(\"Current key:\", key)\n    print(\"Dictionary before change:\", data)\n    del data[key]\n",[17,1330,1331,1363,1367,1379,1399,1418],{"__ignoreMap":55},[59,1332,1333,1335,1337,1339,1341,1343,1345,1347,1349,1351,1353,1355,1357,1359,1361],{"class":61,"line":62},[59,1334,466],{"class":103},[59,1336,401],{"class":334},[59,1338,471],{"class":69},[59,1340,280],{"class":119},[59,1342,142],{"class":123},[59,1344,280],{"class":119},[59,1346,368],{"class":69},[59,1348,482],{"class":328},[59,1350,129],{"class":69},[59,1352,487],{"class":119},[59,1354,490],{"class":123},[59,1356,280],{"class":119},[59,1358,368],{"class":69},[59,1360,497],{"class":328},[59,1362,514],{"class":69},[59,1364,1365],{"class":61,"line":73},[59,1366,411],{"emptyLinePlaceholder":410},[59,1368,1369,1371,1373,1375,1377],{"class":61,"line":80},[59,1370,523],{"class":65},[59,1372,526],{"class":103},[59,1374,529],{"class":65},[59,1376,532],{"class":103},[59,1378,70],{"class":69},[59,1380,1381,1383,1385,1387,1390,1392,1394,1397],{"class":61,"line":90},[59,1382,113],{"class":112},[59,1384,116],{"class":69},[59,1386,280],{"class":119},[59,1388,1389],{"class":123},"Current key:",[59,1391,280],{"class":119},[59,1393,129],{"class":69},[59,1395,1396],{"class":83}," key",[59,1398,134],{"class":69},[59,1400,1401,1403,1405,1407,1410,1412,1414,1416],{"class":61,"line":109},[59,1402,113],{"class":112},[59,1404,116],{"class":69},[59,1406,280],{"class":119},[59,1408,1409],{"class":123},"Dictionary before change:",[59,1411,280],{"class":119},[59,1413,129],{"class":69},[59,1415,532],{"class":83},[59,1417,134],{"class":69},[59,1419,1420,1423,1425,1427,1429],{"class":61,"line":710},[59,1421,1422],{"class":65},"    del",[59,1424,532],{"class":103},[59,1426,562],{"class":69},[59,1428,565],{"class":103},[59,1430,568],{"class":69},[14,1432,1433],{},"This kind of debug output helps you see exactly what happens before the crash.",[14,1435,1436,1437,25],{},"If your problem involves deep repeated function calls, it may also be related to ",[142,1438,1440],{"href":1439},"\u002Ferrors\u002Frecursionerror-in-python-causes-and-fixes\u002F","RecursionError in Python",[45,1442,1444],{"id":1443},"when-to-raise-runtimeerror-yourself","When to raise RuntimeError yourself",[14,1446,1447,1448,1450],{},"You can raise ",[17,1449,19],{}," in your own code when the program reaches an invalid runtime state.",[14,1452,1325],{},[50,1454,1456],{"className":52,"code":1455,"language":54,"meta":55,"style":55},"file_open = False\n\ndef write_report():\n    if not file_open:\n        raise RuntimeError(\"Cannot write report because the file is not open\")\n    print(\"Report written\")\n",[17,1457,1458,1467,1471,1480,1491,1508],{"__ignoreMap":55},[59,1459,1460,1463,1465],{"class":61,"line":62},[59,1461,1462],{"class":103},"file_open ",[59,1464,401],{"class":334},[59,1466,405],{"class":404},[59,1468,1469],{"class":61,"line":73},[59,1470,411],{"emptyLinePlaceholder":410},[59,1472,1473,1475,1478],{"class":61,"line":80},[59,1474,779],{"class":778},[59,1476,1477],{"class":782}," write_report",[59,1479,786],{"class":69},[59,1481,1482,1484,1486,1489],{"class":61,"line":90},[59,1483,539],{"class":65},[59,1485,419],{"class":334},[59,1487,1488],{"class":103}," file_open",[59,1490,70],{"class":69},[59,1492,1493,1495,1497,1499,1501,1504,1506],{"class":61,"line":109},[59,1494,958],{"class":65},[59,1496,97],{"class":96},[59,1498,116],{"class":69},[59,1500,280],{"class":119},[59,1502,1503],{"class":123},"Cannot write report because the file is not open",[59,1505,280],{"class":119},[59,1507,134],{"class":69},[59,1509,1510,1512,1514,1516,1519,1521],{"class":61,"line":710},[59,1511,113],{"class":112},[59,1513,116],{"class":69},[59,1515,280],{"class":119},[59,1517,1518],{"class":123},"Report written",[59,1520,280],{"class":119},[59,1522,134],{"class":69},[14,1524,1525],{},"This is reasonable when:",[169,1527,1528,1531,1534],{},[172,1529,1530],{},"The problem happens at runtime",[172,1532,1533],{},"The state is invalid",[172,1535,1536],{},"A more specific built-in exception does not fit better",[14,1538,1539],{},"Still, prefer a more specific exception when possible.",[14,1541,199],{},[169,1543,1544,1550,1555],{},[172,1545,1546,1547,1549],{},"Use ",[17,1548,186],{}," for a bad value",[172,1551,1546,1552,1554],{},[17,1553,190],{}," for the wrong type",[172,1556,1546,1557,1559],{},[17,1558,19],{}," for a bad runtime state",[14,1561,1562,1563,25],{},"If you are unsure about the difference between exception types, see ",[142,1564,1566],{"href":1565},"\u002Ferrors\u002Ftypeerror-vs-valueerror-in-python-explained\u002F","TypeError vs ValueError in Python explained",[45,1568,1570],{"id":1569},"faq","FAQ",[450,1572,1574],{"id":1573},"is-runtimeerror-the-same-as-syntaxerror","Is RuntimeError the same as SyntaxError?",[14,1576,1577,1578,1580,1581,1583],{},"No. ",[17,1579,206],{}," happens before the program runs. ",[17,1582,19],{}," happens while the program is running.",[450,1585,1587],{"id":1586},"how-do-i-fix-runtimeerror-quickly","How do I fix RuntimeError quickly?",[14,1589,1590],{},"Read the traceback, find the exact failing line, and use the error message to identify the invalid runtime condition.",[450,1592,1594],{"id":1593},"should-i-catch-runtimeerror-with-try-except","Should I catch RuntimeError with try-except?",[14,1596,1597],{},"You can catch it for debugging or recovery, but you should still fix the real cause.",[450,1599,1601],{"id":1600},"can-i-raise-runtimeerror-myself","Can I raise RuntimeError myself?",[14,1603,1604,1605,1608],{},"Yes. You can use ",[17,1606,1607],{},"raise RuntimeError(\"message\")"," when your program reaches an invalid runtime state.",[45,1610,1612],{"id":1611},"see-also","See also",[169,1614,1615,1619,1624,1629,1634,1639],{},[172,1616,1617],{},[142,1618,145],{"href":144},[172,1620,1621],{},[142,1622,1623],{"href":1216},"How to handle exceptions in Python",[172,1625,1626],{},[142,1627,1628],{"href":149},"How to use try-except blocks in Python",[172,1630,1631],{},[142,1632,1633],{"href":1439},"RecursionError in Python: causes and fixes",[172,1635,1636],{},[142,1637,1638],{"href":183},"ValueError in Python: causes and fixes",[172,1640,1641],{},[142,1642,1566],{"href":1565},[1644,1645,1646],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":55,"searchDepth":73,"depth":73,"links":1648},[1649,1650,1651,1652,1653,1659,1660,1661,1662,1668],{"id":47,"depth":73,"text":48},{"id":153,"depth":73,"text":154},{"id":215,"depth":73,"text":216},{"id":259,"depth":73,"text":260},{"id":447,"depth":73,"text":448,"children":1654},[1655,1656,1657,1658],{"id":452,"depth":80,"text":453},{"id":764,"depth":80,"text":765},{"id":911,"depth":80,"text":912},{"id":1097,"depth":80,"text":1098},{"id":1145,"depth":73,"text":1146},{"id":1220,"depth":73,"text":1221},{"id":1443,"depth":73,"text":1444},{"id":1569,"depth":73,"text":1570,"children":1663},[1664,1665,1666,1667],{"id":1573,"depth":80,"text":1574},{"id":1586,"depth":80,"text":1587},{"id":1593,"depth":80,"text":1594},{"id":1600,"depth":80,"text":1601},{"id":1611,"depth":73,"text":1612},"Master runtimeerror in python causes and fixes in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fruntimeerror-in-python-causes-and-fixes",{"title":5,"description":1669},"errors\u002Fruntimeerror-in-python-causes-and-fixes","A7VT_XdPmUIBfLSM0dlqfchOTlGyKppQtLGVCXCuWaI",1777585511603]