Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script in Colab keeps throwing error message: IndentationError: unindent does not match any outer indentation level

I'm currently using Google Colab in order to take advantage of its free GPU. I was trying to modify a code that I copy and pasted from machinelearningmaster.com. However, whenever I try to add a new code line, for example "print("some words"), I get an indention error.

I have tried adding tabs or spaces before the print call but I still get the error. for example: space,space,print("some words") tab, tab ,print("some words")

I have also checked the colab editor settings, currently the indention width setting are set to two spaces.

The first three lines are part of the original code, the print statement is my addition. I copy and pasted this directly from the colab editor. In Colab all four lines are aligned. As you can see here only the first three lines are aligned. I don't know what's going on.

                img_path = images_dir + filename
                ann_path = annotations_dir + image_id + '.xml'
                count=count+1
                            print("this is count: ", count)

I expected this to print the value of count, instead I get an error message telling me: IndentationError: unindent does not match any outer indentation level

like image 312
Rice Man Avatar asked Mar 04 '23 06:03

Rice Man


2 Answers

Okay, after much searching and frustration, I have an idea of what went wrong, but even better, a solution to fix it.

It appears that the Google Collaborator (Colab) editor does not have a way to set it for tabs "\t" versus space (space-bar entries). From the settings tab on the cell you can set the width of the tab from 2 to 4, but these will be interpreted as 2 to 4 space-bar entries. Usually, this isn't a problem. However, if you're like me and you want to test out code from the web, or be lazy and just copy paste from your editor, problems can arise.

Here's how I fixed it. Before pasting the copied code into Colab, first put it into notepad++. Go to View> Show Symbols >Show All Characters, click on this, you should now be able so see all the characters in the code. Find a tab, it will look like an arrow pointing to the right -->, right click and copy it. Open Search> Find, open the Replace tab. Depending on your version of notepad++ the tab you copied will automatically be entered and the replace will already be set to four spaces. Hit "Replace all". This will automatically replace all tabs with equivalent spaces. Copy the code from notepad++ back to Colab. Now there will be no more conflicts.

like image 98
Rice Man Avatar answered Apr 25 '23 09:04

Rice Man


I think using a simple find and replace tool will just work fine. I also came across this error recently in Colab and I went through @Rice Man solution. The only difference was I used Libre office writer instead of Notepad++. I also found this tool to be helpful. I am not proficient in using Colab but this solution worked for me.

like image 30
KIttu Avatar answered Apr 25 '23 07:04

KIttu