13
votes

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?

2
@Sriram No, that's not a good duplicate target. Will is asking how to highlight whitespace, not how to convert tabs to spaces.Christian Dean
@ChristianDean , to view is to finally change/ reformat them properly right?Sriram Sitharaman
Common sense best practice: don't use tabs, and don't complain about using good coding practicesnanotek
Thanks @nanotek, won't use tabs then.Will Croxford

2 Answers

20
votes

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

8
votes

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