Python Development In A VM Using Emacs And Virtualenv

, Wellesley, MA

PyCharm recently added some excellent configuration options to facilitate using a remote Python interpreter. With them you can run PyCharm on a host machine, and execute everything on a guest VM. You can keep a single copy of the code and a virtualenv, shared between the host and guest, and tell PyCharm how to map the local and remote filesystem paths to each other. PyCharm will even do code completion without having a second copy of the code or virtualenv on the host machine. Beautiful!

I searched for something similar in Emacs, and I found a couple of interesting leads[1][2], but complete solution that’s exactly what I want. I prototyped two approaches:

  1. Shared filesystems, with a mapping between guest and host.
  2. TRAMP, referencing the code over SSH using TRAMP.

The former is pretty raw and incomplete. The latter is more promising in the near term. It consists of:

  1. python.el - a replacement for the python.el distributed with Emacs 24 that makes its python-send-region TRAMP-aware.

  2. demo.py - an interactive demo.

  3. bootstrap-virtualenv.sh - a Bash script to bootstrap a virtualenv for use via Emacs over TRAMP.

  4. python.sh - a Bash script to run python in a virtualenv.

  5. .dir.locals.el - configuration for Emacs to run python via the Bash script wrapper.

  6. tramp-dir-locals.el - a hack to make directory local variables work in directories opened via TRAMP.

If you’re doing anything similar, I’d love to know how.

Update: 2012-06-06: Michael Albinus started improving the upstream code on the Emacs trunk today!

Enjoyed reading this post? Discuss it on Reddit, or follow me on Twitter.