# Installing and using Python wrapped Remote Module

**URL:** https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104
**Category:** Beginner Questions
**Tags:** python
**Created:** [July 29, 2019, 1:46pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104 "2019-07-29T13:46:29Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![laqua-stack](https://discourse.itk.org/user_avatar/discourse.itk.org/laqua-stack/32/939_2.png) [@laqua-stack](https://discourse.itk.org/u/laqua-stack)
#### Post date: [July 29, 2019, 1:46pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/1 "2019-07-29T13:46:29Z")

</div>

Hi all,

I tried to build ITK Strain Image Filter [https://github.com/KitwareMedical/ITKStrain](https://github.com/KitwareMedical/ITKStrain) and to install it.  
I used a conda virtual env with Python 3.6 and ITK 5.0.0.post1 from pypy in the environment, a self-build local ITK 5.0.1 and skbuild using Win 10 x64.  
I did “python setup.py install” and it finished without errors. Actually if I “conda list” in the env, there is a package called itk-strain 0.1.0. But I did not manage to import the module to python.  
I tried:  
“from itk import StrainImageFilter” --\> module ‘itk’ has no attribute ‘StrainImageFilter’  
“import itk-strain” --\> syntax-error  
“import itk\_strain” --\> no module itk\_strain  
Actually, I am not aware how the module is finally named.  
I did not finde a module in “help(‘modules’)”, neither did I find a submodule in “dir(‘itk’)”

Is there a specific way to find the correct naming convention of a remote module, wrapped for python? Do I need change something in cmake to enable Python wrapping before I build ITK on my System?

I appreciate any answers and hints.

Kind Regards

---

<div class="post-metadata">

### Author: ![laqua-stack](https://discourse.itk.org/user_avatar/discourse.itk.org/laqua-stack/32/939_2.png) [@laqua-stack](https://discourse.itk.org/u/laqua-stack)
#### Post date: [July 29, 2019, 10:26pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/2 "2019-07-29T22:26:34Z")

</div>

I tried building ITK with wrapping enabled again and I put WrapITK.pth in sitepackages of a the conda-env I am using, added a path variable to build\lib\release and build\bin\release (Now, I do feel a little bit stupid for posting the question above, though 🙂 ) and now I can import StrainPython and have StrainPython.itkStrainImageFilterIVD22DD available. But I ran in the next issue. GetImageFromArray from ITKExtras does not work anymore as it did in ITK from PyPy…

> ## In [19]: deformationField = itk.GetImageFromArray(test.astype(‘float32’), is\_vector=True)
> 
> AttributeError Traceback (most recent call last)  
> in   
> ----\> 1 deformationField = itk.GetImageFromArray(test.astype(‘float32’), is\_vector=True)
> 
> C:\build\Wrapping\Generators\Python\Release\itkExtras.py in GetImageFromArray(arr, is\_vector)  
> 295 “”“Get an ITK image from a Python array.  
> 296 “””  
> → 297 return \_GetImageFromArray(arr, “GetImageFromArray”, is\_vector)  
> 298  
> 299 image\_from\_array = GetImageFromArray
> 
> C:\build\Wrapping\Generators\Python\Release\itkExtras.py in \_GetImageFromArray(arr, function, is\_vector)  
> 272 raise ImportError(‘Numpy not available.’)  
> 273 import itk  
> → 274 PixelType = \_get\_itk\_pixelid(arr)  
> 275 if is\_vector:  
> 276 Dimension = arr.ndim - 1
> 
> C:\build\Wrapping\Generators\Python\Release\itkExtras.py in \_get\_itk\_pixelid(numpy\_array\_type)  
> 225 numpy.float32:itk.F,  
> 226 numpy.float64:itk.D,  
> → 227 numpy.complex64:itk.complex[itk.F],  
> 228 numpy.complex128:itk.complex[itk.D]  
> 229 }
> 
> C:\build\Wrapping\Generators\Python\Release\itkLazy.py in **getattribute** (self, attr)  
> 44 module = self.\_\_belong\_lazy\_attributes[attr]  
> 45 namespace = {}  
> —\> 46 itkBase.LoadModule(module, namespace)  
> 47 for k, v in namespace.items():  
> 48 setattr(self, k, v)
> 
> C:\build\Wrapping\Generators\Python\Release\itkBase.py in LoadModule(name, namespace)  
> 61 else:  
> 62 swig = namespace.setdefault(‘swig’, imp.new\_module(‘swig’))  
> —\> 63 swig. **dict**.update(this\_module.swig. **dict** )  
> 64  
> 65 # don’t worry about overwriting the symbols in namespace – any
> 
> AttributeError: module ‘ITKCommon’ has no attribute ‘swig’

Any suggestions? I am definitly missing somethin, sorry.

---

<div class="post-metadata">

### Author: ![laqua-stack](https://discourse.itk.org/user_avatar/discourse.itk.org/laqua-stack/32/939_2.png) [@laqua-stack](https://discourse.itk.org/u/laqua-stack)
#### Post date: [July 31, 2019, 9:13am UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/3 "2019-07-31T09:13:18Z")

</div>

Okay, i tried back and forth, even rebuild itk with python wrapping. And I experienced a rather strange behavior, I cannot explain.  
It depends on the order, how itk, and for example StrainPython (applies also to other modules) is imported and what happens inbetween.  
I did:

> import itk  
> itk.Image # for testing  
> #… (some other stuff)  
> import StrainPython

And everything works fine.

But if i do nothing inbetween the imports and use any itk function/class the problem in the post above is raised:

> “UnboundLocalError: local variable ‘module’ referenced before assignment”  
> in ITKBase.py

To all who might have similar problems:  
Try executing any itk class like itk.Image between your imports. This seems to solve the

> “AttributeError: module ‘ITKCommon’ has no attribute ‘swig’”  
> and  
> “UnboundLocalError: local variable ‘module’ referenced before assignment”

errors, even though I do not understand why.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 31, 2019, 1:07pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/4 "2019-07-31T13:07:19Z")

