[{"data":1,"prerenderedAt":1365},["ShallowReactive",2],{"doc:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors":3,"surround:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors":1336,"ancestors:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors":1345,"children:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors":1350,"related:\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors":1351},{"id":4,"title":5,"body":6,"dateModified":1303,"datePublished":1303,"description":1304,"extension":1305,"faq":1306,"keyword":1315,"kind":1316,"meta":1317,"navigation":399,"path":1325,"seo":1326,"slug":1329,"stem":1330,"topics":1331,"type":1334,"__hash__":1335},"docs\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors\u002Findex.md","Fix Python in Excel Errors",{"type":7,"value":8,"toc":1289},"minimark",[9,19,124,129,165,169,179,246,264,310,314,319,514,521,631,635,648,707,717,721,727,791,806,810,813,846,916,923,1013,1017,1023,1029,1033,1174,1178,1189,1193,1219,1223,1229,1235,1241,1247,1251,1285],[10,11,12,13,18],"p",{},"A Python formula can fail in more places than an ordinary Excel formula. The code itself can be wrong; the\ndata sent to it can be too large or keep changing; the result can have nowhere to spill; and because the\ncode runs in a Microsoft cloud container rather than on your machine, the connection, the service and your\nlicence can all get in the way too. Each of those failures shows up as a different error value in the cell.\nThis guide, part of ",[14,15,17],"a",{"href":16},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002F","Using Python in Excel",",\nsorts them by where the problem is and gives the fix for each.",[20,21,29,30,29,34,29,38,29,45,29,54,29,61,29,66,29,71,29,76,29,79,29,84,29,89,29,92,29,97,29,102,29,105,29,110,29,115,29,119],"svg",{"viewBox":22,"role":23,"ariaLabelledBy":24,"xmlns":27,"style":28},"0 0 720 380","img",[25,26],"pye-layers-t","pye-layers-d","http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg","width:100%;max-width:720px;height:auto;display:block;margin:1.5rem auto;font-family:Inter,ui-sans-serif,system-ui,sans-serif","\n  ",[31,32,33],"title",{"id":25},"Python in Excel error values grouped by where the problem lies",[35,36,37],"desc",{"id":26},"Code problems show as PYTHON errors. Data problems show as CALC errors. Output problems show as SPILL errors. Service problems show as BUSY, CONNECT and TIMEOUT. Access and version problems show as BLOCKED and UNKNOWN.",[39,40],"rect",{"x":41,"y":41,"width":42,"height":43,"fill":44},"0","720","380","#ffffff",[39,46],{"x":47,"y":47,"width":48,"height":49,"rx":50,"fill":51,"stroke":52,"style":53},"20","680","52","12","#fee8f2","var(--accent,#f43f8f)","stroke-width:2px",[55,56,60],"text",{"x":57,"y":58,"style":59},"40","51","font-size:14px;font-weight:700;fill:var(--accent-ink,#be185d);text-anchor:start","your code",[55,62,65],{"x":63,"y":58,"style":64},"270","font-size:12px;font-weight:400;fill:var(--text,#172033);text-anchor:start","#PYTHON! — syntax or runtime error in the formula",[39,67],{"x":47,"y":68,"width":48,"height":49,"rx":50,"fill":69,"stroke":70,"style":53},"84","#fdefd8","var(--gold,#b4740a)",[55,72,75],{"x":57,"y":73,"style":74},"115","font-size:14px;font-weight:700;fill:var(--gold-ink,#7a4e06);text-anchor:start","the data sent",[55,77,78],{"x":63,"y":73,"style":64},"#CALC! — too much data, or a volatile source",[39,80],{"x":47,"y":81,"width":48,"height":49,"rx":50,"fill":82,"stroke":83,"style":53},"148","#f0f4ff","var(--brand,#5b5cf0)",[55,85,88],{"x":57,"y":86,"style":87},"179","font-size:14px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:start","the result",[55,90,91],{"x":63,"y":86,"style":64},"#SPILL! — the output range is not empty",[39,93],{"x":47,"y":94,"width":48,"height":49,"rx":50,"fill":95,"stroke":96,"style":53},"212","#d9f4f1","var(--teal,#0f9488)",[55,98,101],{"x":57,"y":99,"style":100},"243","font-size:14px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:start","the cloud runtime",[55,103,104],{"x":63,"y":99,"style":64},"#BUSY!, #CONNECT!, #TIMEOUT! — still running or lost",[39,106],{"x":47,"y":107,"width":48,"height":49,"rx":50,"fill":108,"stroke":109,"style":53},"276","#e7ebef","var(--line,#cdd5e6)",[55,111,114],{"x":57,"y":112,"style":113},"307","font-size:14px;font-weight:700;fill:var(--muted,#5b6780);text-anchor:start","access and version",[55,116,118],{"x":63,"y":112,"style":117},"font-size:12px;font-weight:400;fill:var(--muted,#5b6780);text-anchor:start","#BLOCKED!, #UNKNOWN! — not enabled or not supported",[55,120,123],{"x":121,"y":121,"style":122},"360","font-size:12.5px;font-weight:400;fill:var(--muted,#5b6780);text-anchor:middle","work from the top: most errors are in the code or the data",[125,126,128],"h2",{"id":127},"prerequisites","Prerequisites",[130,131,132,141],"ul",{},[133,134,135,136,140],"li",{},"Excel with Python in Excel available, and a workbook with at least one ",[137,138,139],"code",{},"=PY"," formula.",[133,142,143,144,148,149,152,153,156,157,160,161,164],{},"Know where the tools are: ",[145,146,147],"strong",{},"Formulas → Reset runtime"," (or ",[145,150,151],{},"Ctrl+Alt+Shift+F9","), the ",[145,154,155],{},"Diagnostics"," pane\n(right-click a Python cell → ",[145,158,159],{},"Diagnostics for this cell","), and ",[145,162,163],{},"File → Options → Advanced",", which holds the\nPython formula timeout setting.",[125,166,168],{"id":167},"step-1-read-python-in-the-diagnostics-pane","Step 1 — Read #PYTHON! in the Diagnostics pane",[10,170,171,174,175,178],{},[137,172,173],{},"#PYTHON!"," means the code raised an exception: a syntax error, a misspelt column name, a type error, a missing\nvariable. The cell cannot show a traceback, so open the Diagnostics pane for that cell. It lists the exception type,\nthe message and the line number, and it also collects anything your code printed with ",[137,176,177],{},"print()",".",[180,181,186],"pre",{"className":182,"code":183,"language":184,"meta":185,"style":185},"language-python shiki shiki-themes github-light github-dark-high-contrast","sales = xl(\"Sales[#All]\", headers=True)\nsales.groupby(\"region\")[\"Amount\"].sum()      # KeyError: 'region' — the column is \"Region\"\n","python","",[137,187,188,224],{"__ignoreMap":185},[189,190,193,197,201,204,208,211,215,217,221],"span",{"class":191,"line":192},"line",1,[189,194,196],{"class":195},"skGVy","sales ",[189,198,200],{"class":199},"s-kum","=",[189,202,203],{"class":195}," xl(",[189,205,207],{"class":206},"srMev","\"Sales[#All]\"",[189,209,210],{"class":195},", ",[189,212,214],{"class":213},"sa561","headers",[189,216,200],{"class":199},[189,218,220],{"class":219},"sP0c6","True",[189,222,223],{"class":195},")\n",[189,225,227,230,233,236,239,242],{"class":191,"line":226},2,[189,228,229],{"class":195},"sales.groupby(",[189,231,232],{"class":206},"\"region\"",[189,234,235],{"class":195},")[",[189,237,238],{"class":206},"\"Amount\"",[189,240,241],{"class":195},"].sum()      ",[189,243,245],{"class":244},"s-wDw","# KeyError: 'region' — the column is \"Region\"\n",[10,247,248,249,252,253,256,257,260,261,263],{},"The Diagnostics pane shows ",[137,250,251],{},"KeyError: 'region'"," against line 2. Column names from a table header are\ncase-sensitive and keep any stray spaces, so when a ",[137,254,255],{},"KeyError"," makes no sense, return ",[137,258,259],{},"list(sales.columns)"," from a\nscratch cell and compare character by character. Three other ",[137,262,173],{}," causes account for most of the rest:",[130,265,266,279,291],{},[133,267,268,273,274,278],{},[145,269,270],{},[137,271,272],{},"NameError"," for a variable defined in another cell — that cell calculates ",[275,276,277],"em",{},"later",". Python cells run across each\nrow from column A, then down, sheet by sheet; move the definition above or to the left of its first use.",[133,280,281,286,287,290],{},[145,282,283],{},[137,284,285],{},"ModuleNotFoundError"," — the package is not part of the curated distribution, and you cannot ",[137,288,289],{},"pip install","\ninside Excel. Use an available library or do that step outside Excel.",[133,292,293,306,307,178],{},[145,294,295,296,210,299,302,303],{},"Errors from ",[137,297,298],{},"pd.read_csv",[137,300,301],{},"pd.read_excel"," or ",[137,304,305],{},"requests"," — the runtime has no access to local files or the\nnetwork. Bring external data in with Power Query and reference the query with ",[137,308,309],{},"xl()",[125,311,313],{"id":312},"step-2-make-your-own-errors-readable","Step 2 — Make your own errors readable",[10,315,316,317,178],{},"The Diagnostics pane is fine for the author and useless for everybody else who opens the workbook. For formulas that\ndepend on data other people maintain, check the inputs first and return a clear message instead of letting a raw\nexception turn into ",[137,318,173],{},[180,320,322],{"className":182,"code":321,"language":184,"meta":185,"style":185},"sales = xl(\"Sales[#All]\", headers=True)\nrequired = {\"Date\", \"Region\", \"Rep\", \"Amount\"}\nmissing = required - set(sales.columns)\n\nif missing:\n    result = f\"Sales table is missing column(s): {', '.join(sorted(missing))}\"\nelif sales[\"Amount\"].isna().all():\n    result = \"Sales table has no amounts yet\"\nelse:\n    result = sales.groupby(\"Region\", as_index=False)[\"Amount\"].sum()\nresult\n",[137,323,324,344,374,394,401,410,446,460,470,479,508],{"__ignoreMap":185},[189,325,326,328,330,332,334,336,338,340,342],{"class":191,"line":192},[189,327,196],{"class":195},[189,329,200],{"class":199},[189,331,203],{"class":195},[189,333,207],{"class":206},[189,335,210],{"class":195},[189,337,214],{"class":213},[189,339,200],{"class":199},[189,341,220],{"class":219},[189,343,223],{"class":195},[189,345,346,349,351,354,357,359,362,364,367,369,371],{"class":191,"line":226},[189,347,348],{"class":195},"required ",[189,350,200],{"class":199},[189,352,353],{"class":195}," {",[189,355,356],{"class":206},"\"Date\"",[189,358,210],{"class":195},[189,360,361],{"class":206},"\"Region\"",[189,363,210],{"class":195},[189,365,366],{"class":206},"\"Rep\"",[189,368,210],{"class":195},[189,370,238],{"class":206},[189,372,373],{"class":195},"}\n",[189,375,377,380,382,385,388,391],{"class":191,"line":376},3,[189,378,379],{"class":195},"missing ",[189,381,200],{"class":199},[189,383,384],{"class":195}," required ",[189,386,387],{"class":199},"-",[189,389,390],{"class":219}," set",[189,392,393],{"class":195},"(sales.columns)\n",[189,395,397],{"class":191,"line":396},4,[189,398,400],{"emptyLinePlaceholder":399},true,"\n",[189,402,404,407],{"class":191,"line":403},5,[189,405,406],{"class":199},"if",[189,408,409],{"class":195}," missing:\n",[189,411,413,416,418,421,424,428,431,434,437,440,443],{"class":191,"line":412},6,[189,414,415],{"class":195},"    result ",[189,417,200],{"class":199},[189,419,420],{"class":199}," f",[189,422,423],{"class":206},"\"Sales table is missing column(s): ",[189,425,427],{"class":426},"sSjpA","{",[189,429,430],{"class":206},"', '",[189,432,433],{"class":195},".join(",[189,435,436],{"class":219},"sorted",[189,438,439],{"class":195},"(missing))",[189,441,442],{"class":426},"}",[189,444,445],{"class":206},"\"\n",[189,447,449,452,455,457],{"class":191,"line":448},7,[189,450,451],{"class":199},"elif",[189,453,454],{"class":195}," sales[",[189,456,238],{"class":206},[189,458,459],{"class":195},"].isna().all():\n",[189,461,463,465,467],{"class":191,"line":462},8,[189,464,415],{"class":195},[189,466,200],{"class":199},[189,468,469],{"class":206}," \"Sales table has no amounts yet\"\n",[189,471,473,476],{"class":191,"line":472},9,[189,474,475],{"class":199},"else",[189,477,478],{"class":195},":\n",[189,480,482,484,486,489,491,493,496,498,501,503,505],{"class":191,"line":481},10,[189,483,415],{"class":195},[189,485,200],{"class":199},[189,487,488],{"class":195}," sales.groupby(",[189,490,361],{"class":206},[189,492,210],{"class":195},[189,494,495],{"class":213},"as_index",[189,497,200],{"class":199},[189,499,500],{"class":219},"False",[189,502,235],{"class":195},[189,504,238],{"class":206},[189,506,507],{"class":195},"].sum()\n",[189,509,511],{"class":191,"line":510},11,[189,512,513],{"class":195},"result\n",[10,515,516,517,178],{},"Returning a string in place of the expected DataFrame means the cell shows a sentence anyone can act on. The pattern\nscales to any input check — types, date ranges, duplicates — using the same tests you would run in a script, described in\n",[14,518,520],{"href":519},"\u002Fadvanced-data-transformation-and-cleaning\u002Fvalidating-excel-data-with-python\u002Fvalidate-excel-columns-before-import-with-pandas\u002F","Validate Excel Columns Before Import with pandas",[20,522,29,528,29,531,29,534,29,538,29,542,29,546,29,550,29,554,29,557,29,562,29,567,29,571,29,576,29,581,29,585,29,587,29,590,29,593,29,596,29,600,29,603,29,606,29,608,29,611,29,614,29,618,29,621,29,624,29,627],{"viewBox":523,"role":23,"ariaLabelledBy":524,"xmlns":27,"style":527},"0 0 760 282",[525,526],"pye-python-t","pye-python-d","width:100%;max-width:760px;height:auto;display:block;margin:1.5rem auto;font-family:Inter,ui-sans-serif,system-ui,sans-serif",[31,529,530],{"id":525},"Diagnosing a #PYTHON! error from its exception type",[35,532,533],{"id":526},"A KeyError usually means a column name does not match the table header. A NameError usually means the variable is defined in a cell that calculates later. A ModuleNotFoundError or file and network errors mean the runtime does not allow that library or access.",[39,535],{"x":41,"y":41,"width":536,"height":537,"fill":44},"760","282",[39,539],{"x":540,"y":47,"width":541,"height":49,"rx":50,"fill":82,"stroke":83,"style":53},"210","340",[55,543,545],{"x":43,"y":49,"style":544},"font-size:14px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:middle","exception shown in Diagnostics",[191,547],{"x1":43,"y1":548,"x2":43,"y2":549,"stroke":83,"style":53},"72","100",[191,551],{"x1":552,"y1":549,"x2":553,"y2":549,"stroke":83,"style":53},"133.3","626.7",[191,555],{"x1":552,"y1":549,"x2":552,"y2":556,"stroke":83,"style":53},"124",[558,559],"polygon",{"points":560,"fill":561},"133.3,127 128.3,118 138.3,118","#5b5cf0",[39,563],{"x":47,"y":564,"width":565,"height":566,"rx":50,"fill":51,"stroke":52,"style":53},"128","226.7","104",[55,568,255],{"x":552,"y":569,"style":570},"150","font-size:11px;font-weight:600;fill:var(--accent-ink,#be185d);text-anchor:middle",[55,572,575],{"x":552,"y":573,"style":574},"174","font-size:14px;font-weight:700;fill:var(--accent-ink,#be185d);text-anchor:middle","column name",[55,577,580],{"x":552,"y":578,"style":579},"196","font-size:11.5px;font-weight:400;fill:var(--text,#172033);text-anchor:middle","compare with",[55,582,584],{"x":552,"y":583,"style":579},"216","list(df.columns)",[191,586],{"x1":43,"y1":549,"x2":43,"y2":556,"stroke":83,"style":53},[558,588],{"points":589,"fill":561},"380,127 375,118 385,118",[39,591],{"x":592,"y":564,"width":565,"height":566,"rx":50,"fill":69,"stroke":70,"style":53},"266.7",[55,594,272],{"x":43,"y":569,"style":595},"font-size:11px;font-weight:600;fill:var(--gold-ink,#7a4e06);text-anchor:middle",[55,597,599],{"x":43,"y":573,"style":598},"font-size:14px;font-weight:700;fill:var(--gold-ink,#7a4e06);text-anchor:middle","calculation order",[55,601,602],{"x":43,"y":578,"style":579},"define above or",[55,604,605],{"x":43,"y":583,"style":579},"to the left",[191,607],{"x1":553,"y1":549,"x2":553,"y2":556,"stroke":83,"style":53},[558,609],{"points":610,"fill":561},"626.7,127 621.7,118 631.7,118",[39,612],{"x":613,"y":564,"width":565,"height":566,"rx":50,"fill":82,"stroke":83,"style":53},"513.3",[55,615,617],{"x":553,"y":569,"style":616},"font-size:11px;font-weight:600;fill:var(--brand-strong,#4338ca);text-anchor:middle","ModuleNotFound \u002F IO",[55,619,620],{"x":553,"y":573,"style":544},"sandbox limits",[55,622,623],{"x":553,"y":578,"style":579},"Power Query or",[55,625,626],{"x":553,"y":583,"style":579},"a script outside",[55,628,630],{"x":43,"y":629,"style":122},"266","the line number in the pane points at the failing statement",[125,632,634],{"id":633},"step-3-fix-calc-from-data-size-or-volatile-inputs","Step 3 — Fix #CALC! from data size or volatile inputs",[10,636,637,640,641,302,644,647],{},[137,638,639],{},"#CALC!"," in a Python cell has two documented causes. The first is size: the data involved in one calculation is limited\nto 100 MB, and referencing a huge range — whole columns, or a very large table with long text fields — can exceed it. The\nsecond is volatility: a formula that references cells whose values change on every recalculation, such as those driven by\n",[137,642,643],{},"RAND()",[137,645,646],{},"NOW()",", cannot settle.",[180,649,651],{"className":182,"code":650,"language":184,"meta":185,"style":185},"# Too broad: sends every row and every column of a large table\n# sales = xl(\"Sales[#All]\", headers=True)\n\n# Narrower: reference only the columns the analysis needs, via a smaller range or table\namounts = xl(\"Sales!D1:D50000\", headers=True)\nfloat(amounts[\"Amount\"].sum())\n",[137,652,653,658,663,667,672,694],{"__ignoreMap":185},[189,654,655],{"class":191,"line":192},[189,656,657],{"class":244},"# Too broad: sends every row and every column of a large table\n",[189,659,660],{"class":191,"line":226},[189,661,662],{"class":244},"# sales = xl(\"Sales[#All]\", headers=True)\n",[189,664,665],{"class":191,"line":376},[189,666,400],{"emptyLinePlaceholder":399},[189,668,669],{"class":191,"line":396},[189,670,671],{"class":244},"# Narrower: reference only the columns the analysis needs, via a smaller range or table\n",[189,673,674,677,679,681,684,686,688,690,692],{"class":191,"line":403},[189,675,676],{"class":195},"amounts ",[189,678,200],{"class":199},[189,680,203],{"class":195},[189,682,683],{"class":206},"\"Sales!D1:D50000\"",[189,685,210],{"class":195},[189,687,214],{"class":213},[189,689,200],{"class":199},[189,691,220],{"class":219},[189,693,223],{"class":195},[189,695,696,699,702,704],{"class":191,"line":412},[189,697,698],{"class":219},"float",[189,700,701],{"class":195},"(amounts[",[189,703,238],{"class":206},[189,705,706],{"class":195},"].sum())\n",[10,708,709,710,302,713,716],{},"For size, reference the narrowest range or table that contains what you need, pre-aggregate in Power Query before the data\nreaches Python, or split the work across cells so that no single formula carries the full dataset. For volatility, move the\nrandom or time-dependent value into Python itself — ",[137,711,712],{},"np.random.default_rng(42)",[137,714,715],{},"pd.Timestamp.now()"," — or copy the volatile\ncells as static values before referencing them.",[125,718,720],{"id":719},"step-4-clear-the-way-for-spill","Step 4 — Clear the way for #SPILL!",[10,722,723,726],{},[137,724,725],{},"#SPILL!"," means the result was converted to Excel values and needs more cells than are free: something already sits in the\nblock below or to the right. Select the cell to see the dashed outline of the range it wants.",[180,728,730],{"className":182,"code":729,"language":184,"meta":185,"style":185},"sales = xl(\"Sales[#All]\", headers=True)\nsummary = sales.groupby(\"Rep\", as_index=False)[\"Amount\"].sum()\nsummary.head(8)                  # a capped height makes the spill size predictable\n",[137,731,732,752,777],{"__ignoreMap":185},[189,733,734,736,738,740,742,744,746,748,750],{"class":191,"line":192},[189,735,196],{"class":195},[189,737,200],{"class":199},[189,739,203],{"class":195},[189,741,207],{"class":206},[189,743,210],{"class":195},[189,745,214],{"class":213},[189,747,200],{"class":199},[189,749,220],{"class":219},[189,751,223],{"class":195},[189,753,754,757,759,761,763,765,767,769,771,773,775],{"class":191,"line":226},[189,755,756],{"class":195},"summary ",[189,758,200],{"class":199},[189,760,488],{"class":195},[189,762,366],{"class":206},[189,764,210],{"class":195},[189,766,495],{"class":213},[189,768,200],{"class":199},[189,770,500],{"class":219},[189,772,235],{"class":195},[189,774,238],{"class":206},[189,776,507],{"class":195},[189,778,779,782,785,788],{"class":191,"line":376},[189,780,781],{"class":195},"summary.head(",[189,783,784],{"class":219},"8",[189,786,787],{"class":195},")                  ",[189,789,790],{"class":244},"# a capped height makes the spill size predictable\n",[10,792,793,794,797,798,801,802,178],{},"Clear the cells in the way, move the formula to an empty area, or switch the output type back to ",[145,795,796],{},"Python object"," if the\nresult is only an intermediate step. Capping rows with ",[137,799,800],{},"head()"," and selecting explicit columns stops a result from growing into\nneighbouring content as the data grows; the full technique is in\n",[14,803,805],{"href":804},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Freturn-dataframes-and-values-from-python-in-excel-cells\u002F","Return DataFrames and Values from Python in Excel Cells",[125,807,809],{"id":808},"step-5-handle-busy-connect-and-timeout","Step 5 — Handle #BUSY!, #CONNECT! and #TIMEOUT!",[10,811,812],{},"These three come from the cloud runtime rather than from your formula.",[130,814,815,827,835],{},[133,816,817,822,823,302,825,178],{},[145,818,819],{},[137,820,821],{},"#BUSY!"," means the calculation is still running in the Microsoft cloud. It is normal for a few seconds. If it persists\nfor more than about a minute, reset the runtime with ",[145,824,147],{},[145,826,151],{},[133,828,829,834],{},[145,830,831],{},[137,832,833],{},"#CONNECT!"," means the connection to the service was lost — a network drop, sleep and resume, or a service interruption.\nCheck connectivity, then reset the runtime.",[133,836,837,842,843,845],{},[145,838,839],{},[137,840,841],{},"#TIMEOUT!"," means the formula ran longer than the permitted execution time. Retry or reset first; if the formula is\ngenuinely slow, raise the limit under ",[145,844,163],{},", in the calculation settings for the workbook, where the\nPython formula timeout is set — and look at making the code faster.",[180,847,849],{"className":182,"code":848,"language":184,"meta":185,"style":185},"sales = xl(\"Sales[#All]\", headers=True)\n\n# Slow: a Python loop over rows\n# totals = {}\n# for _, row in sales.iterrows():\n#     totals[row[\"Region\"]] = totals.get(row[\"Region\"], 0) + row[\"Amount\"]\n\n# Fast: one vectorised aggregation\nsales.groupby(\"Region\")[\"Amount\"].sum()\n",[137,850,851,871,875,880,885,890,895,899,904],{"__ignoreMap":185},[189,852,853,855,857,859,861,863,865,867,869],{"class":191,"line":192},[189,854,196],{"class":195},[189,856,200],{"class":199},[189,858,203],{"class":195},[189,860,207],{"class":206},[189,862,210],{"class":195},[189,864,214],{"class":213},[189,866,200],{"class":199},[189,868,220],{"class":219},[189,870,223],{"class":195},[189,872,873],{"class":191,"line":226},[189,874,400],{"emptyLinePlaceholder":399},[189,876,877],{"class":191,"line":376},[189,878,879],{"class":244},"# Slow: a Python loop over rows\n",[189,881,882],{"class":191,"line":396},[189,883,884],{"class":244},"# totals = {}\n",[189,886,887],{"class":191,"line":403},[189,888,889],{"class":244},"# for _, row in sales.iterrows():\n",[189,891,892],{"class":191,"line":412},[189,893,894],{"class":244},"#     totals[row[\"Region\"]] = totals.get(row[\"Region\"], 0) + row[\"Amount\"]\n",[189,896,897],{"class":191,"line":448},[189,898,400],{"emptyLinePlaceholder":399},[189,900,901],{"class":191,"line":462},[189,902,903],{"class":244},"# Fast: one vectorised aggregation\n",[189,905,906,908,910,912,914],{"class":191,"line":472},[189,907,229],{"class":195},[189,909,361],{"class":206},[189,911,235],{"class":195},[189,913,238],{"class":206},[189,915,507],{"class":195},[10,917,918,919,922],{},"A reset clears all variables, so every Python cell recalculates from scratch afterwards. Row-by-row loops with ",[137,920,921],{},"iterrows"," are\nthe usual cause of genuine timeouts; vectorised pandas operations do the same work in a fraction of the time. If your Microsoft\n365 plan offers premium compute for Python in Excel, it also shortens queueing on busy formulas.",[20,924,29,929,29,932,29,935,29,938,29,941,29,947,29,951,29,955,29,960,29,963,29,965,29,969,29,972,29,976,29,979,29,982,29,986,29,988,29,992,29,995,29,998,29,1003,29,1006,29,1009],{"viewBox":925,"role":23,"ariaLabelledBy":926,"xmlns":27,"style":527},"0 0 760 156",[927,928],"pye-service-t","pye-service-d",[31,930,931],{"id":927},"Recovering from runtime errors",[35,933,934],{"id":928},"When a Python cell shows BUSY, CONNECT or TIMEOUT, wait briefly, check connectivity, reset the runtime, and if a timeout repeats, speed up the code or raise the timeout setting.",[39,936],{"x":41,"y":41,"width":536,"height":937,"fill":44},"156",[39,939],{"x":47,"y":47,"width":569,"height":940,"rx":50,"fill":108,"stroke":109,"style":53},"86",[55,942,946],{"x":943,"y":944,"style":945},"95","50","font-size:14px;font-weight:700;fill:var(--muted,#5b6780);text-anchor:middle","wait",[55,948,950],{"x":943,"y":548,"style":949},"font-size:11.5px;font-weight:400;fill:var(--muted,#5b6780);text-anchor:middle","#BUSY! \u003C 60 s",[55,952,954],{"x":943,"y":953,"style":949},"92","is normal",[191,956],{"x1":957,"y1":958,"x2":959,"y2":958,"stroke":83,"style":53},"175","63","204",[558,961],{"points":962,"fill":561},"207,63 198,58 198,68",[39,964],{"x":540,"y":47,"width":569,"height":940,"rx":50,"fill":82,"stroke":83,"style":53},[55,966,968],{"x":967,"y":944,"style":544},"285","check network",[55,970,971],{"x":967,"y":548,"style":579},"for #CONNECT!",[191,973],{"x1":974,"y1":958,"x2":975,"y2":958,"stroke":83,"style":53},"365","394",[558,977],{"points":978,"fill":561},"397,63 388,58 388,68",[39,980],{"x":981,"y":47,"width":569,"height":940,"rx":50,"fill":69,"stroke":70,"style":53},"400",[55,983,985],{"x":984,"y":944,"style":598},"475","reset runtime",[55,987,151],{"x":984,"y":548,"style":579},[191,989],{"x1":990,"y1":958,"x2":991,"y2":958,"stroke":83,"style":53},"555","584",[558,993],{"points":994,"fill":561},"587,63 578,58 578,68",[39,996],{"x":997,"y":47,"width":569,"height":940,"rx":50,"fill":95,"stroke":96,"style":53},"590",[55,999,1002],{"x":1000,"y":944,"style":1001},"665","font-size:14px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle","speed up code",[55,1004,1005],{"x":1000,"y":548,"style":579},"or raise the",[55,1007,1008],{"x":1000,"y":953,"style":579},"timeout setting",[55,1010,1012],{"x":43,"y":1011,"style":122},"140","a reset clears every variable; all Python cells recalculate",[125,1014,1016],{"id":1015},"step-6-resolve-blocked-and-unknown","Step 6 — Resolve #BLOCKED! and #UNKNOWN!",[10,1018,1019,1022],{},[137,1020,1021],{},"#BLOCKED!"," means Python formulas are not allowed to calculate for this workbook or account. Python in Excel must be enabled in the\nworkbook, and the account needs access to Microsoft 365 connected services — an administrator can turn those off, and Python in Excel\nis not supported on device-based subscriptions or shared computer activation. The fix is organisational rather than technical: confirm\nthe licence type and connected-services policy with whoever manages your tenant.",[10,1024,1025,1028],{},[137,1026,1027],{},"#UNKNOWN!"," means the version of Excel you opened the file in does not support Python in Excel at all. Mobile apps on iPad, iPhone\nand Android are the common case. The formulas are still stored in the file and calculate again when it is opened in a supported\nversion, but readers on unsupported clients see only the error — which is a good reason to convert final results to static values\nbefore sharing a workbook widely.",[125,1030,1032],{"id":1031},"common-pitfalls","Common pitfalls",[1034,1035,1036,1052],"table",{},[1037,1038,1039],"thead",{},[1040,1041,1042,1046,1049],"tr",{},[1043,1044,1045],"th",{},"Error",[1043,1047,1048],{},"Most likely cause",[1043,1050,1051],{},"Fix",[1053,1054,1055,1068,1083,1101,1113,1126,1138,1150,1162],"tbody",{},[1040,1056,1057,1062,1065],{},[1058,1059,1060],"td",{},[137,1061,173],{},[1058,1063,1064],{},"Exception in the code",[1058,1066,1067],{},"Open Diagnostics for the cell; fix the line it names",[1040,1069,1070,1077,1080],{},[1058,1071,1072,1074,1075],{},[137,1073,173],{}," with ",[137,1076,272],{},[1058,1078,1079],{},"Variable defined in a cell that calculates later",[1058,1081,1082],{},"Move the definition up or left",[1040,1084,1085,1089,1092],{},[1058,1086,1087],{},[137,1088,639],{},[1058,1090,1091],{},"Over 100 MB of data, or volatile inputs",[1058,1093,1094,1095,1097,1098,1100],{},"Narrow references; remove ",[137,1096,643],{},"\u002F",[137,1099,646],{}," dependencies",[1040,1102,1103,1107,1110],{},[1058,1104,1105],{},[137,1106,725],{},[1058,1108,1109],{},"Output range not empty",[1058,1111,1112],{},"Clear the block, move the formula, or return a Python object",[1040,1114,1115,1120,1123],{},[1058,1116,1117,1119],{},[137,1118,821],{}," for over a minute",[1058,1121,1122],{},"Stuck calculation",[1058,1124,1125],{},"Reset runtime",[1040,1127,1128,1132,1135],{},[1058,1129,1130],{},[137,1131,833],{},[1058,1133,1134],{},"Lost connection to the service",[1058,1136,1137],{},"Check network; reset runtime",[1040,1139,1140,1144,1147],{},[1058,1141,1142],{},[137,1143,841],{},[1058,1145,1146],{},"Formula exceeded the time limit",[1058,1148,1149],{},"Vectorise; raise the timeout in Options",[1040,1151,1152,1156,1159],{},[1058,1153,1154],{},[137,1155,1021],{},[1058,1157,1158],{},"Not enabled, or connected services unavailable",[1058,1160,1161],{},"Check licence type and tenant policy",[1040,1163,1164,1168,1171],{},[1058,1165,1166],{},[137,1167,1027],{},[1058,1169,1170],{},"Client does not support Python in Excel",[1058,1172,1173],{},"Open in a supported Excel version",[125,1175,1177],{"id":1176},"performance-and-scale-notes","Performance and scale notes",[10,1179,1180,1181,1184,1185,178],{},"Most service-side errors get rarer as formulas get lighter. Keep the data you send small, aggregate before charting, compute shared\nintermediate results once in an early cell and reuse the variable, and use ",[145,1182,1183],{},"Partial"," calculation while editing so a single change does\nnot trigger every Python cell in the workbook. When a workbook routinely needs more than the runtime comfortably allows — very large\ndata, external connections, custom packages — the job belongs in a script instead; the trade-offs are laid out in\n",[14,1186,1188],{"href":1187},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fpython-in-excel-vs-openpyxl-and-xlwings\u002F","Python in Excel vs openpyxl and xlwings",[125,1190,1192],{"id":1191},"conclusion","Conclusion",[10,1194,1195,1196,1198,1199,1201,1202,1204,1205,1207,1208,1210,1211,1213,1214,1210,1216,1218],{},"Diagnose a Python in Excel error by where it comes from. ",[137,1197,173],{}," is your code — read the Diagnostics pane and check calculation\norder. ",[137,1200,639],{}," is the data — narrow it or remove volatility. ",[137,1203,725],{}," is the output — make room or cap its size. ",[137,1206,821],{},",\n",[137,1209,833],{}," and ",[137,1212,841],{}," are the runtime — wait, check the connection, reset, and speed up slow code. ",[137,1215,1021],{},[137,1217,1027],{},"\nare access and version — a licence, policy or client question rather than a code one. For formulas other people rely on, validate\ninputs and return a readable message before any of these can appear.",[125,1220,1222],{"id":1221},"frequently-asked-questions","Frequently asked questions",[10,1224,1225,1228],{},[145,1226,1227],{},"Where do print statements go in Python in Excel?","\nTo the Diagnostics pane. Right-click the cell and choose Diagnostics for this cell to see printed output along with any error details.",[10,1230,1231,1234],{},[145,1232,1233],{},"Does resetting the runtime lose my data?","\nNo. The workbook's cells and formulas are untouched. The reset clears Python variables in memory, so every Python formula recalculates from the beginning.",[10,1236,1237,1240],{},[145,1238,1239],{},"Why does a formula work for me but show #BLOCKED! for a colleague?","\nAccess is decided per account and client. Their licence type, their organisation's connected-services policy or their Excel version may not allow Python in Excel even though the file is identical.",[10,1242,1243,1246],{},[145,1244,1245],{},"Can I install a missing Python package?","\nNo. Python in Excel uses a curated distribution and does not allow installing packages. Check whether an available library covers the need, or move that step into a script run outside Excel.",[125,1248,1250],{"id":1249},"related","Related",[130,1252,1253,1259,1266,1271,1278],{},[133,1254,1255,1256,1258],{},"Up one level: ",[14,1257,17],{"href":16}," — the full set of guides for =PY formulas.",[133,1260,1261,1265],{},[14,1262,1264],{"href":1263},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Freference-cells-ranges-and-tables-with-xl-in-python-in-excel\u002F","Reference Cells, Ranges and Tables with xl() in Python in Excel"," — the references behind many #PYTHON! and #CALC! errors.",[133,1267,1268,1270],{},[14,1269,805],{"href":804}," — avoiding #SPILL! by shaping results.",[133,1272,1273,1277],{},[14,1274,1276],{"href":1275},"\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002F","Troubleshooting Common Python Excel Errors"," — errors from scripts that read and write workbooks.",[133,1279,1280,1284],{},[14,1281,1283],{"href":1282},"\u002Fgetting-started-with-python-excel-automation\u002Fautomating-excel-with-com-and-pywin32\u002Fhandle-com-errors-and-excel-dialog-prompts-in-python\u002F","Handle COM Errors and Excel Dialog Prompts in Python"," — the equivalent guide for driving Excel from outside.",[1286,1287,1288],"style",{},"html pre.shiki code .skGVy, html code.shiki .skGVy{--shiki-default:#24292E;--shiki-dark:#F0F3F6}html pre.shiki code .s-kum, html code.shiki .s-kum{--shiki-default:#D73A49;--shiki-dark:#FF9492}html pre.shiki code .srMev, html code.shiki .srMev{--shiki-default:#032F62;--shiki-dark:#ADDCFF}html pre.shiki code .sa561, html code.shiki .sa561{--shiki-default:#E36209;--shiki-dark:#FFB757}html pre.shiki code .sP0c6, html code.shiki .sP0c6{--shiki-default:#005CC5;--shiki-dark:#91CBFF}html pre.shiki code .s-wDw, html code.shiki .s-wDw{--shiki-default:#6A737D;--shiki-dark:#BDC4CC}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 .sSjpA, html code.shiki .sSjpA{--shiki-default:#005CC5;--shiki-dark:#FF9492}",{"title":185,"searchDepth":226,"depth":226,"links":1290},[1291,1292,1293,1294,1295,1296,1297,1298,1299,1300,1301,1302],{"id":127,"depth":226,"text":128},{"id":167,"depth":226,"text":168},{"id":312,"depth":226,"text":313},{"id":633,"depth":226,"text":634},{"id":719,"depth":226,"text":720},{"id":808,"depth":226,"text":809},{"id":1015,"depth":226,"text":1016},{"id":1031,"depth":226,"text":1032},{"id":1176,"depth":226,"text":1177},{"id":1191,"depth":226,"text":1192},{"id":1221,"depth":226,"text":1222},{"id":1249,"depth":226,"text":1250},"2026-09-17","What #PYTHON!, #CALC!, #SPILL!, #BUSY!, #CONNECT!, #TIMEOUT!, #BLOCKED! and #UNKNOWN! mean in a =PY cell, how to read the Diagnostics pane, and the fix for each one.","md",[1307,1309,1311,1313],{"q":1227,"a":1308},"To the Diagnostics pane. Right-click the cell and choose Diagnostics for this cell to see printed output along with any error details.",{"q":1233,"a":1310},"No. The workbook's cells and formulas are untouched. The reset clears Python variables in memory, so every Python formula recalculates from the beginning.",{"q":1239,"a":1312},"Access is decided per account and client. Their licence type, their organisation's connected-services policy or their Excel version may not allow Python in Excel even though the file is identical.",{"q":1245,"a":1314},"No. Python in Excel uses a curated distribution and does not allow installing packages. Check whether an available library covers the need, or move that step into a script run outside Excel.","fix python in excel errors",null,{"breadcrumb":1318},[1319,1321,1324],{"name":1320,"item":1097},"Home",{"name":1322,"item":1323},"Getting Started with Python Excel Automation","\u002Fgetting-started-with-python-excel-automation\u002F",{"name":17,"item":16},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors",{"title":1327,"description":1328},"Fix Python in Excel Errors: #PYTHON!, #CALC!, #BUSY! and More","Troubleshoot Python in Excel formulas: read the Diagnostics pane for #PYTHON!, fix #CALC! size limits, #SPILL!, #BUSY!, #CONNECT! and #TIMEOUT! with a runtime reset, and resolve #BLOCKED!.","fix-python-in-excel-errors","getting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Ffix-python-in-excel-errors\u002Findex",[1332,1333],"python-in-excel","troubleshooting","how-to","MLOyWpCXY6W1vTdrU0xKoEpL6UbpYI1JycnxsqljrbM",[1337,1341],{"title":1338,"path":1339,"stem":1340,"children":-1},"Clean Data with pandas in Python in Excel","\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fclean-data-with-pandas-in-python-in-excel","getting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fclean-data-with-pandas-in-python-in-excel\u002Findex",{"title":1342,"path":1343,"stem":1344,"children":-1},"Forecast a Time Series with Python in Excel","\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fforecast-a-time-series-with-python-in-excel","getting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fforecast-a-time-series-with-python-in-excel\u002Findex",[1346,1348],{"path":1347,"title":1322},"\u002Fgetting-started-with-python-excel-automation",{"path":1349,"title":17},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel",[],[1352,1353,1354,1357,1360,1363],{"path":1339,"title":1338,"kind":1334},{"path":1343,"title":1342,"kind":1334},{"path":1355,"title":1356,"kind":1334},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fget-external-data-into-python-in-excel","Get External Data into Python in Excel with Power Query",{"path":1358,"title":1359,"kind":1334},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fplot-charts-with-matplotlib-in-python-in-excel","Plot Charts with matplotlib in Python in Excel",{"path":1361,"title":1188,"kind":1362},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Fpython-in-excel-vs-openpyxl-and-xlwings","compare",{"path":1364,"title":1264,"kind":1334},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-python-in-excel\u002Freference-cells-ranges-and-tables-with-xl-in-python-in-excel",1790972074935]