views:

75

answers:

1

I am debugging some python code in emacs using pdb and getting some import issues. The dependencies are installed in one of my bespoked virtualenv environments.

Pdb is stubbornly using /usr/bin/python and not the python process from my virtualenv.

I use virtualenv.el to support switching of environments within emacs and via the postactivate hooks described in

http://jesselegg.com/archives/2010/03/14/emacs-python-programmers-2-virtualenv-ipython-daemon-mode/

This works well when running M-x python-shell

>>> import sys
>>> print sys.path 

This points to all of my virtualenv libraries indicating that the python-shell is that of my virtualenv.

This is contradicted however by M-! which python, which gives /usr/bin/python

Does anyone know how I can tell M-x pdb to adopt the python process from the currently active virtualenv?

+1  A: 

python-shell uses variable python-default-interpreter to determine which python interpreter to use. When the value of this variable is cpython, the variables python-python-command and python-python-command-args are consulted to determine the interpreter and arguments to use. Those two variables are manipulated by virtualenv.el to set the current virtual environment.

So when you use python-shell command, it uses your virtual environments without any problem.

But, when you do M-! python, you're not using the variables python-python-command and python-python-command-args. So it uses the python tools it finds in your path.

When you call M-x pdb it uses gud-pdb-command-name as the default pdb tool. To redefine this variable, each time you activate an environment, you could do something like this :

(defadvice virtualenv-activate (after virtual-pdb)
  (custom-set-variables
     '(gud-pdb-command-name
        (concat virtualenv-active "/bin/pdb" ))))

(ad-activate 'virtualenv-activate)
Jérôme Radix
Thanks Jerome. For others trying to get this working note that I had to cp /usr/bin/pdb to <path-to-virtualenv>/bin and change the shebang to use the virtualenv python executable. This is because mkvirtualenv --no-site-packages <env-name> does not deposit the necessary pdb into the /bin directory of the newly created environment.
landstatic
Thx for those details. You could also create a symbolic link to avoid copying of files.
Jérôme Radix