'ColocZStats' extension install error on Windows

OS: Win 10
Version: 3D Slicer Stable 5.2.2

Hi there,
I downloaded and installed the 3D Slicer stable version 5.2.2 on my windows for the first time.

After installing the ‘ColocZStats’ extension through its built-in ‘Extension Manager’ and restarting Slicer. The ‘ColocZStats’ can’t be found by searching in the ‘Module finder’.

Meanwhile, I got the following error messages in the Python Console:

Traceback (most recent call last):
File "C:/Users/xiang/AppData/Local/NA-MIC/Slicer 5.2.2/NA-MIC/Extensions-31382/ColocZStats/lib/Slicer-5.2/qt-scripted-modules/ColocZStats.py", line 62, in <module>
  from skimage import morphology
ModuleNotFoundError: No module named 'skimage'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\xiang\AppData\Local\NA-MIC\Slicer 5.2.2\lib\Python\Lib\imp.py", line 169, in load_source
  module = _exec(spec, sys.modules[name])
File "<frozen importlib._bootstrap>", line 613, in _exec
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:/Users/xiang/AppData/Local/NA-MIC/Slicer 5.2.2/NA-MIC/Extensions-31382/ColocZStats/lib/Slicer-5.2/qt-scripted-modules/ColocZStats.py", line 64, in <module>
  slicer.util.pip_install("scikit-image")
File "C:\Users\xiang\AppData\Local\NA-MIC\Slicer 5.2.2\bin\Python\slicer\util.py", line 3578, in pip_install
  _executePythonModule('pip', args)
File "C:\Users\xiang\AppData\Local\NA-MIC\Slicer 5.2.2\bin\Python\slicer\util.py", line 3540, in _executePythonModule
  logProcessOutput(proc)
File "C:\Users\xiang\AppData\Local\NA-MIC\Slicer 5.2.2\bin\Python\slicer\util.py", line 3509, in logProcessOutput
  raise CalledProcessError(retcode, proc.args, output=proc.stdout, stderr=proc.stderr)
subprocess.CalledProcessError: Command '['C:/Users/xiang/AppData/Local/NA-MIC/Slicer 5.2.2/bin/../bin\\PythonSlicer.EXE', '-m', 'pip', 'install', 'scikit-image']' returned non-zero exit status 1.
[Qt] loadSourceAsModule - Failed to load file "C:/Users/xiang/AppData/Local/NA-MIC/Slicer 5.2.2/NA-MIC/Extensions-31382/ColocZStats/lib/Slicer-5.2/qt-scripted-modules/ColocZStats.py"  as module "ColocZStats" !
[Qt] Fail to instantiate module  "ColocZStats"
[Qt] The following modules failed to be instantiated:
[Qt]    ColocZStats

The reason seems to be the unsuccessful install/import of ‘skimage’ and ‘morphology’.

Here is how the ‘skimage’ and ‘morphology’ are installed/imported in ColocZStats.py:

try:
    from skimage import morphology
except ModuleNotFoundError:
    slicer.util.pip_install("scikit-image")
    from skimage import morphology

However, when I restart Slicer again, the ‘ColocZStats’ extension can be searched in the ‘Modules finder’ and works normally.

I checked the SlicerStable dashboard, the test output of ColocZStats on Windows seems to show the same information as above: https://slicer.cdash.org/test/24353596

Collecting matplotlib
  Using cached matplotlib-3.7.1-cp39-cp39-win_amd64.whl (7.6 MB)
Requirement already satisfied: pillow>=6.2.0 in d:\d\s\s-0-build\python-install\lib\site-packages (from matplotlib) (9.4.0)
Collecting importlib-resources>=3.2.0
  Using cached importlib_resources-5.12.0-py3-none-any.whl (36 kB)
Requirement already satisfied: numpy>=1.20 in d:\d\s\s-0-build\python-install\lib\site-packages (from matplotlib) (1.23.4)
Collecting contourpy>=1.0.1
  Using cached contourpy-1.0.7-cp39-cp39-win_amd64.whl (160 kB)
