“inconsistent use of tabs and spaces in indentation”

后端 未结 28 2231
北恋
北恋 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:20

    Sublime Text 3

    In Sublime Text, WHILE editing a Python file:

    Sublime Text menu > Preferences > Settings - Syntax Specific :

    Python.sublime-settings

    {
        "tab_size": 4,
        "translate_tabs_to_spaces": true
    }
    
    0 讨论(0)
  • 2020-11-22 01:20

    I oddly ran into a similar issue with one of my .py files. I simply opened the file in Pycharm and pressed Option+Command+L which correctly formats the file contents in one go.

    I suspect I was having trouble because I coded this particular .py file through jupyter labs as opposed to my usual choice of sublime text or Pycharm and therefore ran into some hidden indentation issues many answers here have alluded to

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

    Highligt the indented bits, and then click tabs. They will then become consistent :)

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

    Using the autopep8 command below fixed it for me:

     autopep8 -i my_file.py
    

    Documentation for autopep8 linked here.

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

    I use Notepad++ and got this error.

    In Notepad++ you will see that both the tab and the four spaces are the same, but when you copy your code to Python IDLE you would see the difference and the line with a tab would have more space before it than the others.

    To solve the problem, I just deleted the tab before the line then added four spaces.

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

    Well I had the same problem and I realised that the problem is that I copied code from another python editor to sublime.

    I was working with jupyter notebook and then I copied the code into sublime. Apparently when you make specific modifications (like moving code in functions) then indentation gets messy and this is where the problem comes from.

    So just stick to one editor. If you do so, then you will be having no problem.

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