Search code examples
pythonindentation

Python unindent error only in python REPL even with correct # of spaces (and no tabs)


In the following code block

def classify0(inx, ds, ls, k):
  sdindices = dist.argsort()
  clcount = {}
  for i in range(k):
    vlab = ls[sdindices[i]]
    clcount[vlab] = clcount.get(vlab,0)+1
  sclcount = sorted(clcount.iteritems(), key=operator.itemgetter(1), reverse=True)
  # Previous lines gives: IndentationError: unindent does not match any outer indentation level
  return sclcount[0][0]

As shown in the comment, the line starting with

sclcount = "

gives the indention error. Now I have checked all the 'usual suspects': there are no tabs and the correct # of spaces.

Also, i am using PyCharm (/intellij) and no warnings/syntax errors . Only in the python shell..


Solution

  • Looks like %cpaste (or %paste) in ipython fixes the issue.