Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debuggin with pdb within a conda environment

I'm developing in python inside a Conda environment. All the packages I add to the environment can be imported successfully when running the "python" binary created under the environment. However, when trying to debug with pdb any of my python scripts I would get ImportError for the very same packages.

For example, after creating a new environment and adding the following packages

pip install keras
pip install conection

I run the following test.py script

import keras
import connexion

print("I have imported keras alright")
print("I have imported connexion alright")

from keras.models import Sequential
from keras.layers import Dense, Activation

# for a single-input model with 2 classes (binary):

model = Sequential()
model.add(Dense(1, input_dim=784, activation='softmax'))

print("I have defined a keras network alright")

This works alright when invoking it the usual way,

python test.py  # Works OK

but fails when running in debug mode in pdb

pdb test.py # ImportError: No module named connexion

The question is: how can pdb be properly configured to work with the packages that were installed in the conda environment?

Additional info: while the python binary is indeed in the conda environment

which python # returns $HOME/miniconda3/envs/$USER/bin/python

pdb seems to always refer to the system version

which pdb # returns /usr/bin/pdb
like image 404
albarji Avatar asked Jul 13 '16 08:07

albarji


2 Answers

Alternatively, use python3 -m pdb <script> to use pdb with conda and python 3

like image 64
randkego Avatar answered Oct 03 '22 00:10

randkego


Copy pdb executable to your environment, and set the shebang (first line) from #!/usr/bin/python to #!/usr/bin/env python. If you want this to be the default behavior for any environment (including the system pdb), you can change the shebang only in /usr/bin/pdb.

like image 33
Dean Fenster Avatar answered Oct 02 '22 23:10

Dean Fenster