Python tabnanny Module
Example
Check Python source for ambiguous indentation:
import tabnanny
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write('def hello():\n print("Hello")\n')
fname = f.name
tabnanny.check(fname)
print('Indentation check completed')
Try it Yourself »
Definition and Usage
The tabnanny module detects ambiguous indentation in Python source files.
Use it to check for inconsistent mixing of tabs and spaces, which can cause subtle bugs in Python code.
Members
Member | Description |
---|---|
NannyNag | Exception raised when ambiguous indentation is detected. |
check() | Check a file for ambiguous indentation. |
More Examples
Example
Detect ambiguous indentation in code:
import tabnanny
import tempfile
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write('def greet():\n print("Hello")\n print("World")\n')
fname = f.name
try:
tabnanny.check(fname)
print('No indentation errors found')
except tabnanny.NannyNag as e:
print(f'Indentation error: {e}')
Try it Yourself »