Pycharm TabError: inconsistent use of tabs and spaces in indentation
Asked Answered
H

7

10

In Pycharm I keep running into this error:

TabError: inconsistent use of tabs and spaces in indentation

I know its a problem with tabs/spaces.

I want:

if len(myresults)==0:
    print("TEST")

Whenever I type, pressing enter after every line typed I actually type:

if len(myresults)==0:
        print("TEST")

Causing this error. How do I fix it? Here are my setting s for pycharm:

Pycharm Settings

I'm probably missing something obvious, but I simply cannot find it.

Hardcastle answered 12/11, 2018 at 14:17 Comment(1)
Have you tried to untick the Use tab character box? Or if you want to keep it ticked, try to untick the Plugin Settings -> Plugins -> EditorConfig (see intellij-support.jetbrains.com/hc/en-us/community/posts/… which works similarly for Pycharm)Ryder
L
6

Try,

  1. In the "Settings | Editor | Code Style" -- try disabling "Detect and use existing file indents for editing" in case if you have it enabled (it is by default). NOTE: re-opening file in editor may be required.
  2. Do you have any .editorconfig files anywhere in the path of that file? Settings from .editorconfig have priority (will overwrite) over your IDE settings.
Liew answered 12/11, 2018 at 14:46 Comment(3)
No dice unfortunatlyHardcastle
There must've something overriding the tabs and indent settings. Try setting the scheme to project. or restore the code style to defaults.(These are very very long shots)Liew
I fixed it by reformatting the entire file... My hands hurt!Hardcastle
K
3

What worked for me:

  • "Settings | Editor | Code Style" -- disable "Detect and use existing file indents for editing" (as @Nipun Sampath suggestted)

Together with:

  • "Settings | Editor | Code Style | Python" -- disable "Use tab character"

For reformatting the file: Ctr+Alt+L -> 'Code cleanup' on either selected text or whole file (depending on what is needed)

Knapweed answered 11/9, 2020 at 8:3 Comment(0)
L
3

Go to Settings --> Editor --> Code Style --> Python --> Tabs and Idents.

Here activate (checkbox) Use tab character AND Smart tabs

Thats works for me.

Lempira answered 25/11, 2020 at 14:50 Comment(0)
W
1

I'm using Pycharm and Jupyter Notebook and had the same problem with both of them. I could not fix it with "convert Indents", So I uninstalled some of the modules that I was using in my programm and reinstall them and worked for me.

Weevily answered 17/7, 2020 at 17:15 Comment(0)
F
0

I'm using Pycharm 2019.1. For me, this error constantly appeared always I pressed Enter to write a new line, and I have to manually rewrite every indentation in order to disappear the red sub-lines that indicate error. I fixed it by analyzing the full code into another text editor (kate editor in my case, but you can use another one). I verified there's some indentations written as [TAB] and the most of them written as four simple spaces. So I replaced all indentations written as [TAB] by indentations written as four spaces (most of the editors replace by using [Ctrl R] shortcut) and... voilà. Everything works fine. Note: I couldn't do the replace into Pycharm editor itself. Apparently pycharm editor does not differentiate a [Tab] of four spaces when you try replace by [Ctrl R]. Hope it help future users.

Fushih answered 16/8, 2019 at 16:31 Comment(0)
A
0

As I also couldn't find it anywhere else, if we are talking about functions and methods declaration, this check-boxes must be unchecked!

Blue check-box

Araminta answered 12/7 at 10:40 Comment(0)
D
-1

I faced the same problem in Pycharm and The way I solved it was by: Replacing all spaces by tabs using the tab key on the key board!

Discommode answered 9/3, 2023 at 13:32 Comment(2)
Hi Immen, welcome to SO. I think your comment does not solve the problem in general. But that's what the questioner asked for.Finnougrian
Answer does not solve the problem in generalFinnougrian

© 2022 - 2024 — McMap. All rights reserved.