Refactoring Python Remotely In Emacs

, Wellesley, MA

After a few small changes since my last update, now I can refactor Python remotely in Emacs.

Refactoring Hello World

I’m running the latest Emacs nightly build, Emacs-2012-09-15-110034 from on my Mac.

I have a CentOS VirtualBox VM that I manage using Vagrant. It has several different Python projects, each using its own virtualenv.

I add Rope, Ropemacs, and Pymacs to my virtualenv.

[vagrant@vagrant-centos-5-8 ~]$ workon tramp-virtualenv-ropemacs
(tramp-virtualenv-ropemacs)[vagrant@vagrant-centos-5-8 ~]$ pip install rope
(tramp-virtualenv-ropemacs)[vagrant@vagrant-centos-5-8 ~]$ pip install -e hg+
(tramp-virtualenv-ropemacs)[vagrant@vagrant-centos-5-8 ~]$ pip install -e git+
(tramp-virtualenv-ropemacs)[vagrant@vagrant-centos-5-8 ~]$ pushd $VIRTUAL_ENV/src/pymacs
(tramp-virtualenv-ropemacs)[vagrant@vagrant-centos-5-8 pymacs]$ make install
(tramp-virtualenv-ropemacs)[vagrant@vagrant-centos-5-8 pymacs]$

In Emacs, I start a Python project using the virtualenv I just created.

C-x C-f /vagrant@ RET
M-x make-directory RET RET
;;; Directory Local Variables
;;; See Info node `(emacs) Directory Variables' for more information.

  (eval . (tramp-virtualenv "~/.virtualenvs/tramp-virtualenv-ropemacs"))
C-x C-s
C-x C-f /vagrant@ RET
M-x load-file RET /vagrant@ RET
M-: (pymacs-load "ropemacs" "rope-") RET

I start writing some Python code that I can refactor.

C-x C-f /vagrant@ RET
def hello():
    return "Hello, world!"
C-x C-s

I start a Rope project.

C-x p o /scpc:vagrant@ RET y

I select "Hello world!", and I extract a variable.

C-c r l greeting RET preview RET yes RET

Finally, I reload the refactored changes into Emacs.

C-x C-f M-n RET

As far as I can tell, Emacs can only have one Pymacs Python process at a time. Pymacs and Rope don’t know how to change the active virtualenv yet. I might tackle that next.

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