Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Pycharm How to load code to interactive debugger?

Tags:

python

pycharm

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, which takes much longer time. I tried two methods but neither work. Coming from R and Matlab, I do prefer this feature. Anyone can suggest?

Method 1: According to interactive shell debugging with pycharm, one can load the code typed in pycharm editor to interactive debugger by

1> set a debugging point
2> highlight the code you want to load to Python console
3> alt+shift+E

When I follow these steps: I set the debug point, debug the code to trigger the debug point, and select the code press alt+shift+e.Instead of loading the code to the debug console, it loaded to the python console by default. I don't have access to variables and packages that I want to debug under the python console context.

I have to minimize the python console, then run it again. Sometimes it runs in debugger console. Below answer suggests to "select the console you want to target". What is the step? you click the debugger? then select the code and press atl+shift+e? I tried, it does not work.

EDIT: found the official doc but not very helpful: https://www.jetbrains.com/help/pycharm/2017.1/loading-code-from-editor-into-console.html

enter image description here

Method2: Type and experiment the code directly in the interactive debugger console. However, there is no easy way to bring the code (multiple lines) back to the editor. The console history is sorted automatically from the newest to oldest. When copied from console history, you have your last line of desired code as the first line in the editor. See below threads, I have the same issue. How to invert console history order in pycharm for copy pasting?

like image 451
Lisa Avatar asked Oct 28 '16 22:10

Lisa


4 Answers

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.

like image 106
Dag Høidahl Avatar answered Oct 16 '22 18:10

Dag Høidahl


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...

like image 42
kpie Avatar answered Oct 16 '22 18:10

kpie


  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)

like image 3
Arthur Tacca Avatar answered Oct 16 '22 16:10

Arthur Tacca


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.

like image 2
matusko Avatar answered Oct 16 '22 17:10

matusko