Debugging with python when using the python environments.

Had to debug python code when I am using a python virtual environment. 

For simple debugging:

python -m pdb add.py

For  debugging with environments:

Learned this method when needed to debug the script that calls creates another environment. 

Step 1: start a new bash screen in order to NOT to mess up the current python environment.

When we are already using python environment and want to debug the code that uses it’s own environment we should use a new bash. We are doing it because our script is starting a new python environment as we can see in step 2.

(env) ubuntu@ip-172-31-6-144$ bash

Step 2: add -m pdb flag to python code

run.sh

#!/bin/bash

source venv/myenv/bin/activate
python -m pdb add.py 

Step 3: Run script with source in front:

doing that is the same as typing that stuff into the terminal.

we need that because pdb relies on the terminal for its interactivity

we cannot run it in a subprocess and be able to type into the prompt.

(env) ubuntu@ip-172-31-6-144$ source run.sh

Step 4: Debug: run, c, print(var)

(Pdb) run
(Pdb) c
(Pdb) print(i) # if you have variable i
(Pdb) print(words) # if you have variable words

Comments