0

I am doing python programming in notepad++ ,

so as suggested in many posts I have downloaded a tool called "python indent" and set it as enable.

Even after this , when I type in the print statement still I get the indentation errors such as "IndentationError: unindent does not match any outer indentation level" , any other tool is available for this purpose? Because in javascript ,when I select the entire file and go to jshint tool and select indentation, I just get the properly indented file .

I am searching for a tool in python , but not able to get it. Anybody has any suggestions please?

edit:

try:

    print "hi"
    keys = mgt.get_keys(name)

I am getting the error after inserting print statement

user1907849
  • 960
  • 4
  • 19
  • 42
  • 4
    in python indentation belongs to the semantics, so auto-indentation is not possible by design. – Daniel Jun 21 '14 at 14:40
  • 1
    Indentation in Python is like brackets in other programming languages, it cannot be automated. You have to understand the code yourself. – aIKid Jun 21 '14 at 14:44

4 Answers4

4

Sounds like you have mixed tabs and spaces in your block. Use one or the other, but not both.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • In pep8, it suggests that *Spaces are the preferred indentation method*: http://legacy.python.org/dev/peps/pep-0008/#tabs-or-spaces – WKPlus Jun 21 '14 at 15:09
  • 1
    @WKPlus: `space`s are preferred, but `tab`s are still allowed, and mixing them is a really bad idea. :) – Ethan Furman Jun 21 '14 at 15:11
2

There is no way to auto-indent python without understanding the code (as @alKid and @Daniel say in the comments):

For example:

if(x < y):
    # Since there are no ending brackets
    # how will the program
    # know when you want
# to exit the indentation block?
bcorso
  • 45,608
  • 10
  • 63
  • 75
0

This is likely Notepad++ inserting tabs/spaces inconsistent with your existing code. I first suggest making the spaces/tabs visible; Menu item View / Show Symbol, then select "Show White Space and Tab". I suggest using this setting for any whitespace-significant language.

I find the default settings in Notepad++ (I'm using v.6.6.7) often mess up my Python indentation. To correct this: Settings / Preferences / Tab Settings / python; uncheck "use default value" and instead specify "Replace by space."

codingatty
  • 2,026
  • 1
  • 23
  • 32
0

In Sublime Text 2 there is the option to auto indent using white spaces under View > Indentation > Indent Using Whitespaces. There is also the option to convert indentation to whitespaces or convert indentation to tabs under View > Indentation > Convert Indentation to Tabs.

I know this does not address notepad++ but it is possible notepad++ has something similar available and if not this may be helpful to Sublime Text 2 users.

Bodhidarma
  • 519
  • 1
  • 7
  • 25