simple ITK in windows container

I am trying to use simpleITK inside the docker windows container. The container is built without any error. But when I run the container, I am getting the following error.

Traceback (most recent call last):

  • File “c:\helloworld.py”, line 1, in *
  • import SimpleITK as sitk*
  • File “C:\Python\lib\site-packages\SimpleITK_init_.py”, line 18, in *
  • from SimpleITK.SimpleITK import **
  • File “C:\Python\lib\site-packages\SimpleITK\SimpleITK.py”, line 13, in *
  • from . import _SimpleITK*
    ImportError: DLL load failed while importing _SimpleITK: The specified module could not be found.

Here is my Dockerfile

FROM python:3.9-windowsservercore
COPY helloworld.py c:/helloworld.py
COPY requirements.txt c:/requirements.txt
RUN python -m pip install -r c:/requirements.txt
CMD [ “python” ,“c:/helloworld.py”]

Here is the helloworld.py

import SimpleITK as sitk
print(“Hello World !!!”)
img = sitk.ReadImage(’./input/sample.mha’)
print(img.GetDimension())
print(img.GetSize())

Possibly "C:\Python\lib\site-packages" is not in the PYTHONPATH, add it:

set PYTHONPATH=%PYTHONPATH%;C:\Python\lib\site-packages\
1 Like

Thanks. I set the environment path. I still get this error when running the container.

**import SimpleITK**

File "C:\Python\lib\site-packages\SimpleITK\__init__.py", line 18, in <module>

``

from SimpleITK.SimpleITK import *

File "C:\Python\lib\site-packages\SimpleITK\SimpleITK.py", line 13, in <module

>

from . import _SimpleITK

ImportError: DLL load failed while importing _SimpleITK: The specified module co

uld not be found.

It looks like it’s not finding the SimpleITK shared library. There should be a ‘.pyd’ file in that same directory, C:\Python\lib\site-packages\SimpleITK`.

I wrote a little script to inspect my SimpleITK installation:

import os
import inspect
import SimpleITK as sitk

sitk_file = inspect.getfile(sitk)
print(sitk_file)

sitk_dir = os.path.dirname(sitk_file)
print(sitk_dir)

print(os.listdir(sitk_dir))

And here’s what the output looks like on my Window system:

C:\Users\dchen\anaconda3\envs\sitkpy\lib\site-packages\SimpleITK\__init__.py
C:\Users\dchen\anaconda3\envs\sitkpy\lib\site-packages\SimpleITK
['docs', 'extra.py', 'SimpleITK.py', '_SimpleITK.cp39-win_amd64.pyd', '_version.py', '__init__.py', '__pycache__']

It seems like your system isn’t finding that ‘.pyd’ file.

thanks,
It helped. I checked the file was available under c:/Python/lib/site-packages/SimpleITK
I also installed the vc++ using the following command.
RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest "https://aka.ms/vs/17/release/vc_redist.x64.exe" -OutFile "vc_redist.x64.exe";
Start-Process -filepath C:\vc_redist.x64.exe -ArgumentList “/install”, “/passive”, “/norestart” -Passthru | Wait-Process; `
Remove-Item -Force vc_redist.x64.exe;

ENV PYTHONPATH=%PYTHONPATH%;C:/Python/lib/site-packages/

I am able to run the application inside the docker container.
Thanks again.

3 Likes

Ahh, the windows docker containers are missing a needed MSVC C++ runtime libraries required by the loading of the SimpleITK shared library.

Thanks for posting the solution!

1 Like