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
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.
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)))
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)
Related
I'm running both emacs 23 and 24 with "--no-init-file" to avoid loading my customizations. But then I explicitly load my ancient version of python-mode.el (version 3.105, last copyright notice 1998), and see the same behavior I'm about to describe, so I believe it's core emacs, not python.
Consider this code (the * marks the cursor location):
*def emacs_is_fun():
for x in range(3):
print(x)
Put the cursor at column 4 at the start of "def".
With emacs 23, running mark-sexp selects "def".
With emacs 24, mark-sexp selects the entire code block, to the end
of the print statement.
This isn't bad. The main problem is this common usage:
*very_long_var_name[very_long_expn] += long_function_call(long_arg_list, blah)
Again, the cursor is at the start of the printable part of the line, col 4.
In emacs 23, mark-sexp selects very_long_var_name.
In emacs 24, mark-sexp selects the full line, from col 4 to the end.
I've been using Ctrl-alt-space alt-w to quickly save variable names.
(Or alt-2 ctrl-alt-space alt-w to save a ref expression.) Now it's
gone. It's emacs, so this must be customizable, but I haven't found
where the new behaviour is implemented (and remember I'm using a python-mode
that is about 15 years old). What do I need to put in my .emacs file?
#Eric Interesting. Thanks for the context in the comment of my other answer.
I think you may just want this then:
(add-hook 'python-mode-hook (lambda () (setq forward-sexp-function nil)))
From comments inside python-mode built-in emacs 24 (which I think you may be getting instead)
At last but not least the specialized python-nav-forward-sexp allows easy navigation between code blocks. If you prefer cc-mode-like forward-sexp movement, setting forward-sexp-function to nil is enough...
This gels closer to what I think of a sexp as, like mentioned in my other answer, but outside of lisp a sexp seems at least somewhat ambiguous.
Seems like this was a bug fix in emacs 24. As that is technically now marking the full sexp. It seems like you want the functionality of marking a symbol instead.
I think by putting this in your .emacs you'll get the functionality you want.
(defun your-mark-symbol (&optional arg allow-extend)
"mark-sexp in lisp.el but with fowrad-symbol instead of foward-sexp"
(interactive "P\np")
(cond ((and allow-extend
(or (and (eq last-command this-command) (mark t))
(and transient-mark-mode mark-active)))
(setq arg (if arg (prefix-numeric-value arg)
(if (< (mark) (point)) -1 1)))
(set-mark
(save-excursion
(goto-char (mark))
(forward-symbol arg)
(point))))
(t
(push-mark
(save-excursion
(forward-symbol (prefix-numeric-value arg))
(point))
nil t))))
(global-set-key (kbd "C-M-SPC") 'your-mark-symbol)
Note that is literally a copy of mark-sexp but with foward-symbol instead of forward-sexp.
This could very well cause unexpected weirdness if you're using mark-sexp via C-M-SPC in other buffers/modes, in which case you would want to do make this keybinding via mode hook instead.
PyCharm will ident the line after a == b correctly after I press enter:
if a == b:
print "something"
but when in emacs, it will give me:
if a == b:
print something
I already have python-mode enabled together with ergoemacs. Can someone who has done this before share how to to it ala PyCharm ?
Update:
*alt-return seems to be doing what I want*
C-j does newline-and-indent, which is what you are looking for probably.
With python-mode.el, RET is bound to py-newline-and-indent by default.
I'm guessing you're running the "wrong" python mode.
Your distribution most likely ships python-mode.el, which I - personally - find to be a bit clumsy.
Instead, try python.el. Just download the file, put it into ~/.emacs.d/ and add
(load "~/.emacs.d/python.el")
to your ~/.emacs file.
While you're at it, here are some good settings for whitespace and maximum line length:
(add-hook 'python-mode-hook '(lambda ()
(setq whitespace-line-column 79)
(setq whitespace-style '(face empty tabs lines-tail trailing))))
Press 'tab' key anywhere on the print something line - it should indent properly. This works for most modes.
EDIT
In other modes which do not have strict indenting rules like python you can highlight everything and Ctrl+Meta+\ will try to format everything.
Can anyone who understands Lisp please help resolve this warning?
I upgraded to Emacs 24.3 and whenever I create a Python file using Emacs I get this warning message. Searched in python.el and found the following section of code that produces the warning:
(let ((indentation (when block-end
(goto-char block-end)
(python-util-forward-comment)
(current-indentation))))
(if indentation
(set (make-local-variable 'python-indent-offset) indentation)
(message "Can't guess python-indent-offset, using defaults: %s"
python-indent-offset)))
And here is my .emacs setup:
(setq-default c-basic-offset 4
tab-width 4
indent-tabs-mode nil)
(add-hook 'c-mode-common-hook
(lambda ()
(c-set-offset 'arglist-intro '+)
(c-set-offset 'arglist-close 0)))
(add-hook 'python-mode-hook
(lambda ()
(c-set-offset 'arglist-intro '+)
(c-set-offset 'arglist-close 0)))
When you open a python file, emacs guesses the indentation offset (number of spaces to indent) based on that file style. When you create a file (the case you describe), emacs cannot guess (file is empty) so it uses your default (4) and notifies the user.
In other words: it is a harmless warning; if you find this is a bug please report it as such.
If you don't like emacs guessing the offset, customize the variable python-indent-guess-indent-offset to nil, and then emacs will use always your default (very unsafe in python, where indentation has meaning and you could be editing a file created by somebody else with other defaults).
If all you want is to silence the warnings, while letting emacs still guess the offset as juanleon's answer explains, you can switch the python-indent-guess-indent-offset-verbose variable off.
(setq python-indent-guess-indent-offset t)
(setq python-indent-guess-indent-offset-verbose nil)
Per this comprehensive answer on the emacs SE.
Look in python.el, not python.elc. If you do not have python.el, then google for it (at least to look at it -- you do not need it to use Emacs). *.elc is a byte-compiled file, which is pretty much incomprehensible to humans.
The source code, in python.el, will tell you just what python-indent-guess-indent-offset does, and hence why you see the result you see.
In addition to the previous answers, and silencing the warning, this procedure deals with the problem and gives two examples for why the warning is useful.
Get a list of currently open buffers with C-x C-b. Filter those with a Python
extension, add quotes around them, and replace them in this command:
(dolist (f '(
"/path/to/file1"
"/path/to/file2"
))
(message (concat "Opening file: " f))
(find-file (expand-file-name f)))
Execute the command (select the region, then M-x evaluate-region). Then *Messages* looks like this:
Opening file: /path/to/file1
Opening file: /path/to/file2
Can’t guess python-indent-offset, using defaults: 4
For information about GNU Emacs and the GNU system, type C-h C-a.
I had files I had used for testing and that I had forgotten, for example:
from time import sleep
sleep(10000)
I only realized it thanks to this message and I deleted the files.
In another case, I had a useful file with just global variables, so I added this
line:
# -*- mode: python; python-indent-offset: 4 -*-
SOME_GLOBAL_VAR = 0
Although this does set the buffer-local variable (check by changing to 6, then
checking the value with C-h v python-indent-offset), it only does so after the
warning message, not before. I decided to tolerate this message.
When I open a python file I have to manually start ropemacs-mode to get the key bindings each time. I tried adding something like this to automatically start ropemacs:
(add-hook 'python-mode-hook (lambda ()
(ropemacs-mode)
))
But it seems to break flymake. What am I doing wrong here?
Dont use ropemacs but the readme says like this
After installing pymacs, add these lines to your ~/.emacs file::
(require 'pymacs)
(pymacs-load "ropemacs" "rope-")
My problem was with some code for using autocomplete.el along with ropemacs and yas which I found here:
http://hide1713.wordpress.com/2009/01/30/setup-perfect-python-environment-in-emacs/
Once I commented out the block relating to autocomplete everything else started working as expected.
atm i'm using emacs to write some python code, so far it works quite fine except one problem that is really a bit annoying.
Always when I update something inside a self written module i reevaluate the buffer and the module in the python shell inside emacs doesn't get updated. i always have to end the python process and start it again to get the change. I figured out that emacs copies some things to a tmp dir to execute them, so i guess it has something to do with this.
Maybe someone out there had the same problem and solved it already so help would be appreciated
You have to reload the module manually in the shell in order for it to take effect.
See the documentation here on the Python reload function
I asked a similar question which you can see here
I found a better solution that needs no emacs config:
simply do
$ ipython profile create
that should create ipython profile in
$HOME/.ipython/profile_default/ipython_config.py
then put the following inside
c = get_config()
c.TerminalInteractiveShell.editor = 'emacsclient'
c.InteractiveShellApp.extensions = [
'autoreload'
]
c.InteractiveShellApp.exec_lines = []
c.InteractiveShellApp.exec_lines.append('%load_ext autoreload')
c.InteractiveShellApp.exec_lines.append('%autoreload 2')
then restart emacs. Now each time you save changes to file inside emacs - ipython would reload it automatically
and the following I have in my emacs config
;; ------------------
;; misc python config
;; ------------------
(company-mode -1)
(elpy-enable)
(elpy-use-ipython "ipython")
(setq python-shell-interpreter "ipython" python-shell-interpreter-args "--simple-prompt --pprint")
(setq python-check-command "flake8")
(setq elpy-rpc-backend "jedi")
(setq elpy-rpc-python-command "python")
; https://github.com/gregsexton/ob-ipython/issues/28
(setq python-shell-completion-native-enable nil)
if you want to see my full python config, it's here
You can advise a python-mode.el function to get the desired effect (at least, if I am understanding your request properly). Put the following in your Emacs init file:
(defun py-reload-file (buf)
"Reload buffer's file in Python interpreter."
(let ((file (buffer-file-name buf)))
(if file
(progn
;; Maybe save some buffers
(save-some-buffers (not py-ask-about-save) nil)
(let ((reload-cmd
(if (string-match "\\.py$" file)
(let ((f (file-name-sans-extension
(file-name-nondirectory file))))
(format "if globals().has_key('%s'):\n reload(%s)\nelse:\n import %s\n"
f f f))
(format "execfile(r'%s')\n" file))))
(save-excursion
(set-buffer (get-buffer-create
(generate-new-buffer-name " *Python Command*")))
(insert reload-cmd)
(py-execute-base (point-min) (point-max))))))))
(defadvice py-execute-region
(around reload-in-shell activate)
"After execution, reload in Python interpreter."
(save-window-excursion
(let ((buf (current-buffer)))
ad-do-it
(py-reload-file buf))))
Now, when you're in a python program, you can select a region of code, press C-| to evaluate the region and the python program will be reloaded (or imported if it wasn't previously loaded) in the Python interpreter buffer. The ENTIRE module will be reloaded, not just the region that was selected and you will be prompted to save the python file if it hasn't already been saved. Note that the caveats that were mentioned in the replies to your previous question still apply (e.g. - If you have class instances already created from the imported module, have instantiated other objects, etc, they won't be reloaded). General breakage may occur, so caveat emptor!).
I'd recommend using Elpy as discussed here.
Add the following to your Emacs configuration file:
(defun my-restart-python-console ()
"Restart python console before evaluate buffer or region to avoid various uncanny conflicts, like not reloding modules even when they are changed"
(interactive)
(if (get-buffer "*Python*")
(let ((kill-buffer-query-functions nil)) (kill-buffer "*Python*")))
(elpy-shell-send-region-or-buffer))
(global-set-key (kbd "C-c C-x C-c") 'my-restart-python-console)
restart your Emacs run your code using C-c C-x C-c
In short, this code has the "if clause" for checking if Python buffer is open. This will help to be able to run C-c C-x C-c at any time of development even when there is no Python process already open. Another part is kill-buffer-query-functions which neglects the prompt for killing the Python buffer.