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.
TheProletariat
  • 916
  • 2
  • 11
  • 23

5 Answers5

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.

Community
  • 1
  • 1
Amber
  • 507,862
  • 82
  • 626
  • 550
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)

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
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.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • I actually got in StackOverflow trouble for asking the IDE question [here](http://stackoverflow.com/questions/18025529/is-there-a-python-ide-that-keeps-variables-in-memory?noredirect=1#comment26364918_18025529). 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. – TheProletariat Aug 05 '13 at 20:41
  • 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. – Steve Barnes Aug 05 '13 at 20:49
1

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

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.

TheProletariat
  • 916
  • 2
  • 11
  • 23