My Emacs Python environment
There are two Python modes:
python-mode.el created by the Python community and
python.el created by the Emacs community (David Love). The first version of
python.el was included in Emacs 22 and an improved version was included in Emacs 23. I am currently using the default
python.el included with Emacs 23. Below are some useful commands. For a complete list, open a Python file and run
M-x apropos RET python RET
- Decrease indentation of the region
- Increase indentation of the region
- Start (or switch) to a Python shell
- Run the current buffer in the Python interpreter
- Run the selected code in the Python interpreter
- Get help on a Python symbol (Better than visiting the slow Python website, right?)
(setq-default indent-tabs-mode nil) ; use only spaces and no tabs (setq default-tab-width 4)
Ropemacs is an Emacs plugin to use Rope, a powerful Python refactoring library. It supports code completion, renaming, and other refactoring. Pymacs is a prerequisite for Ropemacs. Below are some useful commands. For more information, look in the Rope menu.
- Code completion
C-c r r
- Rename a variable, function, etc.
Note: pip install does not work with v0.25. Need to use `make install`
- Install Pymacs (Emacs part)
$ curl -L https://github.com/pinard/Pymacs/tarball/v0.24-beta2 | tar zx $ cd pinard-Pymacs-016b0bc $ make $ mkdir -p ~/.emacs.d/vendor/pymacs-0.24-beta2 $ cp pymacs.el ~/.emacs.d/vendor/pymacs-0.24-beta2/pymacs.el $ emacs -batch -eval '(byte-compile-file "~/.emacs.d/vendor/pymacs-0.24-beta2/pymacs.el")'
- Install Pymacs (Python part)
$ sudo pip install https://github.com/pinard/Pymacs/tarball/v0.24-beta2
- Install Ropemacs and Rope
$ sudo pip install http://bitbucket.org/agr/ropemacs/get/tip.tar.gz
- Edit ~/.emacs to use Ropemacs
(add-to-list 'load-path "~/.emacs.d/vendor/pymacs-0.24-beta2") (require 'pymacs) (pymacs-load "ropemacs" "rope-") (setq ropemacs-enable-autoimport t)
Auto Complete Mode¶
Auto Complete Mode is an extension to make Emacs auto-completion nicer. It creates a drowpdown in the middle of your text. It can use Ropemacs as a backend.
- Download, unpack and byte-compile
$ cd ~/.emacs.d/vendor $ curl http://cx4a.org/pub/auto-complete/auto-complete-1.2.tar.bz2 | tar jx $ cd auto-complete-1.2 $ make byte-compile
- Edit ~/.emacs
(add-to-list 'load-path "~/.emacs.d/vendor/auto-complete-1.2") (require 'auto-complete-config) (add-to-list 'ac-dictionary-directories "~/.emacs.d/vendor/auto-complete-1.2/dict") (ac-config-default)
YASnippet is a template system inspired by Textmate. Demo on YouTube
Code checking option 1: On-the-fly w/ Flymake¶Flymake is part of Emacs 23. PyFlakes. pep8. See also pylint. Reference. Note:
flymake-cursor.elis not required, but it makes flymake less obtrusive.
- Install PyFlakes and pep8
$ sudo pip install pyflakes pep8
flymake-cursor.eland save it as
(add-to-list 'load-path "~/.emacs.d/vendor") (add-hook 'find-file-hook 'flymake-find-file-hook) (when (load "flymake" t) (defun flymake-pyflakes-init () (let* ((temp-file (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace)) (local-file (file-relative-name temp-file (file-name-directory buffer-file-name)))) (list "pycheckers" (list local-file)))) (add-to-list 'flymake-allowed-file-name-masks '("\\.py\\'" flymake-pyflakes-init))) (load-library "flymake-cursor") (global-set-key [f10] 'flymake-goto-prev-error) (global-set-key [f11] 'flymake-goto-next-error)
pycheckers, make it executable, and put it on your PATH
#!/bin/bash pyflakes "$1" pep8 --ignore=E221,E701,E202 --repeat "$1" true
Code checking option 2: Manual checking w/ python-check¶
PyFlakes is a Lint-like tool for Python. It is like PyChecker, but it is fast. PyFlakes can be run from within Emacs using the default python mode in Emacs23.
- Install PyFlakes
$ sudo pip install pyflakes
(setq python-check-command "pyflakes")
- From Emacs, while visiting a Python file, run
$ wget http://ourcomments.org/Emacs/DL/elisp/nxhtml/zip/nxhtml-2.08-100425.zip $ unzip nxhtml-2.08-100425.zip
(load "~/.emacs.d/nxhtml/autostart.el") (setq mumamo-background-colors nil) (add-to-list 'auto-mode-alist '("\\.html$" . django-html-mumamo-mode))
- EnigmaCurry: Emacs as a powerful Python IDE (2008-03-09)
- EnigmaCurry: AutoComplete.el : Python Code Completion in Emacs (2009-01-21)
- Lei Chen: Setup Perfect Python Environment In Emacs (2009-01-30)
- Stack Overflow: Switching from python-mode.el to python.el (2008-12-12)
- Stack Overflow: python emacs completion(2009-04-15)
- Stack Overflow: How can I use Emacs Flymake mode for python with pyflakes and pylint checking code?
- Jesse D. Legg: Emacs for Python Programmers: Basics (2010-03-14)
- Jesse D. Legg: Emacs for Python Programmers #2: Virtualenv, IPython & Daemon Mode (2010-03-14)
- Pymacs installation documentation
- Edward O'Connor: ropemacs (2008-02-27)
- NIPY Documentation: Tricked out emacs for python coding
- Richard Riley: Emacs programming customisations
- Emacs Documentation: Flymake
- amix.dk: Static checking Python code
- Magit in Spacemacs (evil-magit) notes — posted 2018-11-02
- Switching from Emacs to Vim (actually Spacemacs) — posted 2015-12-31
- Colorized, interactive "git blame" in Emacs: vc-annotate — posted 2011-05-28
- Emacs espresso-mode for jQuery — posted 2010-03-10
- Notes on C++ development with Emacs on Ubuntu Linux — posted 2009-07-08
This has been the best written (and up to date using current packages!) page I've seen so far for ropemacs, python, and emacs.
Thanks so much.
Very concise and helpful to take python and emacs to the next level, thank you. Just a note too that Auto Complete mode is now at v1.3 instead of 1.2.
K: Great, I will have to update to Auto Complete 1.3
Works great on my system, except for two things.
Every time I edit a C-file, a warning pops up that flymake is disabled. I'm guessing either load flymake only for python files, or disable warnings.
Both warnings and errors use the flymake-errline face. Since the messages most often contain "W:" and "E:", it should be possible to fix this somewhere.
I'll dig more into this, but I posted in case you already have the answers.
Thanks for the setup :)
- was of course trivial:
(defun anr78/flymake-hook () ; your flymake-stuff here )
(add-hook 'python-mode-hook 'anr78/flymake-hook)
Anders: Thanks for your comments. I will have to look into #2. I would like to fix this also. Let me know if you figure it out.
Using pylint instead of pyflakes, I have the colors working. The pylint-checker looks like:
#!/usr/bin/env python import re import sys from subprocess import * p = Popen("pylint --rcfile=pylint.rc -f parseable -r n --disable=W0613,C,R %s" % sys.argv, shell = True, stdout = PIPE).stdout for line in p.readlines(): match = re.search("\\[([WE])(, (.+?))?\\]", line) if match: kind = match.group(1) func = match.group(3) if kind == "W": msg = "Warning" else: msg = "Error" if func: line = re.sub("\\[([WE])(, (.+?))?\\]", "%s (%s):" % (msg, func), line) else: line = re.sub("\\[([WE])?\\]", "%s:" % msg, line) print line, p.close()
Anders: Thanks for the code.
Finally I did python autocompletion as I wanted!! Thanks a lot!
Anders: Since the option --disable doesn't seem to exist anymore I switched the command line to
p = Popen("pylint --rcfile=pylint.rc -f parseable -r n --disable-msg=W0613 --disable-msg-cat=C,R %s" % sys.argv, shell = True, stdout = PIPE).stdout
Hi, thanks for the code. But one problem is my yasnippet doesn't work...
Thanks a lot. Getting lots of troubles with the other manuals (I'm an Emacs newbie). This guide is concise, easy to follow and works like charm.
Thanks for the tutorial.
i am having the following error when starting emacs:
"error: Pymacs helper did not start within 30 seconds"
It seems i forgot something... Any help would be appreciated.
i follow your steps up to yasnippet.
Post some plugins on : Emacs VS
This was really great. Unfortunately I've now moved to python 3 so ... yuck. Still, any advice on what can be salvaged? Found pymacs and rope for py3k but no ropemode, ropemacs. Autocomplete for py3k apparently here http://www.rwdev.eu/articles/emacspyeng.