Intellij Pycharm Plugin PyExecuteInConsole synchronization




I'm writing a Pycharm plugin that executes commands in Python console. For that, I'm using, for example:

PyExecuteInConsole.executeCodeInConsole(prj, "import math", null, true, true, false, null);

That works fine, when the console is already opened and the python interpreter has started. However, if no Python Console is active, the PyExecuteInConsole() starts a new one, but does not execute the:

"import math"

because, the python interpreter needs a little time to initiate.

My question is: How do I wait for the new Python Console to start and be ready, before I call the PyExecuteInConsole() ?





Hi! By design, it should execute the code in the newly started console (it works as expected in `com.jetbrains.python.actions.PyExecuteSelectionAction`). Are you sure it wasn't executed? Sometimes parts of the command are hidden inside the folding. Could you please check one more time that your command and its result aren't hidden inside the green area of text "Python 3.10.6 ... "


Hi Elizabeth,


thanks for the quick response, you're right, the code was hidden, but executed.

However, this still does not solve my problem - I send more commands sequentially to the console, and need it to be the same console - same python context.

If no console is opened, and I call the following:

PyExecuteInConsole.executeCodeInConsole(... cmd1 ...);
PyExecuteInConsole.executeCodeInConsole(... cmd2 ...);

Pycharm opens two consoles: one console for cmd1 and the second one for cmd2. I guess it's because the PyExecuteInConsole.executeCodeInConsole() executes asynchronously, and does not wait, goes further to the second call for cmd2, and it sees there is no console opened and active yet, thus it opens the second one.


So, I rephrase my original question: How do I get the code above sending two commands to the same console? If no console existed, it opens a new one, If one existed, both commands are sent to that existing one...





I got your problem. Well, actually there is a hacky solution (see `PyExecuteInConsole.executeCodeInConsole`, but a private one). It has an argument `executeInStaringConsole`, which detects by virtual file, should the command be executed in the current console which is starting, or should it be executed in a new console. So in your case you can just pass a function that always returns `true` and all the commands will be executed in the same console.

The only problem is that this function is *private* at the moment, so you can't use it right now, unfotunately :( As a temporary solution, you can implement your own `PyExecuteConsoleCustomizer` and define your function there (make sure to make its priority higher than a default one by passing `order="first"` argument inside plugin.xml)

I've also created a task for myself to create a better API:


Thanks again Elizabeth, I'll try your suggestion. It would be sufficient if the PyExecuteInConsole class allowed for defining a callback that would be called once executeCodeInConsole() finishes.




Please sign in to leave a comment.