Calling Debugger action (Pycharm)

Answered

Hi,

Is there a way to activate the Python Debugger from a button in a toolbar I've added with a plugin (on the file currently open in the editor)? 
Also, can I programmatically send extra arguments to the debugger?

Thank you,
Shachar

0
14 comments

What does "activate" mean? Activate the already open Debugger tool window? Run an existing run configuration with Debugger enabled?

0

Hi Yann,

I want to start a debugging process, and also send it environment variables. The flow - the user clicks the button in my plugin's panel, and then I start a debugging process with certain environment variables. 
If it matters - the plugin would be used only with PyCharm. 

Thank you,
Shachar

0

Hi Yann,

The docs are helpful, thanks, but it would be great to have a working example there.
My code so far (runs after a button is pressed):

RunnerAndConfigurationSettings runSettings = RunManager.getInstance(project).getSelectedConfiguration();
Executor executor = DefaultDebugExecutor.getDebugExecutorInstance();
ProgramRunner runner = RunnerRegistry.getInstance().findRunnerById(DefaultDebugExecutor.EXECUTOR_ID);
final ExecutionEnvironment env = new ExecutionEnvironmentBuilder(project, executor).runProfile(runSettings.getConfiguration()).build();
try {
runner.execute(env);
} catch (ExecutionException e1) {
e1.printStackTrace();
}

I get Null Pointer Exception when running `runner.execute(env)`

Shachar

0

Please post full stacktrace.

0

java.lang.NullPointerException
at com.x.plugin.ide.automations.ui.actionsPanel.lambda$new$2(actionsPanel.java:218)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6548)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3325)
at java.awt.Component.processEvent(Component.java:6313)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4903)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4725)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2281)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4725)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:764)
at java.awt.EventQueue.access$500(EventQueue.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:715)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:737)
at java.awt.EventQueue$4.run(EventQueue.java:735)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:734)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:719)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:664)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:363)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

row 218 is

    runner.execute(env);
0

Please try using methods from com.intellij.execution.runners.ExecutionUtil which also handle errors. Debug Executor can be obtained from ExecutorRegistry.getInstance().getExecutorById(DefaultRunExecutor.EXECUTOR_ID)

0

Hi Yann, 

Used the Debug Executor you suggested - same error. It would be super helpful to have a simple quick-start on how to create a debug process, in this case, a Python debug process. 
It is good and appreciated that you have the execution docs, but connecting the dots here doesn't really work. Barely found any other plugins who do something in that area, perhaps because it is not clear how to do it.

Would much appreciate your help,
Shachar

0

Hi Shachar! You should select Python Debug Runner in your code by id. The following snippet works fine:

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
final Project project = e.getProject();
if (project == null) return;
RunnerAndConfigurationSettings runSettings = RunManager.getInstance(project).getSelectedConfiguration();
if (runSettings == null) return;
Executor executor = DefaultDebugExecutor.getDebugExecutorInstance();
ProgramRunner runner = ProgramRunner.findRunnerById(PyDebugRunner.PY_DEBUG_RUNNER);
if (runner == null) return;
final ExecutionEnvironment env = new ExecutionEnvironmentBuilder(project, executor).runProfile(runSettings.getConfiguration()).build();
try {
runner.execute(env);
} catch (ExecutionException e1) {
e1.printStackTrace();
}
}
0

Hi Elizabeth,

Thank you for the detailed answer. I adjusted the code a bit since `ProgramRunner` in the code of the IntelliJ dev plugin is a bit different.
Attaching modified code for future reference for people:

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
final String PY_DEBUG_RUNNER = "PyDebugRunner";
final Project project = e.getProject();
if (project == null) return;
RunnerAndConfigurationSettings runSettings = RunManager.getInstance(project).getSelectedConfiguration();
if (runSettings == null) return;
Executor executor = DefaultDebugExecutor.getDebugExecutorInstance();
ProgramRunner runner = RunnerRegistry.getInstance().findRunnerById(PY_DEBUG_RUNNER);
if (runner == null) return;
ExecutionEnvironment env = new ExecutionEnvironmentBuilder(project, executor).runProfile(runSettings.getConfiguration()).build();

try {
runner.execute(env);
} catch (ExecutionException e1) {
e1.printStackTrace();
}
}

One more thing - I want to add an environment variables to the run configuration of that debug. How can I add them?

Thank you!
Shachar

0

Elizabeth can I add the Python plugin as a dependency of the project? Didn't find it in Maven. If so perhaps I can use PythonRunConfiguration to add the environment variables

0

Yes, you can add dependency on module (unfortunately, Python Debugger isn't a part of api):

intellij.python.community.impl

Then create your custom runner and insert your env variables like here: https://github.com/JetBrains/intellij-community/blob/38025343b699405ac6b7b679b2916de56b4fb460/python/src/com/jetbrains/python/debugger/PyDebugRunner.java#L325

0

 Hi Elizabeth,

intellij.python.community.impl

Is not part of any gradle repository. Do you mean that I should copy those files into my plugin as a local module and then add the dependency on them?

0

Hi! Sorry, I gave you a wrong info. If you want to use classes form Python, you should download Python plugin, unpack it and add its libraries to your Sdk. You can read move information here: https://github.com/jansorg/pycharm-webinar-base (from our webinar about creating PyCharm plugins), section "7. Now download the Python plugin to finish the setup of your project's SDK.". 
After that you'll be able to import, for example, `com.jetbrains.python.debugger.PyDebugRunner`.

0

Please sign in to leave a comment.