</div>

Perhaps lazy loading has something to do with it? @matt.mccormick

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [July 31, 2019, 3:17pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/5 "2019-07-31T15:17:13Z")

</div>

Welcome to the ITK community @laqua-stack!

First, well done building the Python package. 👏

To make it easier, I have uploaded [binary `itk-strain` Python packages to PyPI](https://pypi.org/project/itk-strain/). Please try installing it in a new conda virtual environment with:

```auto
pip install itk-strain

```

> Is there a specific way to find the correct naming convention of a remote module, wrapped for python?

To find out the content of the package, we can do the following:

```auto
from itk import Strain
dir(Strain)

```

which results in:

```auto
['ImageToImageFilter',
 'StrainImageFilter',
 '_LazyITKModule__belong_lazy_attributes',
 ' __doc__',
 ' __loader__',
 ' __name__',
 ' __package__',
 ' __spec__',
 'image_to_image_filter',
 'lazy_attributes',
 'loaded_lazy_modules',
 'strain_image_filter']

```

We know that the module is named `Strain` by the [argument to `itk_wrap_module`](https://github.com/KitwareMedical/ITKStrain/blob/master/wrapping/CMakeLists.txt#L1).

`StrainImageFilter` is the ITK class that can be used for [streaming pipelines](https://github.com/KitwareMedical/2019-03-13-KRSCourseInBiomedicalImageAnalysisAndVisualization/blob/master/2_Image_Filtering.ipynb).

`strain_image_filter` provides a [functional, Pythonic interface to the filter](https://discourse.itk.org/t/itk-5-0-beta-1-pythonic-interface/1271).

> Any suggestions? I am definitly missing somethin, sorry.

You are doing excellent 🙂 – I think there is a bug in the wrapping that causes the confusing error. I will follow-up…

---

<div class="post-metadata">

### Author: ![laqua-stack](https://discourse.itk.org/user_avatar/discourse.itk.org/laqua-stack/32/939_2.png) [@laqua-stack](https://discourse.itk.org/u/laqua-stack)
#### Post date: [July 31, 2019, 3:40pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/6 "2019-07-31T15:40:46Z")

</div>

Thanks for all the nice words and the help 😊.  
itk-strain on pypy? Awesome!!! Thank you so much.  
I will definitly try this. Is there a concern if a wrapped ITK is already build on the computer? Do i just need to rename WrapITK.pth to .bak in site-packages to make the custom build not interfere with it?

I think it was (ofcourse :-)) a mistake to import StrainPython module in addition to itk, since itk.StrainImageFilter is already there after importing itk. Now I imported itk only. That way it is also possible to use the Filter without specifying the used type like I did before in StrainPython.itkStrainImageFilterIVD22DD.New().

I think the errors I reported came from importing ITK modules (like StrainPython) in addtion to itk.

Again thank you so much.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [July 31, 2019, 3:53pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/7 "2019-07-31T15:53:10Z")

</div>

> [@laqua-stack](#):
>
> WrapITK.pth to .bak in site-packages to make the custom build not interfere with it?

Yes, that will work. A new conda env or virtualenv will work, too.

> [@laqua-stack](#):
>
> I think it was (ofcourse :-)) a mistake to import StrainPython module in addition to itk, since itk.StrainImageFilter is already there after importing itk. Now I imported itk only. That way it is also possible to use the Filter without specifying the used type like I did before in StrainPython.itkStrainImageFilterIVD22DD.New().

Yes, either approach can work. `from itk import Strain` can be helpful to just inspect the contents of the `Strain` module. In most use cases,

```auto
import itk
displacement = itk.imread('mydisplacement.mha')
strain = itk.strain_image_filter(displacement)

```

etc., is convenient.

---

<div class="post-metadata">

### Author: ![laqua-stack](https://discourse.itk.org/user_avatar/discourse.itk.org/laqua-stack/32/939_2.png) [@laqua-stack](https://discourse.itk.org/u/laqua-stack)
#### Post date: [July 31, 2019, 4:10pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/8 "2019-07-31T16:10:06Z")

</div>

Thanks Matt and Dzenan!  
I got it working now 😄  
You will get an update regarding pip’s itk-strain today or tomorrow.

---

<div class="post-metadata">

### Author: ![laqua-stack](https://discourse.itk.org/user_avatar/discourse.itk.org/laqua-stack/32/939_2.png) [@laqua-stack](https://discourse.itk.org/u/laqua-stack)
#### Post date: [July 31, 2019, 5:03pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/9 "2019-07-31T17:03:09Z")

</div>

I tested pip install itk-strain in an conda env, without errors.  
However, I am struggling with compatibility between numpy and PyPI’s itk:

> import itk  
> import numpy as np
> 
> #VectorType=itk.Vector[itk.D,2]  
> VectorType=itk.Vector[itk.F,2]  
> VectorImageType=itk.Image[VectorType,2]  
> SSRTType=itk.Image[itk.SymmetricSecondRankTensor[itk.D,2],2]
> 
> nparr=np.ascontiguousarray(np.moveaxis(np.indices((256,256),dtype=‘float32’),0,-1))  
> #DisplacementField=itk.GetImageFromArray(nparr,is\_vector=True)  
> DisplacementField=itk.PyBuffer[VectorImageType].GetImageFromArray(nparr,is\_vector=True)
> 
> testFilter= itk.StrainImageFilter[VectorImageType].New(DisplacementField)  
> testFilter.SetInput(DisplacementField)  
> testFilter.SetStrainForm(testFilter.GREENLAGRANGIAN)  
> testFilter.Update()  
> StrainTensorField=itk.PyBuffer[SSRTType].GetArrayFromImage(testFilter.GetOutput())

Only itk.ImageVF22 is supported by PyBuffer in this build and  
StrainImageFilter expects itk.ImageVD22 as input.  
And VectorCastImageFilter is not available in the PyPI’s itk build.

But I think if I had Images to read in with ITK’s functionality without the Numpy Bridge, it would work fine 😉

For me it works fine with my self-build ITK.  
Would it be possible(/not too much work) to wrap VectorCastImageFilter or rewrap PyBuffer for ‘double’ with VectorImage? I did not test ImageAdaptor yet.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [August 1, 2019, 10:29pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/10 "2019-08-01T22:29:37Z")

</div>

> Would it be possible(/not too much work) to wrap VectorCastImageFilter or rewrap PyBuffer for ‘double’ with VectorImage?

Yes, good idea! In ITK 5, we can just use `CastImageFilter` instead of `VectorCastImageFilter`. I created these issues to track this:

> <https://github.com/InsightSoftwareConsortium/ITK/issues/1132>
>
> See:
> 
> https://discourse.itk.org/t/installing-and-using-python-wrapped-remote…-module/2104/9

> <https://github.com/InsightSoftwareConsortium/ITK/issues/1133>
>
> Wrap \`CastImageFilter\` to convert to and from vector pixel types like \`Vector\`, …\`CovariantVector\`, etc. and \`VectorImage\`.
> 
> See:
> 
> https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/9

In the meantime, a possible workaround is to write / read from file.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [August 1, 2019, 10:30pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/11 "2019-08-01T22:30:39Z")

</div>

Also, a new version of the `itk-strain` package has been uploaded to PyPI; upgrade with:

```auto
pip install --upgrade itk-strain

```

---

<div class="post-metadata">

### Author: ![laqua-stack](https://discourse.itk.org/user_avatar/discourse.itk.org/laqua-stack/32/939_2.png) [@laqua-stack](https://discourse.itk.org/u/laqua-stack)
#### Post date: [August 8, 2019, 4:10pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/12 "2019-08-08T16:10:25Z")

</div>

Thank you Matt. I appreciate the new itk-strain version.  
Unfortunately I experienced a new problem with the self-build ITK. Since it is closely related to the one I described above, I reply it just here.  
When I use the self-build itk in Python 3.6 conda env, everything works fine.  
But if I attempt to use the same build in Python 3.7 with WrapITK.pth and PATH and PYTHONPATH set to ITKbuild/lib/Release and ITKbuild/bin/Release it throws the following error. Seems like the lazy loader does not look in the lib/Release folder, where \_ITKPyBasePython.pyd is located.

> import itk
> 
> itk.Image  
> Traceback (most recent call last):
> 
> File “”, line 1, in   
> itk.Image
> 
> File “C:\build\Wrapping\Generators\Python\Release\itkLazy.py”, line 46, in **getattribute**  
> itkBase.LoadModule(module, namespace)
> 
> File “C:\build\Wrapping\Generators\Python\Release\itkBase.py”, line 118, in LoadModule  
> LoadModule(dep, namespace)
> 
> File “C:\build\Wrapping\Generators\Python\Release\itkBase.py”, line 128, in LoadModule  
> module = loader.load(swigModuleName)
> 
> File “C:\build\Wrapping\Generators\Python\Release\itkBase.py”, line 253, in load  
> return importlib.import\_module(name)
> 
> File “C:\Users\Fabian\Anaconda3\envs\PythonCPU\lib\importlib\__init_\_.py”, line 127, in import\_module  
> return \_bootstrap.\_gcd\_import(name[level:], package, level)
> 
> File “”, line 1006, in \_gcd\_import
> 
> File “”, line 983, in \_find\_and\_load
> 
> File “”, line 967, in \_find\_and\_load\_unlocked
> 
> File “”, line 677, in \_load\_unlocked
> 
> File “”, line 728, in exec\_module
> 
> File “”, line 219, in \_call\_with\_frames\_removed
> 
> File “C:\build\lib\ITKPyBasePython.py”, line 32, in   
> \_ITKPyBasePython = swig\_import\_helper()
> 
> File “C:\build\lib\ITKPyBasePython.py”, line 24, in swig\_import\_helper  
> import \_ITKPyBasePython
> 
> ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden.

Even though i do not understand the link, but: Do I need to have an extra ITK build (with different referenced Python in CMAKE)?

Thanks in advance. I will try with PyPI’s ITK in the meantime and report.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [August 9, 2019, 3:38am UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/13 "2019-08-09T03:38:52Z")

</div>

> [@laqua-stack](#):
>
> When I use the self-build itk in Python 3.6 conda env, everything works fine.  
> But if I attempt to use the same build in Python 3.7

Are ITK’s CMake configuration variables, `PYTHON_INCLUDE_DIR` and `PYTHON_LIBRARY` consistent with the runtime version? Building for conda Python 3.6 and running Python 3.7, for example, is not expected to work.

---

<div class="post-metadata">

### Author: ![laqua-stack](https://discourse.itk.org/user_avatar/discourse.itk.org/laqua-stack/32/939_2.png) [@laqua-stack](https://discourse.itk.org/u/laqua-stack)
#### Post date: [August 9, 2019, 7:56pm UTC](https://discourse.itk.org/t/installing-and-using-python-wrapped-remote-module/2104/14 "2019-08-09T19:56:40Z")

</div>

I did not build ITK with PYTHON\_INCLUDE\_DIR set to python 3.7 executable yet.  
I am going to try this tonight.

I wasn’t aware of the need.  
Thank you!
