16

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?

Community
  • 1
  • 1
Lisa
  • 4,126
  • 12
  • 42
  • 71
  • 1
    Just use the previously mentioned Evaluate Expression (Alt+F8) window in Code Fragment Mode and copy/paste whatever code you like there. – heenenee Mar 30 '17 at 06:26
  • It isn't answer to the question, but can be helpful. [![enter image description here](https://i.stack.imgur.com/HK3mK.png)](https://i.stack.imgur.com/HK3mK.png) – ADR Apr 04 '17 at 23:03

4 Answers4

9

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.

Dag Høidahl
  • 7,873
  • 8
  • 53
  • 66
  • from my screenshot you can see that i'm already in debug mode. But when pressing alt+shift+E, it still runs in the regular python console. I have to minimize the regular python console, and press alt+shit+E again to run it in debug console. – Lisa Nov 18 '16 at 05:18
  • When I tested this, `Alt+Shift+E` would send the selection to the currently active console. So when you have several consoles open, you first need to select the one you want to target. – Dag Høidahl Nov 18 '16 at 15:29
  • Still struggling. I set the debug point, debug the code, and select the code, it loaded to the python console instead of debugger console by default.I have to minimize the python console, then run it again and sometimes it runs in debugger console. What did you fo for "select the one you want to target?" you click the debugger? the select the code and press atl+shift+e? – Lisa Mar 28 '17 at 13:55
  • Yes, that's what I did. However, I can't get it to work myself now. Whenever the debugger is active and I select some code in an editor and try to execute it, the Python Console area flickers but doesn't update. I've updated my PyCharm version since last time, so maybe it has stopped working. – Dag Høidahl Mar 28 '17 at 14:26
  • @ Dag Høidahl did you find the alternative way to debug? – Lisa Mar 28 '17 at 14:28
  • What are you actually trying to achieve? You can paste the code into the console, or execute the code by selecting it and run Evaluate Expression (`alt+F8`). Does that help you? – Dag Høidahl Mar 28 '17 at 14:28
3

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

kpie
  • 9,588
  • 5
  • 28
  • 50
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)

Arthur Tacca
  • 8,833
  • 2
  • 31
  • 49
  • I tried, but the behavior is not always consistent. When I place the debug point inside a function, the variable cannot be accessed through the python console. However, if the debug point is at the main, some of the variables are accessible, but not dataframes. – Lisa Apr 05 '17 at 13:28
  • Is there a chance you have attached the debugger after importing the module? – Arthur Tacca Apr 05 '17 at 13:42
  • would you edit your answer give more details? Do you load the code into python console? or debugger? do you run under debug mode? I want to make sure your answer helps more people – Lisa Apr 05 '17 at 13:44
  • I added a bit more detail. Hopefully it's a bit clearer. – Arthur Tacca Apr 05 '17 at 16:41
2

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.

Community
  • 1
  • 1
matusko
  • 3,487
  • 3
  • 20
  • 31