Python Forum
Debugging scripts basics - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Debugging scripts basics (/thread-26202.html)



Debugging scripts basics - bako - Apr-24-2020

So, I'm mainly a BASH scripter looking to jump into the Python boat.

What's the equivalent to bash -xv when running a python script? Rather than bug people necessarily, I need help on trying to troubleshoot the logic of a python script.

I've already read https://python-forum.io/Thread-Basic-Simple-debugging-and-how-to-read-tracebacks

Scenario:
I'm trying to get the value of channel 7 on a Raspberry Pi to figure out if the value needs to be changed. From my understanding, GPIO.input should output a 1 or a 0. But, when running the script I get nothing. I've even tried usind -d and -vv. But all I get are entries for Importing modules and cleanups.

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN)
GPIO.setup(7, GPIO.OUT)

GPIO.input(7)
time.sleep(4)

GPIO.cleanup()



RE: Debugging scripts basics - Larz60+ - Apr-24-2020

Since yo like to work from shell, look at pdb which can be imported directly into your script:
https://docs.python.org/3/library/pdb.html


RE: Debugging scripts basics - bako - Apr-24-2020

(Apr-24-2020, 01:49 AM)Larz60+ Wrote: Since yo like to work from shell, look at pdb which can be imported directly into your script:
https://docs.python.org/3/library/pdb.html

Care for an example? From what I can tell it's to debug modules, not the script that I'm actually importing it into.


RE: Debugging scripts basics - buran - Apr-24-2020

Why not use an IDE with debugging?


RE: Debugging scripts basics - bako - Apr-24-2020

(Apr-24-2020, 02:58 AM)buran Wrote: Why not use an IDE with debugging?

I'm old fashioned? IDK... I just feel more comfortable using VIM. That and the script is "remote" on a Raspberry PI with a Hat and I'm using a Mac.


RE: Debugging scripts basics - bowlofred - Apr-24-2020

(Apr-24-2020, 02:27 AM)bako Wrote: Care for an example? From what I can tell it's to debug modules, not the script that I'm actually importing it into.

It will work fine for your regular script as well.

$ python -m pdb yourscript.py
The main commands you'll want to familiarize yourself with are l for listing at the current location, b to set a breakpoint on a line or a function, c to continue to a breakpoint, n for next line, s to step into a sub function, and you can p to print variables and data.

If you are running 3.7 or later, you can simply put the breakpoint() function in your code to stop at that location and examine variables. You don't have to explicitly import pdb.


RE: Debugging scripts basics - Larz60+ - Apr-24-2020

Bako, I'm a bit old fashioned as well, but to not use an IDE is slowing yourself down. I'm in my 70's, started back in the sixties, so if I can do it so can you. I used pdb quite some time ago, but it is definitely for debugging your own code. Can't remember, since I do all of my debugging in VSCode IDE now.


RE: Debugging scripts basics - ndc85430 - Apr-24-2020

ipdb is another (better, IMO!) option.


RE: Debugging scripts basics - Larz60+ - Apr-24-2020

I have done a lot of embedded mpu work and I use a Saleae logic analyzer when debuging GPIO, A/D converters, timers, etc. Thought I'd suggest that, however I just looked up the price, and it has skyrocketed since I bought mine. I paid $139 US, today price is $699.
https://www.sparkfun.com/products/13196
Glad I bought it when I did.
Perhaps a used one can be found.
I have the the 8 channel one, so you can see eight signals at once, very handy.