38

I am trying to track down when a variable gets updated. I have a watcher, but is there any way to set it up so that the execution can be paused when the value is getting updated?

This is called a watchpoint or a data breakpoint.

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
thinkanotherone
  • 2,905
  • 9
  • 29
  • 37
  • 1
    Related discussion: https://intellij-support.jetbrains.com/hc/en-us/community/posts/205802829-watchpoints-in-PyCharm- – guettli Dec 16 '19 at 13:21
  • 6
    **If you want this feature please vote for [Support Data breakpoints](https://youtrack.jetbrains.com/issue/PY-30387) PyCharm issue.** – Piotr Dobrogost Apr 07 '20 at 12:56

3 Answers3

10

Currently pycharm does not have a default built-in feature that tracks variable modification in real time. Alternatively you can do this:

  1. run debug
  2. From debugger pane -> Variables, right click the variable you would like to track and and add it to Watches.
  3. In Watches pane, right click the variable and select referring objects.

The feature you are talking about is, I believe, called watchpoint support and according to this article: http://sourceforge.net/blog/watchpoints-in-python/ Eric and PyScriptor has the feature but not pycharm.

TastyCatFood
  • 1,632
  • 1
  • 15
  • 27
  • 11
    Sorry, but this feels like a cliffhanger. Once you select "referring objects", then what? I tried right-clicking on the referring object to see if I could request that the program pause, but didn't see anything like that. What is the purpose of identifying the referring object? Please clarify. – John Strong Sep 26 '17 at 12:58
  • 1
    @John Strong It looks like I was explaining how to find objects that looked-at/made-changes to a variable at a given time which is sometimes helpful;but it's been a long time since I last used PyCharm, so I cannot remember the details. Not certain if PyCharm does not still have watch point support but people generally have setters and getters, so maybe the feature is not that wanted. – TastyCatFood Oct 02 '17 at 17:00
3

Checkout watchpoints:

watchpoints is an easy-to-use, intuitive variable/object monitor tool for python that behaves similar to watchpoints in gdb.

An answer to How do you watch a variable in pdb describes how it compares to other approaches and why it's favorable.

As for better integration with pycharm, see Support other debuggers like pydevd


Regarding built-in python support and performance impact, see:

  1. Add C hook in PyDict_SetItem for debuggers
  2. add support for watching writes to selected dictionaries

Further notes are available on other questions on SO:

  1. How can I make PyCharm break when a variable takes a certain value? "without modifying my code"
  2. Is there a way to set a breakpoint on variable access in Python with PyDev? concerns a global variable
  3. How to trigger function on value change? combining observable with breakpoint() or pydevd.settrace() might be a solution if more control is needed
  4. Is there a free python debugger that has watchpoints? [closed, "asking us to recommend"]

Lastly, I repeat the proposal to vote for Support Data breakpoints PyCharm issue.

Dvir Berebi
  • 1,406
  • 14
  • 25
2

You can add a breakpoint in the line you need to watch and right-click it. Then in the dialog box you have "condition" as last input: add a condition that uses the variable you need and it should stop when you set it to.

  • 9
    The problem is there is no line to set a breakpoint on as the OP is not sure where and when the variable gets updated – *I am trying to track down **when** did a variable get updated.* For comparison take a look at the following description of this feature from [documentation](https://sourceware.org/gdb/current/onlinedocs/gdb/Set-Watchpoints.html) of gdb – *You can use a watchpoint to stop execution whenever the value of an expression changes, without having to predict a particular place where this may happen. (This is sometimes called a data breakpoint.)* – Piotr Dobrogost Apr 07 '20 at 10:15
  • Exactly, you can see an example for C# here: https://blog.jetbrains.com/dotnet/2020/07/22/data-breakpoints-and-pin-to-frame-debugger-updates-in-rider-2020-2-eap/ – Andrei R. Feb 28 '21 at 10:19