Requirement already satisfied: pyparsing>=2.3.1 in d:\d\s\s-0-build\python-install\lib\site-packages (from matplotlib) (3.0.9)
Requirement already satisfied: packaging>=20.0 in d:\d\s\s-0-build\python-install\lib\site-packages (from matplotlib) (23.0)
Collecting cycler>=0.10
  Using cached cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting kiwisolver>=1.0.1
  Using cached kiwisolver-1.4.4-cp39-cp39-win_amd64.whl (55 kB)
Collecting fonttools>=4.22.0
  Using cached fonttools-4.39.2-py3-none-any.whl (1.0 MB)
Collecting python-dateutil>=2.7
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting zipp>=3.1.0
  Using cached zipp-3.15.0-py3-none-any.whl (6.8 kB)
Requirement already satisfied: six>=1.5 in d:\d\s\s-0-build\python-install\lib\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
Installing collected packages: zipp, python-dateutil, kiwisolver, fonttools, cycler, contourpy, importlib-resources, matplotlib
  WARNING: The scripts fonttools.exe, pyftmerge.exe, pyftsubset.exe and ttx.exe are installed in 'D:\D\S\S-0-build\python-install\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed contourpy-1.0.7 cycler-0.11.0 fonttools-4.39.2 importlib-resources-5.12.0 kiwisolver-1.4.4 matplotlib-3.7.1 python-dateutil-2.8.2 zipp-3.15.0

[notice] A new release of pip is available: 23.0 -> 23.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
Collecting pandas
  Using cached pandas-1.5.3-cp39-cp39-win_amd64.whl (10.9 MB)
Requirement already satisfied: python-dateutil>=2.8.1 in d:\d\s\s-0-build\python-install\lib\site-packages (from pandas) (2.8.2)
Requirement already satisfied: numpy>=1.20.3 in d:\d\s\s-0-build\python-install\lib\site-packages (from pandas) (1.23.4)
Collecting pytz>=2020.1
  Downloading pytz-2023.2-py2.py3-none-any.whl (502 kB)
     ------------------------------------- 502.1/502.1 kB 10.5 MB/s eta 0:00:00
Requirement already satisfied: six>=1.5 in d:\d\s\s-0-build\python-install\lib\site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)
Installing collected packages: pytz, pandas
Successfully installed pandas-1.5.3 pytz-2023.2

[notice] A new release of pip is available: 23.0 -> 23.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
Collecting xlsxwriter
  Using cached XlsxWriter-3.0.9-py3-none-any.whl (152 kB)
Installing collected packages: xlsxwriter
Successfully installed xlsxwriter-3.0.9

[notice] A new release of pip is available: 23.0 -> 23.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
Collecting tifffile
  Using cached tifffile-2023.3.21-py3-none-any.whl (218 kB)
Requirement already satisfied: numpy in d:\d\s\s-0-build\python-install\lib\site-packages (from tifffile) (1.23.4)
Installing collected packages: tifffile
  WARNING: The scripts lsm2bin.exe, tiff2fsspec.exe, tiffcomment.exe and tifffile.exe are installed in 'D:\D\S\S-0-build\python-install\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed tifffile-2023.3.21

[notice] A new release of pip is available: 23.0 -> 23.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
Collecting scikit-image
  Using cached scikit_image-0.20.0-cp39-cp39-win_amd64.whl (23.9 MB)
Requirement already satisfied: packaging>=20.0 in d:\d\s\s-0-build\python-install\lib\site-packages (from scikit-image) (23.0)
Requirement already satisfied: tifffile>=2019.7.26 in d:\d\s\s-0-build\python-install\lib\site-packages (from scikit-image) (2023.3.21)
Collecting networkx>=2.8
  Using cached networkx-3.0-py3-none-any.whl (2.0 MB)
Collecting lazy_loader>=0.1
  Using cached lazy_loader-0.2-py3-none-any.whl (8.6 kB)
Collecting imageio>=2.4.1
  Using cached imageio-2.26.1-py3-none-any.whl (3.4 MB)
Collecting scipy<1.9.2,>=1.8
  Using cached scipy-1.9.1-cp39-cp39-win_amd64.whl (38.6 MB)
Requirement already satisfied: pillow>=9.0.1 in d:\d\s\s-0-build\python-install\lib\site-packages (from scikit-image) (9.4.0)
Collecting PyWavelets>=1.1.1
  Using cached PyWavelets-1.4.1-cp39-cp39-win_amd64.whl (4.2 MB)
