[{"data":1,"prerenderedAt":1399},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes":3},{"id":4,"title":5,"body":6,"description":1392,"extension":1393,"meta":1394,"navigation":892,"path":1395,"seo":1396,"stem":1397,"__hash__":1398},"content\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes.md","NameError in Python: Causes and Fixes",{"type":7,"value":8,"toc":1359},"minimark",[9,13,22,25,41,47,52,96,101,104,118,122,127,130,144,147,150,166,169,203,209,213,216,242,244,331,337,340,344,349,372,377,409,411,436,445,449,465,467,492,495,514,518,545,547,572,578,591,595,598,618,621,636,640,644,647,677,680,708,715,717,743,745,768,779,781,807,809,836,846,848,872,874,912,914,923,927,930,935,940,943,959,967,973,978,980,1046,1054,1061,1065,1071,1088,1091,1106,1109,1159,1162,1188,1197,1201,1207,1227,1230,1251,1259,1270,1274,1278,1287,1291,1299,1303,1308,1315,1319,1322,1326,1355],[10,11,5],"h1",{"id":12},"nameerror-in-python-causes-and-fixes",[14,15,16,17,21],"p",{},"A ",[18,19,20],"code",{},"NameError"," in Python means you used a name that Python cannot find.",[14,23,24],{},"That name might be:",[26,27,28,32,35,38],"ul",{},[29,30,31],"li",{},"A variable",[29,33,34],{},"A function",[29,36,37],{},"A class",[29,39,40],{},"An imported module",[14,42,43,44,46],{},"This error is common for beginners because Python only knows names that were already created, assigned, or imported. If the name does not exist in the current scope, Python raises ",[18,45,20],{},".",[48,49,51],"h2",{"id":50},"quick-fix","Quick fix",[53,54,59],"pre",{"className":55,"code":56,"language":57,"meta":58,"style":58},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","x = 10\nprint(x)\n","python","",[18,60,61,78],{"__ignoreMap":58},[62,63,66,70,74],"span",{"class":64,"line":65},"line",1,[62,67,69],{"class":68},"su5hD","x ",[62,71,73],{"class":72},"smGrS","=",[62,75,77],{"class":76},"srdBf"," 10\n",[62,79,81,85,89,93],{"class":64,"line":80},2,[62,82,84],{"class":83},"sptTA","print",[62,86,88],{"class":87},"sP7_E","(",[62,90,92],{"class":91},"slqww","x",[62,94,95],{"class":87},")\n",[14,97,16,98,100],{},[18,99,20],{}," happens when Python sees a variable, function, or module name that does not exist in the current scope.",[14,102,103],{},"Check these first:",[26,105,106,109,112,115],{},[29,107,108],{},"Is the name spelled correctly?",[29,110,111],{},"Was it defined before this line?",[29,113,114],{},"Did you forget to import it?",[29,116,117],{},"Did you mean to write a string with quotes?",[48,119,121],{"id":120},"what-this-error-means","What this error means",[14,123,124,126],{},[18,125,20],{}," means Python cannot find the name you used.",[14,128,129],{},"For example, Python will raise this error if you try to use:",[26,131,132,135,138,141],{},[29,133,134],{},"A variable that was never assigned",[29,136,137],{},"A function name that does not exist",[29,139,140],{},"A module that was not imported",[29,142,143],{},"Text without quotes, which Python reads as a name",[14,145,146],{},"Always read the exact name shown in the error message first. That tells you what Python could not find.",[14,148,149],{},"A typical error looks like this:",[53,151,153],{"className":55,"code":152,"language":57,"meta":58,"style":58},"print(total)\n",[18,154,155],{"__ignoreMap":58},[62,156,157,159,161,164],{"class":64,"line":65},[62,158,84],{"class":83},[62,160,88],{"class":87},[62,162,163],{"class":91},"total",[62,165,95],{"class":87},[14,167,168],{},"Output:",[53,170,172],{"className":55,"code":171,"language":57,"meta":58,"style":58},"NameError: name 'total' is not defined\n",[18,173,174],{"__ignoreMap":58},[62,175,176,179,182,185,189,192,194,197,200],{"class":64,"line":65},[62,177,20],{"class":178},"sZMiF",[62,180,181],{"class":87},":",[62,183,184],{"class":68}," name ",[62,186,188],{"class":187},"sjJ54","'",[62,190,163],{"class":191},"s_sjI",[62,193,188],{"class":187},[62,195,196],{"class":72}," is",[62,198,199],{"class":72}," not",[62,201,202],{"class":68}," defined\n",[14,204,205,206,208],{},"In this example, Python is telling you that ",[18,207,163],{}," does not exist at that point in the program.",[48,210,212],{"id":211},"simple-example-that-causes-nameerror","Simple example that causes NameError",[14,214,215],{},"Here is a very short example:",[53,217,219],{"className":55,"code":218,"language":57,"meta":58,"style":58},"print(score)\nscore = 100\n",[18,220,221,232],{"__ignoreMap":58},[62,222,223,225,227,230],{"class":64,"line":65},[62,224,84],{"class":83},[62,226,88],{"class":87},[62,228,229],{"class":91},"score",[62,231,95],{"class":87},[62,233,234,237,239],{"class":64,"line":80},[62,235,236],{"class":68},"score ",[62,238,73],{"class":72},[62,240,241],{"class":76}," 100\n",[14,243,168],{},[53,245,247],{"className":55,"code":246,"language":57,"meta":58,"style":58},"Traceback (most recent call last):\n  File \"example.py\", line 1, in \u003Cmodule>\n    print(score)\nNameError: name 'score' is not defined\n",[18,248,249,262,298,310],{"__ignoreMap":58},[62,250,251,254,256,259],{"class":64,"line":65},[62,252,253],{"class":91},"Traceback ",[62,255,88],{"class":87},[62,257,258],{"class":91},"most recent call last",[62,260,261],{"class":87},"):\n",[62,263,264,267,270,273,275,278,281,284,286,289,292,295],{"class":64,"line":80},[62,265,266],{"class":68},"  File ",[62,268,269],{"class":187},"\"",[62,271,272],{"class":191},"example.py",[62,274,269],{"class":187},[62,276,277],{"class":87},",",[62,279,280],{"class":68}," line ",[62,282,283],{"class":76},"1",[62,285,277],{"class":87},[62,287,288],{"class":72}," in",[62,290,291],{"class":72}," \u003C",[62,293,294],{"class":68},"module",[62,296,297],{"class":72},">\n",[62,299,301,304,306,308],{"class":64,"line":300},3,[62,302,303],{"class":83},"    print",[62,305,88],{"class":87},[62,307,229],{"class":91},[62,309,95],{"class":87},[62,311,313,315,317,319,321,323,325,327,329],{"class":64,"line":312},4,[62,314,20],{"class":178},[62,316,181],{"class":87},[62,318,184],{"class":68},[62,320,188],{"class":187},[62,322,229],{"class":191},[62,324,188],{"class":187},[62,326,196],{"class":72},[62,328,199],{"class":72},[62,330,202],{"class":68},[14,332,333,334,336],{},"The problem is on the first line. ",[18,335,229],{}," is used before it is assigned a value.",[14,338,339],{},"Python runs code from top to bottom, so it must see the assignment first.",[48,341,343],{"id":342},"common-causes","Common causes",[14,345,346,347,181],{},"These are the most common reasons for ",[18,348,20],{},[26,350,351,354,357,360,363,366,369],{},[29,352,353],{},"Misspelled variable or function name",[29,355,356],{},"Using a variable before it is assigned",[29,358,359],{},"Forgetting quotes around a string value",[29,361,362],{},"Forgetting to import a module or function",[29,364,365],{},"Using the wrong variable name inside a loop or function",[29,367,368],{},"Trying to use a name that only exists in another scope",[29,370,371],{},"Running only part of a script in a notebook or interactive session",[373,374,376],"h3",{"id":375},"example-misspelled-name","Example: misspelled name",[53,378,380],{"className":55,"code":379,"language":57,"meta":58,"style":58},"username = \"Maya\"\nprint(usernme)\n",[18,381,382,398],{"__ignoreMap":58},[62,383,384,387,389,392,395],{"class":64,"line":65},[62,385,386],{"class":68},"username ",[62,388,73],{"class":72},[62,390,391],{"class":187}," \"",[62,393,394],{"class":191},"Maya",[62,396,397],{"class":187},"\"\n",[62,399,400,402,404,407],{"class":64,"line":80},[62,401,84],{"class":83},[62,403,88],{"class":87},[62,405,406],{"class":91},"usernme",[62,408,95],{"class":87},[14,410,168],{},[53,412,414],{"className":55,"code":413,"language":57,"meta":58,"style":58},"NameError: name 'usernme' is not defined\n",[18,415,416],{"__ignoreMap":58},[62,417,418,420,422,424,426,428,430,432,434],{"class":64,"line":65},[62,419,20],{"class":178},[62,421,181],{"class":87},[62,423,184],{"class":68},[62,425,188],{"class":187},[62,427,406],{"class":191},[62,429,188],{"class":187},[62,431,196],{"class":72},[62,433,199],{"class":72},[62,435,202],{"class":68},[14,437,438,441,442,444],{},[18,439,440],{},"username"," was defined, but ",[18,443,406],{}," was used.",[373,446,448],{"id":447},"example-missing-quotes","Example: missing quotes",[53,450,452],{"className":55,"code":451,"language":57,"meta":58,"style":58},"print(hello)\n",[18,453,454],{"__ignoreMap":58},[62,455,456,458,460,463],{"class":64,"line":65},[62,457,84],{"class":83},[62,459,88],{"class":87},[62,461,462],{"class":91},"hello",[62,464,95],{"class":87},[14,466,168],{},[53,468,470],{"className":55,"code":469,"language":57,"meta":58,"style":58},"NameError: name 'hello' is not defined\n",[18,471,472],{"__ignoreMap":58},[62,473,474,476,478,480,482,484,486,488,490],{"class":64,"line":65},[62,475,20],{"class":178},[62,477,181],{"class":87},[62,479,184],{"class":68},[62,481,188],{"class":187},[62,483,462],{"class":191},[62,485,188],{"class":187},[62,487,196],{"class":72},[62,489,199],{"class":72},[62,491,202],{"class":68},[14,493,494],{},"If you meant text, write:",[53,496,498],{"className":55,"code":497,"language":57,"meta":58,"style":58},"print(\"hello\")\n",[18,499,500],{"__ignoreMap":58},[62,501,502,504,506,508,510,512],{"class":64,"line":65},[62,503,84],{"class":83},[62,505,88],{"class":87},[62,507,269],{"class":187},[62,509,462],{"class":191},[62,511,269],{"class":187},[62,513,95],{"class":87},[373,515,517],{"id":516},"example-missing-import","Example: missing import",[53,519,521],{"className":55,"code":520,"language":57,"meta":58,"style":58},"print(math.sqrt(16))\n",[18,522,523],{"__ignoreMap":58},[62,524,525,527,529,532,534,537,539,542],{"class":64,"line":65},[62,526,84],{"class":83},[62,528,88],{"class":87},[62,530,531],{"class":91},"math",[62,533,46],{"class":87},[62,535,536],{"class":91},"sqrt",[62,538,88],{"class":87},[62,540,541],{"class":76},"16",[62,543,544],{"class":87},"))\n",[14,546,168],{},[53,548,550],{"className":55,"code":549,"language":57,"meta":58,"style":58},"NameError: name 'math' is not defined\n",[18,551,552],{"__ignoreMap":58},[62,553,554,556,558,560,562,564,566,568,570],{"class":64,"line":65},[62,555,20],{"class":178},[62,557,181],{"class":87},[62,559,184],{"class":68},[62,561,188],{"class":187},[62,563,531],{"class":191},[62,565,188],{"class":187},[62,567,196],{"class":72},[62,569,199],{"class":72},[62,571,202],{"class":68},[14,573,574,575,577],{},"This happens because ",[18,576,531],{}," was never imported.",[14,579,580,581,586,587,46],{},"If you are still learning how variables work, see ",[582,583,585],"a",{"href":584},"\u002Flearn\u002Fpython-variables-explained-for-beginners\u002F","Python variables explained for beginners",". If the problem is related to imports, see ",[582,588,590],{"href":589},"\u002Flearn\u002Fhow-import-works-in-python\u002F","how import works in Python",[48,592,594],{"id":593},"how-to-fix-a-nameerror","How to fix a NameError",[14,596,597],{},"Use this checklist:",[26,599,600,603,606,609,612,615],{},[29,601,602],{},"Check the spelling of the name carefully",[29,604,605],{},"Define the variable before using it",[29,607,608],{},"Add quotes if you meant a string",[29,610,611],{},"Import the module or function before calling it",[29,613,614],{},"Make sure the name exists in the current function or file",[29,616,617],{},"Run the code from the top so earlier definitions are available",[14,619,620],{},"A good way to think about it is:",[622,623,624,627,630,633],"ol",{},[29,625,626],{},"Find the exact unknown name in the error message",[29,628,629],{},"Search earlier in the file for where it should be created",[29,631,632],{},"Fix the missing definition, typo, quotes, or import",[29,634,635],{},"Run the program again",[48,637,639],{"id":638},"example-fixes","Example fixes",[373,641,643],{"id":642},"fix-a-typo-in-a-variable-name","Fix a typo in a variable name",[14,645,646],{},"Wrong:",[53,648,650],{"className":55,"code":649,"language":57,"meta":58,"style":58},"message = \"Welcome\"\nprint(mesage)\n",[18,651,652,666],{"__ignoreMap":58},[62,653,654,657,659,661,664],{"class":64,"line":65},[62,655,656],{"class":68},"message ",[62,658,73],{"class":72},[62,660,391],{"class":187},[62,662,663],{"class":191},"Welcome",[62,665,397],{"class":187},[62,667,668,670,672,675],{"class":64,"line":80},[62,669,84],{"class":83},[62,671,88],{"class":87},[62,673,674],{"class":91},"mesage",[62,676,95],{"class":87},[14,678,679],{},"Correct:",[53,681,683],{"className":55,"code":682,"language":57,"meta":58,"style":58},"message = \"Welcome\"\nprint(message)\n",[18,684,685,697],{"__ignoreMap":58},[62,686,687,689,691,693,695],{"class":64,"line":65},[62,688,656],{"class":68},[62,690,73],{"class":72},[62,692,391],{"class":187},[62,694,663],{"class":191},[62,696,397],{"class":187},[62,698,699,701,703,706],{"class":64,"line":80},[62,700,84],{"class":83},[62,702,88],{"class":87},[62,704,705],{"class":91},"message",[62,707,95],{"class":87},[373,709,711,712],{"id":710},"move-the-assignment-above-the-print","Move the assignment above the ",[18,713,714],{},"print()",[14,716,646],{},[53,718,720],{"className":55,"code":719,"language":57,"meta":58,"style":58},"print(price)\nprice = 9.99\n",[18,721,722,733],{"__ignoreMap":58},[62,723,724,726,728,731],{"class":64,"line":65},[62,725,84],{"class":83},[62,727,88],{"class":87},[62,729,730],{"class":91},"price",[62,732,95],{"class":87},[62,734,735,738,740],{"class":64,"line":80},[62,736,737],{"class":68},"price ",[62,739,73],{"class":72},[62,741,742],{"class":76}," 9.99\n",[14,744,679],{},[53,746,748],{"className":55,"code":747,"language":57,"meta":58,"style":58},"price = 9.99\nprint(price)\n",[18,749,750,758],{"__ignoreMap":58},[62,751,752,754,756],{"class":64,"line":65},[62,753,737],{"class":68},[62,755,73],{"class":72},[62,757,742],{"class":76},[62,759,760,762,764,766],{"class":64,"line":80},[62,761,84],{"class":83},[62,763,88],{"class":87},[62,765,730],{"class":91},[62,767,95],{"class":87},[373,769,771,772,774,775,778],{"id":770},"change-hello-to-hello-when-text-is-intended","Change ",[18,773,462],{}," to ",[18,776,777],{},"\"hello\""," when text is intended",[14,780,646],{},[53,782,784],{"className":55,"code":783,"language":57,"meta":58,"style":58},"word = hello\nprint(word)\n",[18,785,786,796],{"__ignoreMap":58},[62,787,788,791,793],{"class":64,"line":65},[62,789,790],{"class":68},"word ",[62,792,73],{"class":72},[62,794,795],{"class":68}," hello\n",[62,797,798,800,802,805],{"class":64,"line":80},[62,799,84],{"class":83},[62,801,88],{"class":87},[62,803,804],{"class":91},"word",[62,806,95],{"class":87},[14,808,679],{},[53,810,812],{"className":55,"code":811,"language":57,"meta":58,"style":58},"word = \"hello\"\nprint(word)\n",[18,813,814,826],{"__ignoreMap":58},[62,815,816,818,820,822,824],{"class":64,"line":65},[62,817,790],{"class":68},[62,819,73],{"class":72},[62,821,391],{"class":187},[62,823,462],{"class":191},[62,825,397],{"class":187},[62,827,828,830,832,834],{"class":64,"line":80},[62,829,84],{"class":83},[62,831,88],{"class":87},[62,833,804],{"class":91},[62,835,95],{"class":87},[373,837,839,840,842,843],{"id":838},"import-math-before-using-mathsqrt","Import ",[18,841,531],{}," before using ",[18,844,845],{},"math.sqrt()",[14,847,646],{},[53,849,851],{"className":55,"code":850,"language":57,"meta":58,"style":58},"print(math.sqrt(25))\n",[18,852,853],{"__ignoreMap":58},[62,854,855,857,859,861,863,865,867,870],{"class":64,"line":65},[62,856,84],{"class":83},[62,858,88],{"class":87},[62,860,531],{"class":91},[62,862,46],{"class":87},[62,864,536],{"class":91},[62,866,88],{"class":87},[62,868,869],{"class":76},"25",[62,871,544],{"class":87},[14,873,679],{},[53,875,877],{"className":55,"code":876,"language":57,"meta":58,"style":58},"import math\n\nprint(math.sqrt(25))\n",[18,878,879,888,894],{"__ignoreMap":58},[62,880,881,885],{"class":64,"line":65},[62,882,884],{"class":883},"sVHd0","import",[62,886,887],{"class":68}," math\n",[62,889,890],{"class":64,"line":80},[62,891,893],{"emptyLinePlaceholder":892},true,"\n",[62,895,896,898,900,902,904,906,908,910],{"class":64,"line":300},[62,897,84],{"class":83},[62,899,88],{"class":87},[62,901,531],{"class":91},[62,903,46],{"class":87},[62,905,536],{"class":91},[62,907,88],{"class":87},[62,909,869],{"class":76},[62,911,544],{"class":87},[14,913,168],{},[53,915,917],{"className":55,"code":916,"language":57,"meta":58,"style":58},"5.0\n",[18,918,919],{"__ignoreMap":58},[62,920,921],{"class":64,"line":65},[62,922,916],{"class":76},[48,924,926],{"id":925},"nameerror-vs-unboundlocalerror","NameError vs UnboundLocalError",[14,928,929],{},"These two errors are related, but they are not the same.",[373,931,933],{"id":932},"nameerror",[18,934,20],{},[14,936,937,939],{},[18,938,20],{}," means the name does not exist at all in the current lookup path.",[14,941,942],{},"Example:",[53,944,946],{"className":55,"code":945,"language":57,"meta":58,"style":58},"print(count)\n",[18,947,948],{"__ignoreMap":58},[62,949,950,952,954,957],{"class":64,"line":65},[62,951,84],{"class":83},[62,953,88],{"class":87},[62,955,956],{"class":91},"count",[62,958,95],{"class":87},[14,960,961,962,964,965,46],{},"If ",[18,963,956],{}," was never defined, Python raises ",[18,966,20],{},[373,968,970],{"id":969},"unboundlocalerror",[18,971,972],{},"UnboundLocalError",[14,974,975,977],{},[18,976,972],{}," means Python thinks a variable is local to a function, but you used it before assigning a value inside that function.",[14,979,942],{},[53,981,983],{"className":55,"code":982,"language":57,"meta":58,"style":58},"count = 10\n\ndef show_count():\n    print(count)\n    count = 20\n\nshow_count()\n",[18,984,985,994,998,1011,1021,1032,1037],{"__ignoreMap":58},[62,986,987,990,992],{"class":64,"line":65},[62,988,989],{"class":68},"count ",[62,991,73],{"class":72},[62,993,77],{"class":76},[62,995,996],{"class":64,"line":80},[62,997,893],{"emptyLinePlaceholder":892},[62,999,1000,1004,1008],{"class":64,"line":300},[62,1001,1003],{"class":1002},"sbsja","def",[62,1005,1007],{"class":1006},"sGLFI"," show_count",[62,1009,1010],{"class":87},"():\n",[62,1012,1013,1015,1017,1019],{"class":64,"line":312},[62,1014,303],{"class":83},[62,1016,88],{"class":87},[62,1018,956],{"class":91},[62,1020,95],{"class":87},[62,1022,1024,1027,1029],{"class":64,"line":1023},5,[62,1025,1026],{"class":68},"    count ",[62,1028,73],{"class":72},[62,1030,1031],{"class":76}," 20\n",[62,1033,1035],{"class":64,"line":1034},6,[62,1036,893],{"emptyLinePlaceholder":892},[62,1038,1040,1043],{"class":64,"line":1039},7,[62,1041,1042],{"class":91},"show_count",[62,1044,1045],{"class":87},"()\n",[14,1047,1048,1049,1051,1052,46],{},"This raises ",[18,1050,972],{},", not ",[18,1053,20],{},[14,1055,1056,1057,46],{},"If you are not sure which one you have, read ",[582,1058,1060],{"href":1059},"\u002Ferrors\u002Funboundlocalerror-local-variable-referenced-before-assignment-fix\u002F","UnboundLocalError: local variable referenced before assignment fix",[48,1062,1064],{"id":1063},"beginner-debugging-steps","Beginner debugging steps",[14,1066,1067,1068,1070],{},"When you see ",[18,1069,20],{},", follow these steps:",[622,1072,1073,1076,1079,1082,1085],{},[29,1074,1075],{},"Look at the exact line number in the traceback",[29,1077,1078],{},"Find the unknown name on that line",[29,1080,1081],{},"Search earlier in the file for where it should be defined",[29,1083,1084],{},"Check for typos, missing imports, and missing quotes",[29,1086,1087],{},"Test a small fix and run the file again",[14,1089,1090],{},"These commands can also help while debugging:",[53,1092,1096],{"className":1093,"code":1094,"language":1095,"meta":58,"style":58},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python your_script.py\n","bash",[18,1097,1098],{"__ignoreMap":58},[62,1099,1100,1103],{"class":64,"line":65},[62,1101,57],{"class":1102},"sbgvK",[62,1104,1105],{"class":191}," your_script.py\n",[14,1107,1108],{},"Run the whole script from the top. This helps when the problem is caused by missing earlier definitions.",[53,1110,1112],{"className":55,"code":1111,"language":57,"meta":58,"style":58},"print(variable_name)\nprint(type(variable_name))\ndir()\nhelp(name)\n",[18,1113,1114,1125,1140,1147],{"__ignoreMap":58},[62,1115,1116,1118,1120,1123],{"class":64,"line":65},[62,1117,84],{"class":83},[62,1119,88],{"class":87},[62,1121,1122],{"class":91},"variable_name",[62,1124,95],{"class":87},[62,1126,1127,1129,1131,1134,1136,1138],{"class":64,"line":80},[62,1128,84],{"class":83},[62,1130,88],{"class":87},[62,1132,1133],{"class":178},"type",[62,1135,88],{"class":87},[62,1137,1122],{"class":91},[62,1139,544],{"class":87},[62,1141,1142,1145],{"class":64,"line":300},[62,1143,1144],{"class":83},"dir",[62,1146,1045],{"class":87},[62,1148,1149,1152,1154,1157],{"class":64,"line":312},[62,1150,1151],{"class":83},"help",[62,1153,88],{"class":87},[62,1155,1156],{"class":91},"name",[62,1158,95],{"class":87},[14,1160,1161],{},"Use them carefully:",[26,1163,1164,1170,1176,1182],{},[29,1165,1166,1169],{},[18,1167,1168],{},"print(variable_name)"," can help if the variable exists",[29,1171,1172,1175],{},[18,1173,1174],{},"print(type(variable_name))"," shows what kind of value it is",[29,1177,1178,1181],{},[18,1179,1180],{},"dir()"," shows names available in the current scope",[29,1183,1184,1187],{},[18,1185,1186],{},"help(name)"," gives information about a function, class, or module",[14,1189,1190,1191,1193,1194,1196],{},"Be aware that ",[18,1192,1168],{}," and ",[18,1195,1186],{}," will also fail if the name truly does not exist yet.",[48,1198,1200],{"id":1199},"common-mistakes","Common mistakes",[14,1202,1203,1204,1206],{},"Some beginner mistakes cause ",[18,1205,20],{}," again and again:",[26,1208,1209,1212,1215,1218,1221,1224],{},[29,1210,1211],{},"Typing a variable name incorrectly",[29,1213,1214],{},"Using a variable before assigning it",[29,1216,1217],{},"Forgetting to put quotes around text",[29,1219,1220],{},"Forgetting an import statement",[29,1222,1223],{},"Using a local name outside its scope",[29,1225,1226],{},"Running only part of a script in an interactive environment",[14,1228,1229],{},"For example, this can happen in a notebook:",[53,1231,1233],{"className":55,"code":1232,"language":57,"meta":58,"style":58},"# Cell 2\nprint(total)\n",[18,1234,1235,1241],{"__ignoreMap":58},[62,1236,1237],{"class":64,"line":65},[62,1238,1240],{"class":1239},"sutJx","# Cell 2\n",[62,1242,1243,1245,1247,1249],{"class":64,"line":80},[62,1244,84],{"class":83},[62,1246,88],{"class":87},[62,1248,163],{"class":91},[62,1250,95],{"class":87},[14,1252,1253,1254,1256,1257,46],{},"If you forgot to run the cell that creates ",[18,1255,163],{},", Python will raise ",[18,1258,20],{},[14,1260,1261,1262,1265,1266,46],{},"If your error message is specifically ",[18,1263,1264],{},"name 'x' is not defined",", see the more focused guide: ",[582,1267,1269],{"href":1268},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix\u002F","NameError: name is not defined fix",[48,1271,1273],{"id":1272},"faq","FAQ",[373,1275,1277],{"id":1276},"what-is-the-difference-between-nameerror-and-syntaxerror","What is the difference between NameError and SyntaxError?",[14,1279,1280,1283,1284,1286],{},[18,1281,1282],{},"SyntaxError"," means the code is written in an invalid way. ",[18,1285,20],{}," means the code syntax is valid, but Python cannot find a name at runtime.",[373,1288,1290],{"id":1289},"why-do-i-get-nameerror-for-text-like-hello","Why do I get NameError for text like hello?",[14,1292,1293,1294,1296,1297,46],{},"Python treats ",[18,1295,462],{}," as a variable name unless you write it as a string with quotes, like ",[18,1298,777],{},[373,1300,1302],{"id":1301},"can-a-missing-import-cause-nameerror","Can a missing import cause NameError?",[14,1304,1305,1306,46],{},"Yes. If you use a module, function, or class without importing it first, Python may raise ",[18,1307,20],{},[14,1309,1310,1311,46],{},"If the problem is about importing a package that does not exist, you may also need ",[582,1312,1314],{"href":1313},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix","ModuleNotFoundError: No module named X fix",[373,1316,1318],{"id":1317},"why-does-code-work-in-one-cell-but-fail-in-another","Why does code work in one cell but fail in another?",[14,1320,1321],{},"In notebooks or interactive sessions, a name may only exist if the cell that defines it was run first.",[48,1323,1325],{"id":1324},"see-also","See also",[26,1327,1328,1332,1336,1340,1345,1349],{},[29,1329,1330],{},[582,1331,1269],{"href":1268},[29,1333,1334],{},[582,1335,1060],{"href":1059},[29,1337,1338],{},[582,1339,585],{"href":584},[29,1341,1342],{},[582,1343,1344],{"href":589},"How import works in Python",[29,1346,1347],{},[582,1348,1314],{"href":1313},[29,1350,1351],{},[582,1352,1354],{"href":1353},"\u002Ferrors\u002Fattributeerror-in-python-causes-and-fixes\u002F","AttributeError in Python: Causes and Fixes",[1356,1357,1358],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 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 .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 .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}",{"title":58,"searchDepth":80,"depth":80,"links":1360},[1361,1362,1363,1364,1369,1370,1379,1383,1384,1385,1391],{"id":50,"depth":80,"text":51},{"id":120,"depth":80,"text":121},{"id":211,"depth":80,"text":212},{"id":342,"depth":80,"text":343,"children":1365},[1366,1367,1368],{"id":375,"depth":300,"text":376},{"id":447,"depth":300,"text":448},{"id":516,"depth":300,"text":517},{"id":593,"depth":80,"text":594},{"id":638,"depth":80,"text":639,"children":1371},[1372,1373,1375,1377],{"id":642,"depth":300,"text":643},{"id":710,"depth":300,"text":1374},"Move the assignment above the print()",{"id":770,"depth":300,"text":1376},"Change hello to \"hello\" when text is intended",{"id":838,"depth":300,"text":1378},"Import math before using math.sqrt()",{"id":925,"depth":80,"text":926,"children":1380},[1381,1382],{"id":932,"depth":300,"text":20},{"id":969,"depth":300,"text":972},{"id":1063,"depth":80,"text":1064},{"id":1199,"depth":80,"text":1200},{"id":1272,"depth":80,"text":1273,"children":1386},[1387,1388,1389,1390],{"id":1276,"depth":300,"text":1277},{"id":1289,"depth":300,"text":1290},{"id":1301,"depth":300,"text":1302},{"id":1317,"depth":300,"text":1318},{"id":1324,"depth":80,"text":1325},"Master nameerror in python causes and fixes in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fnameerror-in-python-causes-and-fixes",{"title":5,"description":1392},"errors\u002Fnameerror-in-python-causes-and-fixes","gYaZYQzjyz6N4ceii1rM-7pjpGwWx-ti3U2PsQZDc9Q",1777585495689]