0

How can I run pdb from emacs on a file that is activated in a given conda environment.

I have setup exec-path and PATH to contain the current conda path, eg.

exec-path
("~/miniconda3/envs/sci/bin" ...)

(getenv "PATH")
"~/miniconda3/envs/sci/bin:..."

From within emacs,

(executable-find "python")
"~/miniconda3/envs/sci/bin/python

returns the proper python. pdb is located at "/usr/bin/pdb". However, if I run pdb on a file that is running in the sci conda environment, eg. its has numpy, etc. installed, pdb can't find those libraries:

test.py

import numpy as np
import pandas as pd

tst = np.linspace(1, 10, num=10)
print(tst)

pdb ./test.py

Current directory is 
~/scratch/python/
> 
~/scratch/python/test.py(1)<module>()
-> import numpy as np
(Pdb) n
ImportError: 'No module named numpy'
> 
~/scratch/python/test.py(1)<module>()
-> import numpy as np
(Pdb) 
Rorschach
  • 31,301
  • 5
  • 78
  • 129

2 Answers2

0

Customizing gud-pdb-command-name to python -m pdb seems to work.

So, pdb python -m pdb ./test.py runs in the correct environment.

Rorschach
  • 31,301
  • 5
  • 78
  • 129
0

When using virtualenv or conda what I do is activate the env in a terminal and then launch emacs from that terminal. On a Mac:

$ conda activate hcpy
(hcpy)$ /Applications/Emacs.app/Contents/MacOS/Emacs

if you want to just have a terminal version of emacs add the argument -nw

then in emacs the command M+x pdb

works fine for me. The issue here is that there are often multiple python installs. So it is not enough for emacs to find a python exe, emacs needs to find the python that has the libraries installed.

so for instance if I go:

$which python

I get:

/Users/jamesanderson/anaconda3/bin/python

but, if I go:

$source ./.py3dev/bin/activate
$which python
/Users/jamesanderson/code/python/camera/.py3dev/bin/python

so setup your virtual env with conda with the libraries you need. When you are inside emacs and start a shell, and do the which python step. The answer has to be the instance of python with the libraries installed.

James Anderson
  • 589
  • 2
  • 8
  • that doesn't work on Ubuntu (kinda doubt it works on a Mac, did you try debugging an environment with a package not installed in your default env?), but it's also just too much extra baggage. Setting the conda env in emacs isn't a problem, you just need to manage the `exec-path` and `PATH` correctly when switching environments – Rorschach Sep 17 '18 at 15:14
  • on linux, just type `emacs` not the mac path (mac comes with an ancient emacs that is the default). – James Anderson Sep 17 '18 at 15:28
  • I'm on emacs 27, I don't have a system install, its build from source – Rorschach Sep 17 '18 at 15:29