How to start ropemacs-mode automatically? - python

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.

Related

Indentation not working properly in emacs for python

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)

Emacs proper indentation for python code after newline ala PyCharm

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.

Emacs 24.3 python: Can't guess python-indent-offset, using defaults 4

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.

emacs and python updating modules

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.

how can I load my .el file only while editing python code

I have collected a list of emacs customizations in a file my-python-setup.el. How can I ensure that emacs will first load python-mode and then load this library only when I'm editing python files?
I've tried putting
(load-library "my-python-setup")
in my .emacs file, but that loads these customizations for all kinds of files.
These customizations are on top of python-mode, and the value of auto-mode-alist currently is ("\\.py\\'" . python-mode).
I'm by no means an Emacs expert, but I think you could just stick add a python-mode-hook function and load your library in there. Something like:
;; define your hook function
(defun python-mode-setup ()
(message "Custom python hook run")
(load-library "my-python-setup"))
;; install your hook so it is called when python-mode is invoked
(add-hook 'python-mode-hook 'python-mode-setup)
Here is my personal python-mode-hook, for example:
(defun python-mode-setup ()
(setq python-indent-offset 4
python-indent 4
;; turn off indentation guessing in various python modes
python-guess-indent nil
python-indent-guess-indent-offset nil
py-smart-indentation nil
;; fix mark-defun in new python.el
python-use-beginning-of-innermost-defun t))
(add-hook 'python-mode-hook 'python-mode-setup)
If you only want your code to be loaded when you edit python code, you may want to consider sticking your lisp code in your own major-mode which extends python mode.
(define-derived-mode 'my-python-mode 'python-mode "MyPy"
"A customized version of python-mode"
... here goes your code ...
)
Then you'll have to configure emacs to load 'my-python-mode instead of python-mode by adjusting auto-mode-alist.

Categories

Resources