Integrate pycharm and Slicer

Operating system: Arch Linux
Slicer version: newest
Expected behavior: working debugging, want to use imports in pycharm and code completion
Actual behavior: debugging doesn’t work, can’t import slicer or vtk or qt in pycharm (code completion)

Hello,

I work 2 years with Slicer and I made some modules but I can’t never integrate pycharm and Slicer-SuperBuild with 100%. What should I do? Because when I add vtk, qt, slicer paths to pycharm interpreter path that doesn’t work. Is it possible? And I can’t run debugging process. When I click connect in Slicer(debugging plugin) then nothing happen: Slicer doesn’t freeze.
Can’t find any relevant tutorial in Internet. I appreciate any suggestion.

Thank you
Darek

Hello Darek,

I use PyCharm to debug Slicer on a daily basis. I run it on Windows, but I think it should work the same way, because python should be the same.
There are instructions on how to set up Slicer and PyCharm on the website of the DebuggingTools extension:
https://www.slicer.org/wiki/Documentation/Nightly/Extensions/DebuggingTools
Some important steps to do before you can use the debugger:

  • In PyCharm, set up and start a “Python Remote Debug” server on localhost with a port number e.g. 5678
  • In Slicer specify the egg file of PyCharm in the Python Debugger module, and set the same port number
    If the connection is successful, you will see a note in the PyCharm debugger console. And break points should work after that (no need to restart anything).

Code completion usually doesn’t work in the PyCharm code editor main window. The reason is that some python modules are loaded in runtime, so there is no way for PyCharm to know at any time what python modules are available. But if you pause the program execution with a breakpoint, you can use the debugger console to type code, and code completion will work correctly. That is because PyCharm knows at that time what modules are currently loaded.

I hope this helps,
Tamas

4 Likes

@ungi
‘If the connection is successful’

Are there troubleshooting resources when the connection is unsuccessful?

Hi Amy,
I switched to VS Code a few years ago and I’m not using PyCharm anymore. There are some troubleshooting suggestions on this page, but I don’t remember running into such issues: SlicerRt/SlicerDebuggingTools: Extension for 3D Slicer containing various tools for module development and debugging
If you are considering switching to VS Code too, I can highly recommend it and happy to give you a demo too.
Tamas

I was previously using vs code for other python projects- I would gladly take the demo offer!

Thank you!
Let me know when a zoom call might be convenient for you?

PyCharm developers often break their debugger (happens about once a year). The problem and workaround in their latest version is described here: Problems with Pycharm 2024.3 - Does not connect · Issue #19 · SlicerRt/SlicerDebuggingTools · GitHub

I switched to VS Code a few years ago, too. PyCharm has a little extra convenience that the debugger can automatically reconnect when you restart Slicer (while if you use VS Code then you need to click to connect each time) and the PyCharm debugging console looks more like a regular Python console (while in VS Code the console has a separate single line for input, which takes some time to get used to). However, I switched to VS Code for debugging because I use VS Code for everything else anyway and remote debugging requires PyCharm professional version (you need to pay if you are not student or educator).

Thank-you both! I’ll switch to VSCode via the instructions and follow back

1 Like

Hurray! VSCode was such a more straightforward configuration!

1 Like