Google Colab code change always produces "TabError: Inconsistent use of tab and spaces in indentation"

Viewed 1637

When I make even a simple change in a file checked out in /root/model/ in Google Colab environment Intro, I get

"TabError: Inconsistent use of tab and spaces in indentation"

I've set the editor in Colab settings to show 4 spaces for a Tab, and to show line-numbers (the Colab editor doesn't have many settings to change), and the line that I added looks perfectly indented. I can't tell why/how it's not indented.

Does anyone come across this issue?

How do you change files (in code that's checkout out in /content/ or /root/ from a model-repo) as the Colab-editor gives error on very basic of changes?

5 Answers

I came across this issue. I have the feeling that something goes wrong from time to time when copying and pasting code lines, but I do not have a precise diagnosis.

Changing the tabs into spaces solved the issue for me.

SOLVE: I removed the space before and after the line with the problem and solved

This is a bug in colab where it will repopulate an indentation done from the keyboard shortcuts with the wrong character. It starts if you paste tab-delimited code into a Jupyter Notebook and then insert a space-delimited line from the editor. The code has to be all tabs or all spaces in a cell, but the editor inserts spaces in exist lines with leading tabs. Solution is to reformat any inserted code. If you use darkmode in colab, the leading characters can be hard to see.

SOLVED : I faced this issue when using google colaboratory the solution for me was to copy my code from google colab and paste it in sublime text 3 in .py file and in sublime go to View -> Indentation and click Convert Indentation to Tabs and recopy paste your code to google colab is should work

It just boils down to the use of tabs or spaces if you are copying and pasting e.g. from editors, IDEs, notepads, githubs, or anywhere. Just make sure you have either tabs or spaces in your script on colab just by selecting the script. You will see arrows or dots. After spending a lot of time I got to know 1 arrow == 8 spaces, I don't know about others. JUST MAKE SURE ONLY TABS OR SPACES in your scripts for consistency. Colab seems to fail to connect outside code while copying, may be.

Related