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
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?
5 Answers
Answers 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.
Answers 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...
Answers 3
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.
Answers 4
Answers 5
Are you just missing the "attach to debugger" button on the regular Python Console? Look on the left hand side and its second from the bottom (the bug icon, above the plus icon). Once you have clicked on it, when you call code in your modules that has breakpoints they will be hit just like if you had run the whole program in the debugger.
0 comments:
Post a Comment