Title: IndentationError: Fix Bad Indents, Tabs, and Empty Blocks
Open Graph Title: IndentationError: Fix Bad Indents, Tabs, and Empty Blocks
Description: IndentationError means a line's indent doesn't match its block. Align it, convert tabs to spaces, add pass under empty blocks, rerun py_compile.
Open Graph Description: IndentationError means a line's indent doesn't match its block. Align it, convert tabs to spaces, add pass under empty blocks, rerun py_compile.
Opengraph URL: https://thecodeforge.io/python/python-indentationerror/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/python/python-indentationerror/"
},
"headline": "IndentationError: Fix Bad Indents, Tabs, and Empty Blocks",
"description": "IndentationError means a line's indent doesn't match its block. Align it, convert tabs to spaces, add pass under empty blocks, rerun py_compile.",
"keywords": "IndentationError: Fix Bad Indents, Tabs, and Empty Blocks, Python, Beginner, Errors, IndentationError means a line's indent doesn't match its block. Align it, convert tabs to spaces, add pass under empty blocks, rerun py_compile.",
"proficiencyLevel": "Beginner",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-09-23T00:00:00+00:00",
"dateModified": "2026-10-31T00:00:00+00:00",
"url": "https://thecodeforge.io/python/python-indentationerror/",
"inLanguage": "en",
"educationalLevel": "Beginner",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Python", "item": "https://thecodeforge.io/python/"},
{"@type": "ListItem", "position": 3, "name": "IndentationError: Fix Bad Indents, Tabs, and Empty Blocks", "item": "https://thecodeforge.io/python/python-indentationerror/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Can I use braces instead of indentation in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No. Python computes block structure from leading whitespace alone — braces don't exist for grouping. The tokenizer emits INDENT and DEDENT tokens from your spaces and tabs, and inconsistent levels are a syntax error raised before any of your code runs."
}
},
{
"@type": "Question",
"name": "What is TabError and how is it different from IndentationError?",
"acceptedAnswer": {
"@type": "Answer",
"text": "TabError is a subclass of IndentationError raised for ambiguous mixing of tabs and spaces. Because tab width varies by editor, the parser refuses to guess which block a mixed line belongs to. Convert the file to spaces and the error class disappears."
}
},
{
"@type": "Question",
"name": "How many spaces should I indent — two, four, or eight?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use four spaces per level, never tabs, and keep it uniform across the file. Four spaces is the community standard your formatters and teammates expect — consistency matters more than the number, but four is the default every tool assumes."
}
},
{
"@type": "Question",
"name": "What does pass do and when should I use it?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The `pass` statement is a no-op placeholder that satisfies the 'suite required' rule. Put it under any header whose body isn't written yet. Replace it with real logic later — the file compiles and imports in the meantime."
}
},
{
"@type": "Question",
"name": "How do I find invisible mixed whitespace in my file?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Run `cat -A file.py` and look for `^I` markers (tabs) among space-indented lines, or run `python3 -m tabnanny file.py`, which reports ambiguous indentation directly. Enable your editor's render-whitespace so tabs and trailing spaces stay visible while you type."
}
},
{
"@type": "Question",
"name": "Can mixed tabs and spaces run without raising but do the wrong thing?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes — that failure mode is worse than the error. When mixing is consistent enough to tokenize, Python may attach a statement to the wrong block silently. The code runs, tests may even pass, and behavior is wrong. Spaces-only plus a formatter removes the ambiguity class entirely."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large