TheProletariat
TheProletariat

Reputation: 1056

How do you debug without an interactive console

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.

Upvotes: 2

Views: 472

Answers (5)

Emily Sheehan
Emily Sheehan

Reputation: 41

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

Upvotes: 1

TheProletariat
TheProletariat

Reputation: 1056

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.

Upvotes: 0

Steve Barnes
Steve Barnes

Reputation: 28370

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.

Upvotes: 1

Joran Beasley
Joran Beasley

Reputation: 113930

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)

Upvotes: 1

Amber
Amber

Reputation: 526523

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.

Upvotes: 8

Related Questions