Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting pdb in Emacs to use Python process from current virtualenv

Tags:

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?

like image 931
codeasone Avatar asked Sep 17 '10 11:09

codeasone


2 Answers

Invoke pdb like this:

python -m pdb myscript.py 

Instead of

pdb myscript.py 
like image 139
spookylukey Avatar answered Oct 05 '22 12:10

spookylukey


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) 

To have pdb in your virtual environment, do the following :

cp /usr/bin/pdb /path/to/virtual/env/bin 

Then edit the first line of /path/to/virtual/env/bin/pdb to have :

#! /usr/bin/env python 

Reactivate your env and Pdb should now use your virtualenv python instead of the system-wide python.

like image 41
Jérôme Radix Avatar answered Oct 05 '22 12:10

Jérôme Radix