Python Forum

Full Version: Debugging scripts basics
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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-Sim...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()
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
(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.
Why not use an IDE with debugging?
(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.
(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.
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.
ipdb is another (better, IMO!) option.
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.