Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indentation not working properly in emacs for python

Tags:

python

emacs

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

like image 747
Flame of udun Avatar asked Nov 20 '14 05:11

Flame of udun


People also ask

How do I correct an indentation in Emacs?

Move to the end of the block you want to indent and set the mark by pressing C-SPC . Move the cursor to the first line of the block, and indent this line however far you want all the lines in the block to be indented. Press M-C-\ . The text in the marked block will now be reformatted.

Is Python sensitive to indentation?

Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.


2 Answers

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.

like image 138
Jorgen Schäfer Avatar answered Sep 22 '22 11:09

Jorgen Schäfer


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)))
like image 44
Rahul Avatar answered Sep 20 '22 11:09

Rahul