8

I am currently debugging code using Python. I have not been using Python for a while. I put some breakpoints on a variable which is an integer. Let's say this variable is X = 10. How can I:

  • see what is in the variable? (I can highlight and a yellow case appears but if there are a lot of information it is not practical to display like this)
  • do some manipulation of the variable, for example I would like to do X+2 and get the result?
2
  • I don't know about Python in Eclipse but in regular Eclipse there is a Variables window, which allows you to view only the variables and their values (menu Window, Show view, Other, Debug, Variables).
    – Dominique
    Commented May 31, 2016 at 11:24
  • If everything else fails, a quick and dirty way for doing this (in most cases) would be just to print the variable in the console: >> print X >> print type(x) And stop your program just after that, if necessary: >> sys.exit() Commented Jun 8, 2016 at 10:59

5 Answers 5

3
+50

As noted in the comments, there are many possible IDEs you can use with Python. The original question was specifically about Eclipse and so my answer focuses on a solution using that IDE. Other solutions are possible if you prefer a different environment...

First off, you need to sort out which plug-in you use for eclipse. You have a few options as you can see on the python wiki. It looks like PyDev is more popular, but you could pick others.

Assuming you go for PyDev, you can use the watch facility as described here to evaluate any expression. Alternatively you can use the console debugger to evaluate code directly in the debugger.

EDIT: As per the comments, you can also open the Expressions window using Window > Show View > Other > Debug > Expressions. This is the same window as used for the watch facility and the contents can be edited directly.

6
  • Also worth mentioning that with PyDev in Eclipse a simpler means to do exactly this is to open Expressions under Window > Show View > Other > Debug > Expressions. From here, when visiting a breakpoint you will have access to all your local variables as they are at that time making it very easy to test variables and even callables.
    – Murphy4
    Commented Jun 13, 2016 at 22:28
  • @Murphy4 Thanks - I've added that to the answer. Commented Jun 13, 2016 at 23:21
  • this is quite a difficult way to do this , unless you are familar with eclipse i would use PyCharm , its a great at the handholding part beginners need because its coding hints are really helpfull.
    – Amias
    Commented Jun 14, 2016 at 10:55
  • @Amias I find that IDEs are a matter of taste. Some people I know prefer Eclipse, others use PyCharm. The OP asked for a solution in Eclipse (hence the tag). Commented Jun 14, 2016 at 11:01
  • sure , just pointing out there are much easier ways to do this than with eclipse. The edits to this post have confused it slightly because the mentions of eclipse in the question have been removed.
    – Amias
    Commented Jun 14, 2016 at 11:08
1

I'm sure everyone has their own opinion, but I find the python tools for Eclipse quite complex. If you aren't tied to an Eclipse IDE, try PyCharm. It has very good tools for doing exactly what you want, and many tutorial videos. This video shows you how to do exactly what you have requested: https://www.youtube.com/watch?v=QJtWxm12Eo0

2
  • 1
    Pycharm has a excellent debugger integration and doesn't get in the way like other IDE's , i've used it for the past few years and can't recommend it enough. The nice thing is that once you have learnt one jetbrains IDE you can easily switch to another that uses another language and be productive very quickly.
    – Amias
    Commented Jun 14, 2016 at 10:53
  • Dan, the video you are referring to is marked as "private" and not available at the moment. Commented Jan 26, 2024 at 7:17
1

I don't think you can do this purely in Eclipse, not without extensive plugins. However, there are some very good python debugging tools out there.

If you're willing to get your hands dirty, the builtin python debugger pdb does everything you want and more. Import pdb, pdb.set_trace() where you want to break, and you step into an interactive debugger that's very familiar if you've used gdb before.

If you want to use something more IDE focused, I recommend Immunity Debugger. It's really rather good, and has a lot of documentation. This might be a little more than you're looking for though.

0

Try opensource, free VS Code (with Python plugin). It has a slick intellisense feature and you can watch, inspect the variables, debug them. The editor is node.js based and can work in every platform. Eclipse is overkill for python development IMHO.

0

ipdb is a handy tool for python debugging:

ipdb exports functions to access the IPython debugger, which features tab completion, syntax highlighting, better tracebacks, better introspection with the same interface as the pdb module.

To install ipdb, simply run pip install ipdb --user in your shell.

To set breakpoint, add import ipdb; ipdb.set_trace() before the line where you want to jump into the debugger. E.g.:

import ipdb; ipdb.set_trace()
X=10

Once you run your program python myfunc.py, an IPython-like interactive shell will be triggered and you can run python commands in it. E.g.:

ipdb> p X
10
ipdb> X+2
12

Here is a simple tutorial: An Introduction to Python Debugging

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.