Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Debugging scripts basics
#1
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()
Reply
#2
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
Reply
#3
(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.
Reply
#4
Why not use an IDE with debugging?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply
#6
(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.
Reply
#7
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.
Reply
#8
ipdb is another (better, IMO!) option.
Reply
#9
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Learned Python Basics, what now? muzikman 20 5,861 Oct-11-2021, 01:34 AM
Last Post: Underscore
  PYserial basics? bako 10 5,026 Apr-26-2020, 09:15 PM
Last Post: bowlofred
  [split] Debugging scripts basics tultalk 5 3,020 Apr-25-2020, 01:02 AM
Last Post: menator01
  Problem with basics Wraith2102 2 1,963 Jun-17-2019, 06:51 PM
Last Post: perfringo
  Dictionary Basics pythonjm 5 3,475 Nov-26-2018, 11:11 PM
Last Post: pythonjm
  What to do after learning python basics xyzabc12310000 1 2,819 May-20-2018, 11:43 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020