Python Forum
what do YOU use to debug Python code you write? an IDE?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what do YOU use to debug Python code you write? an IDE?
#1
what do YOU use to debug Python code you write?  an IDE?  what tools and methods?  do you ever do a detailed trace to see each statement being executed and print all variables involved?  do you ever add in special print calls to output information need to see?

if the output or error message doesn't make the cause of the problem obvious, i usually start debugging by adding special print calls.  my "IDE" is an interactive bash shell and an emacs editor.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I use the debugger built into PyCharm IDE.
Not sure if it's their own or a package.
It has pretty much all that gdb had (when I used it with Emacs for C)
Reply
#3
The biggest script I've written is less than 300 line of code. I have tried pudb once only and honestly I didn't know what to do with this.  Cool
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
the biggest script ive coded is 1500 loc, i mostly use leafpad. nano is ok but scrolling is nicer in leafpad. a lot of python coders work with several files at once-- leafpad is far from ideal for that. though i suppose if your window manager lets you group and tab windows together, that could work.

Quote:do you ever do a detailed trace to see each statement being executed and print all variables involved?

not really.

Quote:do you ever add in special print calls to output information need to see?

absolutely.
Reply
#5
(Dec-02-2017, 09:14 AM)wavic Wrote: The biggest script I've written is less than 300 line of code. I have tried pudb once only and honestly I didn't know what to do with this.  Cool

so, what do you do to debug your code if the first run that doesn't abort produces incorrect output?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
For such a small code I just read the error messages and jump to the pointed line. I use prints if it necessary to see what is happening.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
what if there are no error messages (and thus, no line numbers) but the output your program produces is wrong.  suppose you write a script to take a year, month, and date, and without using any library, it calculates what day of the week that is.  then you enter 1941, 12, 7 and it prints out "Monday" even though you know the correct answer is "Sunday".  or suppose your phase of the moon script says that the moon today is a New moon.

how do you debug these kind of error?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
by looking for parts of the code that dont do what they should.

you look over the code for mistakes that you made. you wrote the code to do what you want, so you read the code to make certain its what you meant. you try out parts of the code (by reading it) to double check its working. if necessary, use prints.

the first time you do this is an important step. the more you do it, the better you get at spotting mistakes. its not mysticism, though its difficult to describe without examples-- it totally depends on the code youve written.

i wrote a very short chapter on debugging that names simple things like:

* Are you working with strings or numerics? (or lists?)

You can change one to another, but some commands and your own user-defined functions may require specifically strings or specifically numerics.

(these will usually error but sometimes they can be sneaky.)


* Are all the variables spelled the same way?

(most of these are caught when referencing an unset variable returns an error.)


* Inside and outside the loop: make certain that each line has the proper indentation.

(i caught one of these on this forum the other day. no error, just looked at the code and noticed the functions return command was inside the loop, which both exited the function and stopped the loop early.)


the best way to learn how to do this is to practice.
Reply
#9
just wonder how many people insert prints to help them debug
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
I do, for small scripts. For a bigger projects I prefer proper logging, with different levels, e.g. DEBUG, INFO, ERROR, CRITICAL, etc. and different logging streams/handlers - stdout, mail, file, etc.
Reply


Forum Jump:

User Panel Messages

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