#PYTHON! error in Excel: causes and how to fix it
Fix #PYTHON! in Python in Excel: read the Diagnostics pane, typos, KeyError from column names, headers=True, syntax errors, cell order, cloud limits.
#PYTHON! means "your Python code raised an error". It appears only in cells that use Python in Excel (the PY function), and it is the Python equivalent of a formula mistake: a typo, a misspelt column name, a missing bracket, a variable that does not exist yet. Excel cannot show Python's error message inside the cell, so it shows #PYTHON! and puts the full message in the Diagnostics pane. Learning to read that pane is the whole trick. This page reproduces the most common mistakes on a small sheet and shows the corrected code. Connection, licence and timing problems with Python show other errors, listed at the end. Background: Python and Copilot in Excel: what they are and how to use them carefully.
What #PYTHON! means
When you commit a Python cell with Ctrl+Enter, Excel sends the code and the referenced ranges to a Microsoft Cloud container, runs it, and brings back the value of the last line. If Python raises an exception at any point, such as SyntaxError, NameError, KeyError, AttributeError or TypeError, the cell shows #PYTHON!. Right-click the cell and choose Diagnostics for this cell (the pane often opens by itself): it shows the exception type, the message and the line number. In the examples below, the formula bar shows how Excel stores the code; in the cell you type only the Python itself.
Cause 1: a typo in a method or function name
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Region | Sales | ||
| 2 | East | 5200 | #PYTHON! | |
| 3 | West | 3100 | ||
| 4 | North | 3900 |
Diagnostics: AttributeError: 'Series' object has no attribute 'summ'.
Result: #PYTHON!
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Region | Sales | ||
| 2 | East | 5200 | 12200 | |
| 3 | West | 3100 | ||
| 4 | North | 3900 |
Corrected: 5,200 + 3,100 + 3,900 = 12,200.
Result: 12200
Python is case-sensitive: .Sum(), .SUM() and .summ() all fail. Excel's function names do not work in Python either; SUM(...) is not Python.
Cause 2: a column name that does not match the header
With headers=True, the first row of the range becomes the column names, exactly as typed, including capitals and spaces.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Region | Sales | ||
| 2 | East | 5200 | #PYTHON! | |
| 3 | West | 3100 | ||
| 4 | North | 3900 |
Diagnostics: KeyError: 'sales'. The header in B1 is "Sales" with a capital S.
Result: #PYTHON!
Fix. Match the header exactly (['Sales']). Trailing spaces in a header cell are a sneaky variant: "Sales " is a different name. df.columns as the last line of a test cell lists the names Python sees.
Cause 3: forgetting headers=True
Without headers=True, xl() treats the first row as data and numbers the columns 0, 1, 2, so there is no column called Sales.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Region | Sales | ||
| 2 | East | 5200 | #PYTHON! | |
| 3 | West | 3100 | ||
| 4 | North | 3900 |
Diagnostics: KeyError: 'Sales'. The column names are 0 and 1.
Result: #PYTHON!
Fix. Add headers=True whenever the range includes a header row. If you insert a reference by selecting cells while in Python mode, Excel usually adds it for you when it detects headers.
Cause 4: syntax errors
A missing bracket or quote, a stray comma, or wrong indentation in a multi-line cell stops Python before it runs anything. The Diagnostics pane shows SyntaxError with the line number. Common slips: curly quotes pasted from a document instead of straight ones, writing = instead of == in a comparison, and forgetting the colon at the end of an if or for line. Code such as df[df['Sales'] > 4000 (missing ]) is a typical example.
Cause 5: a variable that does not exist yet
Python cells run in a fixed order: row by row, left to right, sheet by sheet in workbook order. A variable defined in a cell further down or to the right is not available yet when an earlier cell runs.
# Cell D1 (runs first)
total = df['Sales'].sum()
# Cell D2 (runs second)
df = xl('A1:B4', headers=True)df does not exist yet when D1 needs it: NameError: name 'df' is not defined. Swap the two cells and both work.Fix. Put the cell that defines a variable above (or to the left of) the cells that use it. Many people keep a dedicated setup area at the top of the first sheet that loads the data into DataFrames.
Cause 6: things the cloud container cannot do
- Reading files from your computer, such as
pd.read_csv('C:/data/sales.csv'), fails withFileNotFoundError: the code runs in Microsoft's cloud, not on your PC. Load the data into the workbook (Power Query works well) and usexl(). - Downloading from the internet with
requestsorurllibis blocked; external data must come through Excel. - Importing a library that is not part of the provided distribution raises
ModuleNotFoundError. Popular packages such as pandas, NumPy, Matplotlib, seaborn and statsmodels are included. - Type problems such as calculating a mean over a column that contains text ("n/a") raise
TypeError; clean the column in Excel or withpd.to_numeric(df['Sales'], errors='coerce').
How to find every #PYTHON!
- Go To Special. Press
F5orCtrl+G, click Special, choose Formulas with only Errors ticked. Every formula showing an error is selected, including this one. - Find All.
Ctrl+F(Cmd+F), search for#PYTHON!with Look in: Values and Within: Workbook, then Find All to get a clickable list. - Error Checking. Formulas › Error Checking steps through each error cell; the warning icon beside a cell also shows Excel's short explanation of the specific reason, which for the newer errors is often more precise than the code itself.
- Trace Error (in the Error Checking drop-down) draws arrows back to the cell where the error first appeared, which matters because every formula that refers to the cell shows the same error.
- Open the Diagnostics pane for each #PYTHON! cell: the last line of the traceback names the exception, and the line number points to the failing line of your code.
- Work from the first cell. Because Python cells run in order and share variables, one failing cell near the top can make later cells fail too. Fix them in calculation order.
Other errors you may see in Python cells
| Error | Meaning | What to do |
|---|---|---|
| #PYTHON! | The code raised an exception | Read the Diagnostics pane and correct the code |
| #BUSY! error in Excel: causes and how to fix it #BUSY! | Still calculating in the cloud | Wait; if over a minute, Formulas › Python › Reset runtime |
#TIMEOUT! | The code ran longer than the allowed time | Simplify, or raise the Python formula timeout in File › Options › Advanced |
| #CONNECT! error in Excel: causes and how to fix it #CONNECT! | The connection to the Python cloud failed | Reset the runtime and recalculate |
| #BLOCKED! error in Excel: causes and how to fix it #BLOCKED! | Settings, licence, quota or calculation mode prevent Python | See the #BLOCKED! page |
| #CALC! error in Excel: causes and how to fix it #CALC! | Volatile inputs or too much data sent | Remove RAND/NOW references, reduce data |
| #SPILL! error in Excel: causes and how to fix it #SPILL! | Excel-value output cannot spill | Clear the cells below and to the right |
| #UNKNOWN! error in Excel: causes and how to fix it #UNKNOWN! | This Excel version does not support Python | Update or use Excel for the web |
When you should NOT hide #PYTHON!
Do not wrap a Python cell's result in IFERROR in another cell, and do not catch every exception inside the code with a bare try: ... except: pass. Both hide a real coding mistake and leave other cells calculating with missing data. Catch only the specific case you expect (for example errors='coerce' when converting text to numbers) and let genuine errors show.
Version notes
- #PYTHON! exists only in Microsoft 365 versions with Python in Excel (Windows, Mac and Excel for the web, depending on plan and update channel).
- The code runs in Microsoft's cloud, so an internet connection is required; it uses a fixed set of Python libraries chosen by Microsoft.
- Opened in a version without Python in Excel, Python cells show #UNKNOWN! error in Excel: causes and how to fix it or their last values rather than #PYTHON!.