Python "IndentationError" After Copy-Pasting Code
Python's IndentationError after pasting code from another source almost always comes down to mixed tabs and spaces, or indentation that visually looks correct in your editor but uses a different number of spaces than the surrounding code — Python cares about this at a level most editors don't make obvious.
The Problem
You copy a working code snippet from documentation, a chat message, or another file, paste it into your script, and running it fails immediately:
File "script.py", line 12
result = calculate(x, y)
^
IndentationError: unexpected indent
The pasted code looks correctly aligned in your editor, which makes the error confusing — visually, nothing seems wrong.
Why It Happens
Python uses indentation to define code blocks, and it's strict about consistency in a way that's invisible to the human eye in most editors. Common causes:
- The pasted code uses tabs, while the surrounding code uses spaces (or vice versa) — most editors render both to look identical on screen, but Python treats them as genuinely different characters, and mixing them within the same block raises an error
- The pasted code was indented with a different number of spaces per level (2 spaces vs. 4 spaces is common) than the rest of your file, so even though both are "spaces," the actual indentation depth doesn't line up correctly with the surrounding block
- Copying from a source that added extra leading whitespace invisibly, particularly common when pasting from web pages, PDFs, or chat applications that sometimes introduce unexpected characters during copy
- Copying a code block that was indented relative to a different starting point (like inside a class or function in the source) and pasting it at a different indentation level in your file without adjusting
The Fix
First, make invisible whitespace visible in your editor. Most code editors have a "show whitespace" or "render whitespace" setting that displays tabs and spaces as distinct visible characters, immediately revealing a mismatch that's otherwise invisible:
# VS Code: enable via Settings > "Render Whitespace" > "all"
Once whitespace is visible, look specifically at the boundary between your existing code and the newly pasted section — a switch between tab characters and space characters, or a different number of spaces, becomes immediately obvious once rendered.
Use a tool to automatically detect and fix mixed tabs/spaces rather than manually retyping indentation. Python itself includes a built-in check for this:
python -m tabnanny script.py
This flags exactly which lines have ambiguous or inconsistent indentation, without requiring you to visually inspect every line yourself.
To convert an entire file to consistent indentation (spaces are the Python community standard, specifically 4 spaces per level, per PEP 8), most editors offer a "convert indentation to spaces" command, or you can do it directly with a short script:
with open('script.py', 'r') as f:
content = f.read()
content = content.expandtabs(4)
with open('script.py', 'w') as f:
f.write(content)
Going forward, configure your editor to automatically insert spaces when you press Tab, rather than an actual tab character, which prevents this issue from being introduced in newly written code:
# VS Code settings.json
"editor.insertSpaces": true,
"editor.tabSize": 4
If you regularly paste code from varied sources, consider running your file through an auto-formatter like black after pasting, which normalizes indentation (and much more) automatically, catching this category of issue as a side effect of its normal formatting pass:
pip install black
black script.py
Still Not Working?
If tabnanny reports no issues but you're still seeing an IndentationError, check for invisible non-breaking space characters or other unusual whitespace-like characters that sometimes get introduced when copying from web pages or rich-text sources — these look like normal spaces visually and even to some whitespace-detection tools, but aren't standard ASCII spaces. Retyping the problematic line manually rather than pasting it, or piping the pasted content through a tool that strips non-ASCII characters, resolves this specific edge case when it occurs.