“inconsistent use of tabs and spaces in indentation”

后端 未结 28 2230
北恋
北恋 2020-11-22 00:56

I\'m trying to create an application in Python 3.2 and I use tabs all the time for indentation, but even the editor changes some of them into spaces and then print out \"inc

相关标签:
28条回答
  • 2020-11-22 01:29

    If you are using Sublime Text for Python development, you can avoid the error by using the package Anaconda. After installing Anaconda, open your file in Sublime Text, right click on the open spaces → choose Anaconda → click on autoformat. Done. Or press Ctrl + Alt + R.

    0 讨论(0)
  • 2020-11-22 01:30

    Sometimes, tab does mess up while indenting. One way is to obviously use the tab and backspace to correctly indent the code.

    Another way is to use space 4 times (depending on how much you want to indent).

    A weird way that worked for me when nothing else worked, whichever line I getting the error, I backspaced that line to the previous line and then pressed enter. It automatically indented the line to correct position and I was not getting any error after that.

    Hopefully, this should help.

    0 讨论(0)
  • 2020-11-22 01:30

    Use pylint it will give you a detailed report about how many spaces you need and where.

    0 讨论(0)
  • 2020-11-22 01:32

    I had the same error. I had to add several code lines to an existing *.py file. In Notepad++ it did not work.

    After adding the code lines and saving, I got the same error. When I opened the same file in PyCharm and added the lines, the error disappeared.

    0 讨论(0)
  • 2020-11-22 01:32

    I got the same errors but could not figure out what I was doing wrong.

    So I fixed it by running auto-indent on my code and allowing the machine to fix my fault.

    If anyone is wondering how I did that. Simple. Go in vim. Type in G=gg.

    This will automatically fix everything. Good luck :)

    0 讨论(0)
  • 2020-11-22 01:33

    Don't use tabs.

    1. Set your editor to use 4 spaces for indentation.
    2. Make a search and replace to replace all tabs with 4 spaces.
    3. Make sure your editor is set to display tabs as 8 spaces.

    Note: The reason for 8 spaces for tabs is so that you immediately notice when tabs have been inserted unintentionally - such as when copying and pasting from example code that uses tabs instead of spaces.

    0 讨论(0)
提交回复
热议问题