Indentation not working properly in emacs for python

11,126

Solution 1

Check the value of python-indent-offset. If it is 0, change it M-x set-variable RET python-indent-offset RET 4 RET.

Emacs tries to guess the offset used in a Python file when opening it. It might get confused and set that variable to 0 for some badly-formatted Python file. If this is indeed the problem, please do file a bug report using M-x report-emacs-bug and the text of the Python file so that the auto-detection can be fixed.

Solution 2

It is related to this bug

http://debbugs.gnu.org/cgi/bugreport.cgi?bug=15975

The quickest workaround which I found was too add Jorgens Answer into .emacs file, add the following at the end of your .emacs file

(add-hook 'python-mode-hook
  (lambda () (setq python-indent-offset 4)))

Solution 3

Can you comment the lines related to auto-complete in your init.el?

; (add-to-list 'load-path "~/.emacs.d/auto-complete-1.3.1")
; (require 'auto-complete)
; (add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")
; (require 'auto-complete-config)
; (ac-config-default)
; (global-auto-complete-mode t)
Share:
11,126
Flame of udun
Author by

Flame of udun

Always learning :)

Updated on June 03, 2022

Comments

  • Flame of udun
    Flame of udun about 2 years

    I am using emacs-for-python provided by gabrielelanaro at this link.

    Indentation doesn't seem to be working for me at all.

    It doesn't happen automatically when I create a class, function or any other block of code that requires automatic indentation(if, for etc.) and press enter or Ctrl + j. Instead emacs says "Arithmetic Error".

    It doesn't happen when I press Tab anywhere in a .py file. Again, every Tab press causes "Arithmetic error".

    Also, when I manually indent code using spaces, I can't erase those spaces! Backspace-ing these indents also causes "Arithmetic Error".

    This problem surfaces when I use the regular Python AC mode as well.

    emacs : GNU Emacs 24.3.1 (x86_64-pc-linux-gnu, GTK+ Version 3.10.7) of 2014-03-07 on lamiak, modified by Debian

  • Flame of udun
    Flame of udun over 9 years
    Done.Saved and evaluated buffer using Alt X.Restarted emacs and opened the .py file but the problem persists
  • vikramls
    vikramls over 9 years
    Can you try disabling the epy-init.el line as well and see if TAB works in the python-mode that is bundled with emacs?
  • vikramls
    vikramls over 9 years
    Invoke emacs like this: emacs -q. Then open your py file and try the TAB indent. Actually open your .py file, type M-x python-mode and then try the TAB indent.
  • craigster0
    craigster0 about 6 years
    Fixed it for me -- it was 0! The first few lines were indented incorrectly (no indentation) and apparently that throws the python package off.