How to Rerun Script Using Keyboard Shortcut in Same Tab


First, my "Run/Debug Configurations" under Execution has "Run with Python console" set.

The problem: I want to rerun the Python script in the SAME Python Console tab WITHOUT having to use my mouse to click the rerun icon in the toolbar area of the Python Console window.

What I want is to have my cursor in the script window, press F5, and have the script rerun in the same tab in the Python Console.

What's happening currently is that a new tab is popping up each time I run the app.

How to configure so I can rerun the project using a keyboard shortcut F5 in the same Python Console tab (without making more tabs)?


Comment actions Permalink


You need to make sure Use existing console for "Run with Python console" is enabled under Preferences | Build, Execution, Deployment | Console

Comment actions Permalink

Thanks Sergey,

I made an unlisted video here (which will be removed later):

The main problem is, when my caret cursor is in the scripting area (not the Python console area), and I press F5 to rerun, nothing happens.

I map F5 to "Rerun", the Python Console shows F5 is mapped to Rerun, but rerun only works when the caret cursor is either focused on the Python Console, OR when the Rerun button is clicked with the mouse pointer.

When I enable "Use existing console for Run with Python Console", F5 still doesn't work in the scripting area ( file code area), and still the only way I've been able to rerun is the rerun button on the Python Console. With this feature enabled, when I Run (not rerun) the program multiple times, the processes running stacks up so that's not the behavior I'm going for.

I simply want to be able to map F5 to the Rerun command, and while the caret cursor is focused within the scripting area, make a quick change to code, then press F5, then all processes are killed and the script is re-ran in the same tab.

I clearly must be doing something wrong but I don't know what. I really want to figure this out because as simple as moving my hand to my mouse is and clicking rerun, doing that a lot adds up to a lot of time wasted.

Comment actions Permalink

Update: I was able to get functionality I was looking for except it's not within the Python Console.

Step 1: In Run/Debug Configurations for the project, UNCHECK "Run with Python console" Reason: you can't use Python Console to rerun with keyboard shortcut F5 but you can when not using Python Console.

Step 2: Keymap "Run context configuration" or just "Run" to F5

Step 3: Remove F5 from other currently assigned commands


Downside: no Python Console. Just ends with "Process finished with exit code 0"


In any event, I think this is the best I can do at the moment. It's fine, but would like Python Console and F5 keyboard shortcut to work with rerun.

Comment actions Permalink

In fact, invoking the shortcut for Run should be enough. It's not necessary to uncheck "Run with Python console".

So you just hit Shift+F10 (the default shortcut for Run on Windows), then you make changes to the code and hit Shift+F10 again. If Use existing console for "Run with Python console" is enabled, then you will be able to rerun it in the same tab.

The problem is that you used Rerun action which works inside the Python Console only. So in case you keep the cursor in the editor, you need to use Run action.

I hope my description is clear.

Comment actions Permalink

When I:

  1. keep "Run with Python console" checked
  2. restore all default keymaps to their original mappings
  3. keep "Use existing console for "Run with Python console" checked
  4. put my caret cursor focused in my python file
  5. press SHIFT+F10, literally nothing happens
  6. then I map Run to F5 (instead of SHIFT+F10)
  7. put caret cursor focused in my python file
  8. press F5, it runs and works
  9. but here's the problem when I'm coding GUI (PyQt5)... the GUI program is opened currently
  10. I make changes to the code and press F5 again
  11. Now at the info bar at the bottom, I see "Executing code in Console..." and a blue progress bar has appeared
  12. Take-away: it didn't kill the previously opened GUI and restart. This causes processes to stack up as "Background tasks"
  13. So, running with Run (regardless of what keyboard keys are used), when "Use existing console for "Run with Python console"" set WILL run in the same tab, but causes processes to stack up instead of killing the previous process.

Anyways, I don't think this will be resolved in this thread despite your excellent help and tips. Unless there is a setting that uses the same Python console and restarts it when "Run" is invoked, this can't be solved. I can just work around it.

Thanks for the tip on the Rerun action. I wasn't aware of that it only works inside the Python console.

Comment actions Permalink

I see. So there are basically two problems:

1) GUI occupies the Python process and won't let you rerun the script unless you terminate it. That's why you see Executing code in Console...

2) Restart of Python Console (killing the process) would mean that it actually not using existing console as per Use existing console for "Run with Python console"

I will discuss it with our developer to see if there is anything we can do about it. Maybe we'll need to submit a feature request to cover that case.

Comment actions Permalink

Ok, I just checked it with our dev. Do you know if PyQt supports the scenario when one changes the code and GUI application reloads to pick up those changes?
If yes, then we should indeed file a feature request to support it.
If not, then we can't really do anything about it since GUI app occupies the python process and you should either close it to free the process and let PyCharm use the same Python Console or uncheck Use existing console for "Run with Python console" to let PyCharm create a new Python Console for each run.

Comment actions Permalink

I don't know if PyQt supports the scenario you mentioned.

I think at the heart of all this, the simplest solution would be to give users the ability to use a keyboard shortcut while in the coding window that will Rerun (reload entirely) the Python console process. This functionality exists - it's just that a user has to physically move their mouse to the Rerun button on the Python console toolbar. If we (I) could execute this command with my cursor in the coding window, it would solve all the problems discussed in this thread.

Comment actions Permalink

Agree, having this button inside the editor would solve this problem, but it may be confusing for those users who don't need it.
However, please feel free to submit a feature request about it (or about a shortcut to Rerun console from the editor) to our issue tracker using the link

It seems that the only solution to work without using the mouse when working with GUI apps is to use the following sequence:

1) Run the file in Python Console

2) Switch back to PyCharm if the opened GUI app has stolen the focus (or close the app)

3) Switch to Python Console with a shortcut which you can assign in Keymap settings:

4) Once focus is back in the Python Console, use Rerun shortcut.

EDIT: If you assign a shortcut to Run File in Console you can skip the 3rd step


Please sign in to leave a comment.