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 emacsformacosx.com 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+https://matthewlmcclure@bitbucket.org/matthewlmcclure/ropemacs#egg=ropemacs
(tramp-virtualenv-ropemacs)[vagrant@vagrant-centos-5-8 ~]$ pip install -e git+https://github.com/matthewlmcclure/Pymacs.git@tramp-attempt-2#egg=pymacs
(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@192.168.33.2:~/tramp-virtualenv-ropemacs/.dir-locals.el RET
M-x make-directory RET RET
---
;;; Directory Local Variables
;;; See Info node `(emacs) Directory Variables' for more information.

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

I start writing some Python code that I can refactor.

C-x C-f /vagrant@192.168.33.2:~/tramp-virtualenv-ropemacs/hello.py RET
---
def hello():
    return "Hello, world!"
---
C-x C-s

I start a Rope project.

C-x p o /scpc:vagrant@192.168.33.2:/home/vagrant/tramp-virtualenv-ropemacs/ 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.