Requirement already satisfied: numpy>=1.21.1 in d:\d\s\s-0-build\python-install\lib\site-packages (from scikit-image) (1.23.4)
Installing collected packages: scipy, PyWavelets, networkx, lazy_loader, imageio, scikit-image
  Attempting uninstall: scipy
    Found existing installation: scipy 1.9.2
    Uninstalling scipy-1.9.2:
      Successfully uninstalled scipy-1.9.2
ERROR: Could not install packages due to an OSError: [WinError 5] Access is denied: 'D:\\D\\S\\S-0-build\\python-install\\Lib\\site-packages\\~cipy\\_lib\\_ccallback_c.cp39-win_amd64.pyd'
Consider using the `--user` option or check the permissions.


[notice] A new release of pip is available: 23.0 -> 23.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
Traceback (most recent call last):
  File "D:/D/S/S-0-E-b/ColocZStats-build/lib/Slicer-5.2/qt-scripted-modules/ColocZStats.py", line 62, in <module>
    from skimage import morphology
ModuleNotFoundError: No module named 'skimage'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "D:\D\S\S-0-build\python-install\Lib\imp.py", line 169, in load_source
    module = _exec(spec, sys.modules[name])
  File "<frozen importlib._bootstrap>", line 613, in _exec
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "D:/D/S/S-0-E-b/ColocZStats-build/lib/Slicer-5.2/qt-scripted-modules/ColocZStats.py", line 64, in <module>
    slicer.util.pip_install("scikit-image")
  File "D:\D\S\S-0-build\Slicer-build\bin\Python\slicer\util.py", line 3578, in pip_install
    _executePythonModule('pip', args)
  File "D:\D\S\S-0-build\Slicer-build\bin\Python\slicer\util.py", line 3540, in _executePythonModule
    logProcessOutput(proc)
  File "D:\D\S\S-0-build\Slicer-build\bin\Python\slicer\util.py", line 3509, in logProcessOutput
    raise CalledProcessError(retcode, proc.args, output=proc.stdout, stderr=proc.stderr)
subprocess.CalledProcessError: Command '['D:/D/S/S-0-build/python-install/bin\\PythonSlicer.EXE', '-m', 'pip', 'install', 'scikit-image']' returned non-zero exit status 1.
loadSourceAsModule - Failed to load file "D:/D/S/S-0-E-b/ColocZStats-build/lib/Slicer-5.2/qt-scripted-modules/ColocZStats.py"  as module "ColocZStats" !
Fail to instantiate module  "ColocZStats"
The following modules failed to be instantiated:
   ColocZStats

What exactly should I do to fix it?

In addition, according to the SlicerStable dashboard, there are two tests of ColocZStats on MacoOS failed. The ‘Details’ shows ‘Timeout’, and the ‘Summary’ shows ‘Unstable’. The following are the test outputs: https://slicer.cdash.org/viewTest.php?onlyfailed&buildid=2979269

py_ColocZStats:

Collecting pandas

py_nomainwindow_qSlicerColocZStatsModuleGenericTest:

Collecting matplotlib

What is the specific meaning of these outputs? Does it have any impact on how the extension works?

Thank you in advance for your help, I appreciate it!

The module is implemented incorrectly. It uses pip_install to install many Python packages in the global scope (when the module is loaded). Instead, packages should be installed as late as possible, i.e., when they are actually used.

Installation of the skimage package fails due to the recent rename, which prevents the package from loading.

Please send a pull request to the extension’s repository with the proposed fix. If you don’t get a response within 1-2 weeks then let us know and we’ll see what we can do.

1 Like

Hello @lassoan,

It uses pip_install to install many Python packages in the global scope (when the module is loaded). Instead, packages should be installed as late as possible, i.e., when they are actually used.

Thank you for your kind reminder. I’m the developer of the module, and I have made the necessary changes to it by relocating the installation of most Python packages from the global scope to where they are actually used.

And I have avoided the error I mentioned by simply removing the unnecessary code related with skimage, cause I think for now I don’t need it anymore.

However, could you please provide more details about the reseason for the skimage package installation failure you mentioned earlier? :

Installation of the skimage package fails due to the recent rename, which prevents the package from loading.

This would be helpful in case I need to use it in the future.
Thank you very much for your assistance!