user9811991
user9811991

Reputation: 335

PyCharm focus Python Console for input upon run

In PyCharm 2018.2.5 (Community Edition), I have bound a keyboard shortcut to "Run File in Console". This launches opens the console and runs the file, but does not focus the console for input and anything I type goes into my code. (Sometimes when I use the switcher to focus the Python console it will try to search console variables when I type.)

Is there a way to automatically focus the Python console for input when it is launched?

Upvotes: 9

Views: 3666

Answers (7)

Patrick Mahoney
Patrick Mahoney

Reputation: 33

I found the option to do this in Run/Debug Configuration. There is "Modify options" which has "Focus run/debug tool when started"

This is with PyCharm 2023.2.5 (Community Edition) on macOS

Upvotes: 1

Patrick Papenhoff
Patrick Papenhoff

Reputation: 1

Within the 'Execute NppExec Script :

  • Insert the NPP_CONSOLE 0 command somewhere at the beginning to hide the console.
  • Then insert the NPP_CONSOLE 1 command somewhere before the python command to show the console again. The console will get and keep the focus.

Example :

NPP_CONSOLE 0
NPP_SAVE
NPP_CONSOLE 1
cd "$(FULL_CURRENT_PATH)"
C:/dev/Python/Python3/python.exe -u "$(FULL_CURRENT_PATH)"

Upvotes: 0

Dannie
Dannie

Reputation: 1

It's very easy! Just make macros with two actions:

1 - Run command,

2 - Activate run tool window. And with keymap in settings create a shortcut. For example, I do this with shift + f10 .

Upvotes: 0

Sujit Vasanth
Sujit Vasanth

Reputation: 31

Yes it IS possible

  1. in the run console change the view mode to Window (click the cog)
  2. add the following lines to the benning of your source code
import pygetwindow as gw
win = gw.getWindowsWithTitle('Run')[0]
win.activate()

now it will automatically give focus to the run window... see my youtube demo.. https://www.youtube.com/watch?v=MhmltlUjU3k

Upvotes: 1

IF.Francisco.ME
IF.Francisco.ME

Reputation: 146

i have just posted something related to this. It is needed to distinguish between "Python console" and "python or debug console" within the keymap options. The first one focus at the console but without automatically move caret to it, the later does it.

See more detailes in this answer.

Upvotes: 0

jay123
jay123

Reputation: 63

No for PyCharm there is no way to automatically focus the Python console for input when it is launched.

In Notepad ++ i can do the following.

  1. Enter my code
  2. press shift+a to execute a macro which saves the file and execute it with Python 2.7 (or 3). I also made this macro a menu item.
  3. When the console runs and the script asks for input you are already in focus and can directly put in our entry without having to click and highlight the window or so as it is necessary in PyCharm.

So the step between writing the code and executing it to see what it does exactly one hotkey. Its not one hotkey and a mouse click or what ever no.... its exactly one press of a button. This makes learning very effective because there are no detours.

I looked long in PyCHarm but i could not find this functionality or a workaround. So the answer to your question is "With PyCharm this is not possible."

Here is how to do it in Notepad ++.

  • Download Notepad ++
  • In N++ install the NppExec Plugin.
  • Create the "save and run in python" macro: In NppExec create a script as shown here:

    NPP_CONSOLE 0 
    npp_save 
    npp_run cmd /K C:\Python27\python.exe "$(FULL_CURRENT_PATH)"
    
  • Save your macro script as say "Save and run in Python Selfmade"
  • In the hotykeys menu assign your macro to a new hotkey.
  • In the Npp_Exec menu click on Advanced Options and create a menu entry for your macro. To do this select the script you created under "Associated Script" and then click on "Add/Modify"

Another editor that offers you this quick and direct run functionality is "Atom" with its many plugins.

Upvotes: 4

mhoffman2016
mhoffman2016

Reputation: 100

The shortcut to switch between the code and console is Alt+4; by clicking on the console or using this shortcut, future consoles you open will be selected by the input.

Upvotes: 2

Related Questions