Extension adding an entry to Slicer-XYZ.ini [PYTHONPATH]


SlicerOpenCV currently installed cv2.pyd to the root directory, and currently import cv2 fails. A fix to this is to install it to the qt-scripted-modules folder, but I was wondering if there was any interest in adding the ability for an extension to add entries to the Slicer-XYZ.ini sections?

For example, the extension could add a path entry to the [PYTHONPATH] section.


code of interest:

Alternately, an extension could do this manually in a module’s init function, accessing the qSlicerExtensionsModelManager functionality.

Edit: uninstalling the extension wouldn’t remove the entry though, so this is sub-optimal.

Any scripted module that calls import cv2 could add the dll’s path to Python paths, but this, too, would be just a workaround.

For me, the main question is:

  • why cv2.pyd is installed into 26876-win-amd64-SlicerOpenCV-git8ea8ff1-2018-01-29\lib\Slicer-4.9\cv2.pyd instead of in a scripted module directory? (scripted module directories are already added to PYTHONPATH section)
  • if cv2.pyd current location is preferable for some reason, then why not add that location to PYTHONPATH section as well?

@jcfr Could you comment on these?

Not a perfect answer to your first point, but the extension packaging would have to assume that the directory is “qt-scripted-modules”. Probably a reasonable assumption, but if the scripted directory ever changed, the extension would break (extremely unlikely scenario, I agree).

At the moment, point two is not possible, and was the goal of this discussion.

@fedorov @Nicole_Aucoin any thoughts?

I recall working with @jcfr on this problem during OpenCV integration and I think that this bug was the reason why the opencv library was installed where it was. I can’t dig up the details though.


Adam, I don’t have answers to your questions. But to me it seems suboptimal to modify those variables from the module code.

The alternative is installing cv2.pyd to qt-scripted-modules.

Edit: or implementing a system for extensions to install/uninstall entries to the settings file.

Maybe it could be simpler to have settings aggregated from a folder: Slicer.(app,exe) would look in that folder and append search settings from all the .ini files contained within. I think this would mesh well with the extension manager, and could also make local developer workflow nicer: just symlink .ini files into the aggregate folder from local extension build trees.

Here’s the commit where I talked about trying to get python wrapping and importing to work:

It referenced:

1 Like

Thanks @Nicole_Aucoin! I added those pointers to the issue.

Since r25959 introduced in April 2017, extension can already package python modules and packages using PYTHON_SITE_PACKAGES_SUBDIR CMake variable to specify the install destination.

The packages found in this sub directory will be importable.

I suggest to change this line:

1 Like

Thank you @jcfr for the suggestion, and @adamrankin for the PR!

The fix is now merged in https://github.com/SBU-BMI/SlicerOpenCV/pull/50. We shall see, tomorrow.

As a followup, the suggestion above was implemented, but did not fix the import cv2 issue.

What did you do exactly?

Do you create an extension package and install it or you run from the build tree? If you run from the build tree: did you specify the additional launcher settings ini file on the command line when you started Slicer?

What did you do exactly?

I downloaded the nightly package, installed SlicerOpenCV extension, and did import cv2 in the python console.

import cv2 works well on Windows (latest nightly).

Do you have this problem on Mac? Maybe it’s the same Apple SIP policy issue that plagues so many other things - see for example Tutorial for using pyradiomics, no module named _collections.

Yes, I tested on mac