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

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?

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:
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:

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

Related

How to wrap lines automatically in Python Spyder?

I'm new to Python.
I'm using Spyder 4.0.1, Python 3.7.
The Spyder built-in PEP-8 convention code style check returns error E501 (line too long). Although I set the vertical line and wrap lines, it still shows error for the long characters line. Pycharm would wrap lines automatically, but I cannot find any effective solution in Spyder.
What should I do? Do I have to press Enter manually for the long line? Thanks.
In Windows Go To
Tools->Preferences->Editor and select Wrap lines.
If you use mac, you need to go to Python on the left right corner then click on Preferences.
Then go to Editor, and check Wrap lines.
To turn on the Word Wrap feature, go to View > Toggle Word Wrap or click the Edit Display icon in the Code Editor toolbar (it's the second-to-last one on the right) and select Toggle Word Wrap. You can also set the program preferences to keep this feature on by default.

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

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?
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.

indentation issue python in sign detection script

I have a script with I try to detect STOP sign in the traffic, but I have a problem with indentation, and I don't know how to solve it...
Image with the code:
And error message:
You can clearly see (even if code-in-image is a no-no) line 24 has spaces, line 25 has tabs.
Make them all spaces.
You can't mix tab and space for indentation. Make all of them either space or tab.

Show indents for Spyder IDE

Is there a way to visualize tab indents in the Spyder?
I know I can visualize blank spaces under the 'Source' tab. For tabs, this now shows 4 dots.
It would be great if there was a way to show this as a single tab rather than 4 spaces.
I'm pretty sure there are actually 4 spaces there. As per PEP8, Python code should use 4 spaces for indentation, not a tab character. If you haven't changed your settings* in Spyder, it will insert 4 spaces when you press the tab key.
Spyder 4 will allow you to mark indentation levels with vertical lines. It will hopefully be released in 2019. Or you can already download the first beta.
*The characters used for indentation could be changed by going to Tools/Preferences/Editor/Indentation characters. It should always be 4 spaces though.

PyDev adding tabs to block indentation

I am using Eclipse Ingigo with Pydev 2.3.0.2011121518, it has been working great, but has suddenly starting doing this weird thing with tabs.
I have got the "Replace tabs with spaces when typing" option set for PyDev --> Editor in Eclipse preferences, and that works fine, when I press tab, I get 4 spaces. However for some rerason, now when I create a block, such as an "if" block pydev automatically indents the next line, as it should but this time it adds a tab, leading to "Mixed Indentation: Tab found" warnings.
Has anyone seen this behavior before, and know a fix, or is it a bug with Pydev on Eclipse Indigo?
I have just solved this - I had the option ."Assume tab spacing when file contain tabs" checked in Eclipse Preferences --> PyDev --> Editor. When I unchecked this option the automatic indentation worked properly and it added spaces and not tabs. It seems that in one of my docstrings I had some tabs (possibly copied in from another editor), and that must have been making Eclipse assume tab spacing for automatic indentation (even though it was still replacing tabs with spaces as I typed them)
On mars2 and PyDev 5.1.1 this still occurs to me. I have Replace tabs with spaces when typing checked and Assume tab spacing when file contain tabs unchecked. I had to replace all tabs with spaces to remove this warning.
I've faced similar issue, for me the error was Mixed Indentation: Spaces found.
Solution: In Eclipse with Pydev, Source menu -> Convert tabs to space-tabs.

Categories

Resources