2

As a self-taught programmer, I learned to debug using an interactive console that kept all of my variables in memory when I build /run the script. However, I noticed the overwhelming trend for debugging in IDEs (and, I suppose CLI + Editor solutions, for that matter) is to build your script in one place and provide a separate console "sandbox" type area that only keeps variables if you copy/paste your code.

How do you debug without an interactive console? Can anyone list a few debugging steps that could help me be a better programmer / debugger?

Currently, this is a very simplified version of what I do:

  1. Write some pseudocode (sometimes)
  2. Write some code in an editor that should work
  3. run / build the script
  4. Check stdout for errors
  5. If no errors, then 7.
  6. If errors, then back to 2 after fixing the offending code.
  7. Type variable names into console to verify that they look like I anticipated.
  8. Rinse and Repeat until it works as I intended.
2
  • "if no errors, then 7" ? Commented Aug 5, 2013 at 20:28
  • @jh314 That answers a different question I had about how to run python scripts from within python. I now feel like a moron for not having heard of pdb, so your comment is extremely helpful. Thanks. Commented Aug 5, 2013 at 21:10

5 Answers 5

8

The best way to do this would be to write tests. That automates steps 3 through 7 for you. It also prevents regressions from occurring when you change other code.

Sign up to request clarification or add additional context in comments.

Comments

1

you can use the q module for this easily https://pypi.python.org/pypi/q

xyxy.py

import q do_something() q.d() #this will open interactive shell def f(): do_something() q.d() #open console here with access to all local variables of f 

you can also use automated tests (builtin unittest module or nosetests or something else)

Comments

1

Use a decent python IDE - there are a lot out there and you will be able to stop at breakpoints inspect variables by hovering or adding watches and enter a context console where you can interact with your code in the context of the breakpoint.

2 Comments

I actually got in StackOverflow trouble for asking the IDE question here. Pycharm and many others do this like I am used to, but I'm picking up that it's not the best way to code.
Which is best is a matter of opinion hence not a good question - the best way to code is to know it all and get it right first time. Doesn't happen that often to most of us.
1

Use print statements in between the areas of problem code... otherwise, just download a good IDE

1 Comment

Increasingly, this is the answer I'm comfortable with. Thanks.
0

It turns out that PyCharm, at least, DOES have an interactive console and the default keymapping (on Mac) is option-shift-E. Then your variables are loaded in memory. However, the suggestions above are better programming practices.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.