53

I'm coding in Python and I really like Notepad++. However, off late when I use tab to indent, it seems fine in Notepad++, but when I run the program I get an indentation error, and when I check my code in Emacs or something, I find that Notepad++ actually adds more tab spaces than it shows on screen. What is happening?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
iman453
  • 9,105
  • 16
  • 54
  • 70

6 Answers6

108

There is no universal tab size, so I always make sure to replace tabs by spaces (so you know what you see is what you get everywhere else as well)

Go to Settings -> "Preferences..." -> Language Menu/Tab Settings and check 'Replace by space'

aero
  • 1,654
  • 1
  • 21
  • 31
ikottman
  • 1,996
  • 3
  • 21
  • 23
  • 2
    oh yeah, I forgot to mention that too. One too many times I've got bitten by using the tab. :) – Ashley Grenon Jul 29 '10 at 20:13
  • 1
    Oh, sweet..didn't know that was possible. Thanks! – iman453 Jul 29 '10 at 20:14
  • 2
    +1 This is exactly what I do before I spend serious development time on any IDE. – danyim Jul 29 '10 at 21:15
  • 9
    You can make this change for Python only if you select Go to Preferences -> Language Menu/Tab Settings -> – James Apr 12 '13 at 14:12
  • Tab is usually 4 spaces by convention. I guess this is intuitive. But in real world Notepad++ has its own way which pythons seems not to understand. Thanks for the fix. This really helped me. – Doogle Jan 30 '17 at 09:41
  • Any different from selecting python under Tab Settings in that same window? – Hack-R Jun 14 '17 at 13:49
  • I can't understand with Python is so picky between a tab or 4 spaces... why don't they treat them the same way? – Thomas Aug 30 '17 at 15:44
45

I would suggest going to View > Show Symbol > Show Whitespace and Tab to get an better idea of how your indentations look.

Ashley Grenon
  • 9,305
  • 4
  • 41
  • 54
8

PEP 8 tells us to use spaces instead of tabs in Python for cross-editor compatibility and consistency:

http://www.python.org/dev/peps/pep-0008/

Have a look at this answer for how to change tabs to spaces in Notepad++

Convert tabs to spaces in Notepad++

Perhaps that will fix your problem

Community
  • 1
  • 1
Nobody
  • 4,731
  • 7
  • 36
  • 65
2

Tiny update - to get spaces as tabs, you now go to Settings>>Preferences>>Tab Settings and check the "Replace by space" box

2

I am new to python and started using Notepad++. But I faced the same issue as you... Indentation problems. On my senior's advice, I switched to PyCharm community edition. I pasted the code from Notepad++ to PyCharm and it highlighted the block with indentation problems... The issue was that, some of the lines used spaces and some used tabs. This happened because the code on which I was working was taken from the internet.

Checking for such inconsistencies would solve the problem. Or, there is a better alternative... Switch to PyCharm. It is exclusively built for python coding.. Hope this helps people like me searching for solutions for indentation issues in Notepad++

Vikas
  • 1,900
  • 1
  • 19
  • 20
2

Use Python Indent Plugin for Notepad++: I have used both Pycharm & Notepad++, and frankly - even though both are installed on my machine right now I prefer using Notepad++. So, if you want to continue using Notepad++ for Python development you should definitely install the 'Python Indent' Plugin. This plugin will automatically create tabs for you when writing Python code. The only downside to the plugin is you have to remember to Enable it (by going to Plugins --> Python Indent and then clicking 'Enable') when you want to use it.

To install the Python Indent plugin in Notepad++ just go to 'Plugins' --> 'Plugin Manager' and then click on 'Show Plugin Manager'. Then check off 'Python Indent' and click on the 'Install' button.

Additionally, you should Follow Python's Usage Guide: Tab spacing can differ across programs and, following the recommendations of PEP8 (Python Enhancement Proposals Number 8 - which is Python's Accepted Styling and Usage Guide) you should use the space bar to make your indents. Check out the PEP8 Page about spacing here: https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces

Good luck!

Jaxian
  • 1,146
  • 7
  • 14
  • I have installed Python Indent but it uses 4 spaces as indentation. Is there a way to change it to 2 spaces only? I try Setting --> Preferences --> Language --> Tab size: 2, but it does not work. – dudung Mar 15 '23 at 04:39