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

**URL:** <https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976>\
**Category:** Development\
**Tags:** extensions-manager, python\
**Created:** [January 30, 2018, 4:18pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976 "2018-01-30T16:18:09Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [January 30, 2018, 4:18pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/1 "2018-01-30T16:18:09Z")

</div>

Hello,

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.

Thoughts?  
Adam

code of interest:  
[qSlicerExtensionsManagerModelPrivate::addExtensionPathToLauncherSettings](http://qSlicerExtensionsManagerModelPrivate::addExtensionPathToLauncherSettings)  
[qSlicerExtensionsManagerModelPrivate::extensionPythonPaths](https://github.com/Slicer/Slicer/blob/master/Base/QTCore/qSlicerExtensionsManagerModel.cxx#L737)

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [January 30, 2018, 4:33pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/2 "2018-01-30T16:33:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 30, 2018, 5:12pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/3 "2018-01-30T17:12:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [January 30, 2018, 5:30pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/4 "2018-01-30T17:30:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [January 30, 2018, 5:31pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/5 "2018-01-30T17:31:31Z")

</div>

@fedorov @Nicole_Aucoin any thoughts?

---

<div class="post-metadata">

**Author:** ![Nicole\_Aucoin](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Nicole\_Aucoin](https://discourse.slicer.org/u/Nicole_Aucoin)\
**Post date:** [January 30, 2018, 5:55pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/6 "2018-01-30T17:55:05Z")

</div>

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.

Nicole

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [January 30, 2018, 6:03pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/7 "2018-01-30T18:03:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adamrankin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/adamrankin/32/155_2.png) [@adamrankin](https://discourse.slicer.org/u/adamrankin)\
**Post date:** [January 30, 2018, 6:08pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/8 "2018-01-30T18:08:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ihnorton/32/9_2.png) [@ihnorton](https://discourse.slicer.org/u/ihnorton)\
**Post date:** [January 30, 2018, 6:25pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/9 "2018-01-30T18:25:54Z")

</div>

> [@adamrankin](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Nicole\_Aucoin](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Nicole\_Aucoin](https://discourse.slicer.org/u/Nicole_Aucoin)\
**Post date:** [January 30, 2018, 6:33pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/10 "2018-01-30T18:33:08Z")

</div>

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

> <https://github.com/Slicer/SlicerOpenCV/commit/fdf1dde83d60b721273f486df0f227b5005f7dff>
>
> Turn on wrapping OpenCV in python.
> This produces the cv2.so python package that …can be imported.
> 
> Use the OpenCV CMake var -DPYTHON2\_PACKAGES\_PATH to specify that the
> OpenCV python package needs to be installed in the third party library directory,
> it will be found there by changes made to the AdditionalLauncherSettings.ini file
> in Slicer svn 25305. To get packaged, it has to be in the Slicer third party
> library install directory.
> 
> Adapted the opencv python example that computes the histogram of an input image to provide a self test.
> Uploaded a copy of one of the Slicer Pathology sample slices to midas and download it for this test.
> 
> Don't install the ctest command line test on windows as it's not finding the python wrapped
> library when the test is run from the build directory. It still runs from the Testing module
> gui.
> 
> Issue #12

It referenced:

> **[GitHub - NA-MIC/viewvc.slicer.org: Documents where source code originally...](https://github.com/NA-MIC/viewvc.slicer.org?view=revision&revision=25305)**
>
> Documents where source code originally available at viewvc.slicer.org has been archived. - GitHub - NA-MIC/viewvc.slicer.org: Documents where source code originally available at viewvc.slicer.org h...

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [January 30, 2018, 9:21pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/11 "2018-01-30T21:21:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [January 31, 2018, 4:14am UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/12 "2018-01-31T04:14:34Z")

</div>

Since [r25959](http://viewvc.slicer.org/viewvc.cgi/Slicer4?view=revision&revision=25959) 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.

> <https://github.com/Slicer/Slicer/blob/5d294412119bf51f14c3a19ec98f055c1cbe30fe/Base/QTCore/qSlicerExtensionsManagerModel.cxx#L735-L751>

I suggest to change this line:

> <https://github.com/SBU-BMI/SlicerOpenCV/blob/74e0f836bdc284e3b73e541c8d770808ccadec55/SuperBuild/External_OpenCV.cmake#L103>

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [January 31, 2018, 10:39pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/13 "2018-01-31T22:39:00Z")

</div>

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](https://github.com/SBU-BMI/SlicerOpenCV/pull/50). We shall see, tomorrow.

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [February 19, 2018, 6:14pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/14 "2018-02-19T18:14:38Z")

</div>

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

> <https://github.com/SBU-BMI/SlicerOpenCV/blob/master/SuperBuild/External_OpenCV.cmake#L103>

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 19, 2018, 11:05pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/15 "2018-02-19T23:05:36Z")

</div>

> [@fedorov](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [February 19, 2018, 11:21pm UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/16 "2018-02-19T23:21:44Z")

</div>

> What did you do exactly?

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 20, 2018, 1:16am UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/17 "2018-02-20T01:16:45Z")

</div>

`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](https://discourse.slicer.org/t/tutorial-for-using-pyradiomics-no-module-named-collections/2111/7).

---

<div class="post-metadata">

**Author:** ![fedorov](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fedorov/32/14_2.png) [@fedorov](https://discourse.slicer.org/u/fedorov)\
**Post date:** [February 20, 2018, 4:11am UTC](https://discourse.slicer.org/t/extension-adding-an-entry-to-slicer-xyz-ini-pythonpath/1976/18 "2018-02-20T04:11:17Z")

</div>

Yes, I tested on mac
