简体   繁体   中英

Pycharm TabError: inconsistent use of tabs and spaces in indentation

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.

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.

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

Here activate (checkbox) Use tab character AND Smart tabs

Thats works for me.

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM