Python and Emacs

Good info:

http://www.emacswiki.org/emacs/PythonProgrammingInEmacs

http://milkbox.net/note/installing-pymacs-rope-on-emacs-24/

http://pedrokroger.net/2010/07/configuring-emacs-as-a-python-ide-2/

http://www.enigmacurry.com/2008/05/09/emacs-as-a-powerful-python-ide/

http://www.enigmacurry.com/2009/01/21/autocompleteel-python-code-completion-in-emacs/

Look at pymacs too: http://pymacs.progiciels-bpi.ca/

There are lots of "Emacs as Python IDE" articles. This one is mine, and it's geared towards Emacs 24.

Try to re-create what's possible in Eclipse. Mostly I use the following features in Eclipse:

Note that "generation of getters and setters" isn't really useful in Python. I'm not sure how feasible "auto fix of errors" is in emacs.

In a nutshell, use Rope. It's a python refactoring library. Very useful blog entry here: http://www.enigmacurry.com/2008/05/09/emacs-as-a-powerful-python-ide/

Basically, install pymacs, rope, and ropemacs. You get:

A note about the code-completion in rope: it works via the minibuffer. There's an emacs package called AutoComplete that works via a popup GUI. You can configure it so that it uses rope. Good blog entry here: http://www.enigmacurry.com/2009/01/21/autocompleteel-python-code-completion-in-emacs/

For unit tests, see this website: http://pedrokroger.com/2010/07/configuring-emacs-as-a-python-ide-2/

I've been using Debian derivatives for a while now (first Debian, then Linux Mint Debian Edition, then standard Linux Mint) and although I'm usually one to use the distributions packages when possible, in this case I found it better to use the most up to date packages I could. Bugs are fixed in the latest version that make them difficult to use with the latest emacs.

Install Pymacs

This is an interface between emacs and python, allowing you to write emacs extensions in python. There are two pieces, and emacs piece and a python piece.

The emacs piece is easy to install. If you have the package system set-up like this:

(when (>= emacs-major-version 24)
  (require 'package)
  (package-initialize)
  (add-to-list 'package-archives
               '("elpa" . "http://tromey.com/elpa/"))
  (add-to-list 'package-archives 
               '("marmalade" .  "http://marmalade-repo.org/packages/"))
  (add-to-list 'package-archives 
               '("melpa" . "http://melpa.milkbox.net/packages/") t)
)

Then you can just select Pymacs to install via the packages buffer. This will install the emacs part of Pymacs, but not the python part. To install the Python part, do this:

pip install -e "git+https://github.com/pinard/Pymacs.git#egg=Pymacs"
cd $VIRTUAL_ENV/src/pymacs
make

In my case I was installing as root, so I had to cd into /root/src/pymacs:

cd /root/src/pymacs
make

Then copy over Pymacs.py to where the local python packages are kept:

cp /root/src/pymacs/Pymacs.py /usr/local/lib/python2.7/dist-packages

Virtualenvs

You can install virtualenv.el to get the ability to switch between virtualenvs within emacs. What this means exactly, I'm not 100% about. I think it means that you will be able to launch an inferior python process within emacs that will use the packages in your virtualenv.

Unfortunately, I do not think that ropemacs actually ends up using the virtualenv once you switch into it. I'm not sure why, but I suspect that the python process used by rope isn't the same one that's used for the Python inferior modes.

One possibility is to start your emacs process on the command line once you've activated your virtualenv. This is inconvenient if you use emacs for a bunch of other things and you already have an instance running before you start editing your python files.

Another possibility seems to be pasting this is config.py in your .ropeproject/ folder:

VIRTUAL_ENV = "/home/user/envs/envname/"
ACTIVATE_FILE = VIRTUAL_ENV + "bin/activate_this.py"
execfile(ACTIVATE_FILE, dict(__file__=ACTIVATE_FILE))

This works, and it's very simple, though it seems a bit hackish to me.

Accessing the documentation

Maybe use rope?

You also do this via pylookup. Note that pylookup only appears to work on python 2.6 and above.

Good instructions here:

https://github.com/tsgates/pylookup

Basically, you download the package and put it somewhere in your emacs path, like ~/.emacs.d. You create the database index by running:

./pylookup.py -u python-2.7.1-docs-html

The -u parameter can be a URL if you want to access the web for the documentation. Otherwise, point the command to a local copy of your documentation.

Try it out by running this:

./pylookup.py -l ljust

In your .emacs file, put this:

(autoload 'pylookup-lookup "pylookup")
(autoload 'pylookup-update "pylookup")
(setq pylookup-program "~/.emacs.d/pylookup/pylookup.py")
(setq pylookup-db-file "~/.emacs.d/pylookup/pylookup.db")
(global-set-key "\C-ch" 'pylookup-lookup)

Code completion

Rope is a popular way to go here. It's non-emacs specific.

Ropemacs integrates rope into emacs. You can also hook AutoComplete into Rope (for pull down completion menus)

Also, http://www.emacswiki.org/emacs/Anything:anything

PythonAndEmacs (last edited 2015-07-18 02:44:59 by DesmondRivet)