Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'see' / highlight tabs and spaces in PyCharm for checking indentation?

this may sound dumb, but I am still fairly new, is there clear way to spot if I have tabs or spaces in the wrong place with PyCharm? Or even to display all tabs and spaces visually?

I just spent ages looking for problem with 'invalid syntax' error at Def line of function, I had thought it might still be some wrong indent, which I did have before, so checked this painstakingly and found nothing. In the end, was missing ) from end of function before this. I realised the coloured lines on right hand side show errors and got this one from there.

Also I understand you can mix tabs and 4 space character indents in PyCharm with no problem?

But if you use tab one line and 4 spaces the next with, for example, simple text editor, Python will say 'no I'm not going to run this because I'm a strict pedant and this is just too naughty'? Any other common sense best practice habits in this area?

like image 720
Will Croxford Avatar asked Jul 14 '17 22:07

Will Croxford


2 Answers

How to 'see' / highlight tabs and spaces in PyCharm for checking indentation?

You can tell Pycharm to visually show tabs and spaces by going to View -> Active Editor -> Show Whitespaces:

enter image description here

Also I understand you can mix tabs and 4 space character indents in PyCharm with no problem?

Yes you can. But you shouldn't. Even if you choose to however, PyCharm will convert them all to spaces by default. You can change this behavior by going to Settings -> Editor -> Code Style -> Python and then selecting the Use tab character check box:

enter image description here

like image 147
Christian Dean Avatar answered Oct 19 '22 11:10

Christian Dean


There's a setting to make white space visible in:
Settings -> Editor -> General -> Appearance -> Show whitespaces

like image 28
Joao Delgado Avatar answered Oct 19 '22 10:10

Joao Delgado