Access slicer modules and libraries in pycharm

Hi,
I’d like to know why when I try to call slicer modules\libraries (vtk, numpy), they are not recognized, unless I’m in the remote-debug mode, and run some loadable module, while there is a breakpoint somewhere in the code:

image

image

What path should I add to the python search path? and how it is different in the different modes?

Thanks!!

Slicer is not a Python interpreter extended by Python libraries, but an application that embeds Python. The difference is explained here: https://docs.python.org/2/extending/embedding.html

The consequence is that you cannot run Slicer from any generic Python interpreter, such as the one started by the PyCharm IDE. You can only load some of the libraries, such as VTK or SimpleITK into any Python interpreter (and it should not be too difficult to make the core MRML library and some other parts loadable as well). However, in general, to access all Slicer classes, you always need to start Slicer application and then connect to Slicer’s built-in Python interpreter.

There may still be some other modules we need to exclude, but the following PR allows to safely import slicer in the regular python(.exe) binary included with Slicer:

However, note that the python binary must either be started from the launcher or from a shell with with the necessary environment variables.

Thanks for the information @ihnorton, this will be very useful.

I see that some Slicer Qt widget modules had to be excluded. Do you know why they caused crash?

Can we still access MRML widgets and various custom Qt widgets defined in Slicer modules?

PythonQt shared libraries try to access PythonQt::self singleton (or something with a similar name) during shared library loading, but it is not initialized.

Can we still access MRML widgets and various custom Qt widgets defined in Slicer modules?

No, right now anything Qt has to be excluded. There is a discussion on the PythonQt forum about what would be required to use PythonQt that way – we would need a wrapper shim that initializes PythonQt and sets up the event loop. For now my use-case is Python-based CLI.