BXUP Track progress & test yourself — free
War Room · Field diagnosis · ~2 min read
WAR ROOM · FIELD DIAGNOSIS

WR-007 · IndentationError / TabError after pasting code

Symptom

Python refuses to run a file that looks perfectly aligned on screen. The message is IndentationError: unexpected indent, IndentationError: unindent does not match any outer indentation level, or TabError: inconsistent use of tabs and spaces in indentation. The arrow in the error often points at a line you never touched.

Context

Almost always after code moved between tools: pasted from a browser tutorial, ChatGPT, WhatsApp, a PDF, or a teammate using a different editor. One part of the file is indented with spaces, another with tab characters — both render as identical-looking gaps, so your eyes cannot tell them apart. Common in the first month, and again any time a team shares snippets outside Git. Linked module: 0.3 (Python that AI actually uses).

Diagnosis

Python defines blocks by indentation, and it treats a tab and four spaces as different things even when they look the same. Three causes cover nearly every case. (1) Pasted code carries tabs while your own lines use spaces (or the reverse) — TabError or a mismatch error on the first line where they meet. (2) A pasted block landed one level deeper or shallower than its surroundings — unexpected indent at the paste boundary. (3) Invisible characters came along with the paste (non-breaking spaces from web pages are notorious). Confirm by making the invisible visible: in VS Code run View → Render Whitespace — spaces show as dots, tabs as arrows. Mixed lines are instantly obvious. The error's line number is where Python gave up, which is usually at or one line below the real inconsistency.

The fix

In VS Code, select the whole file and run Convert Indentation to Spaces from the command palette (Ctrl+Shift+P), then save and rerun — this rewrites every tab to spaces in one move. If the error survives, go to the reported line with whitespace rendering on and re-indent that block by hand: delete all leading whitespace on each line and press Tab (the editor now inserts spaces). For invisible pasted characters, retype the offending line rather than editing it. Then set the editor so it cannot happen again: "Insert Spaces" on, tab size 4 — the Python community standard.

Prevention

Set VS Code to spaces-only once ("editor.insertSpaces": true, tab size 4) and let Render Whitespace stay on while you learn. Never edit the same file in two editors with different settings. When pasting from any chat or web page, paste into the editor and immediately re-indent the block with the editor's own Tab key. Teams solve this permanently with a formatter such as black run before every commit — you will meet that habit in Phase 4.

In an interview

Q: "A junior's code fails with TabError but the file looks perfectly aligned. What is happening and how do you fix it?" Strong answer: explain that tabs and spaces render identically but Python distinguishes them, name paste-from-another-tool as the usual source, give the two-step fix (render whitespace to see it, convert indentation to spaces to cure it), and close with the prevention: one editor setting plus a formatter. Mentioning that the error line can sit below the real cause signals real debugging experience.

This is the free reading view. Open BXUP to track your progress, run the hands-on labs, and prove it with original, exam-pattern assessments.
© Anvi Innovate · BXUP — Be Exponentially Upskilled · BXUP is an independent learning platform, not affiliated with or endorsed by AWS, Microsoft, Google, NVIDIA, IBM, Salesforce, Databricks, Oracle or Anthropic. All trademarks belong to their respective owners. · bxup.in