Skip to content Skip to sidebar Skip to footer

In Pycharm How To Load Code To Interactive Debugger?

I want to be able to load and run several line of code into interactive debugger, to make sure the code works fine. By doing so I don't need to rerun from the beginning of the code

Solution 1:

Pressing Alt+Shift+E will invoke the command "Execute Selection in Console". If there is no open console already, PyCharm will open a regular (non-debug) Python console.

To execute the selection in a debugger console, you first need to have the debugger running. You need to run a script using the debugger and pause it with a breakpoint, then the command will behave as expected.

If you have more than one open console, e.g. one regular and one debugger, you must activate the target console before performing the keyboard command.

Solution 2:

Ok so I might be off on this a little bit but from my use of R I have felt the frustration the comes from running code by selection. For instance if you select a block of code that initializes some variables with the debugging mode and later delete them in your source things may get a little messy later if the source you had selectively run is altered. Also running code piece wise sometimes buggers the order, but the urge to run code from the top every time is certainly well fought by the wait time required to accomplish certain tasks.

So the question is, when you restart pycharm does the does the python console remember the things that you ran in it last time you had pycharm open? No it doesn't but when you run your program the plot ovbiously works because plt is defined on line 2. If you just run line 6 without line 2 you have problems.

Also it looks like you set a breakpoint on line six which is another matter entirely...

Solution 3:

  1. First make sure you have properly closed (not just hidden) the console by clicking the close console button on the left, which is a red X (see this page).
  2. Now (re-)open the console and click the "attach debugger" icon, which the green bug icon second from the bottom on the left (just above the green plus). It should become pushed in and grey.
  3. Now if you import your modules and run functions in them within, any breakpoints set in those functions should be hit like normal and you can single-step through them, just like when you're debugging a script. You can do this by typing directly in the console or by selecting code and choosing "execute in console" as described in another answer. It seems like the stack trace doesn't work though (in my quick test). For example, if you have function "fn()" in file "foo.py" in directory "dir", try this: import dir.foo dir.foo.fn()

  4. If you change your code after you import it, the Python console will still be using the old code. (If you then stop at breakpoints, this can get very confusing.) You can force an update to the new code by calling the reload function as show below. Alternatively, go back to step 1 (which is a good idea if things start to break confusingly). from importlib import reload # This line only needed on Python 3 reload(dir.foo)

Solution 4:

Alhough it is not a pycharm's feature, I would strongly suggest trying jupyter notebook. It is very useful for prototyping.

If, you want to do it the pycharm way, as Dag Høidahl pointed out, one option is to execute block of code in console with Alt+Shift+E.

However, to fully leverage the awesome Pycharm's debugger, you should write an unit test calling particular piece of code which you want to test (or debug). This way you can add breakpoints properly debug, or automatically ensure expected behavior in the future. Nevertheless, writing unit tests has many other advantages you will appreciate.

Post a Comment for "In Pycharm How To Load Code To Interactive Debugger?"