# Help getting started with python wrapping

**URL:** <https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612>\
**Category:** Beginner Questions\
**Tags:** wrapping, cmake, python\
**Created:** [November 12, 2020, 11:21pm UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612 "2020-11-12T23:21:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![doconnell](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doconnell](https://discourse.itk.org/u/doconnell)\
**Post date:** [November 12, 2020, 11:21pm UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/1 "2020-11-12T23:21:06Z")

</div>

Hello,

I’m having some trouble using the ITK python wrapping functionality. I’m trying to work through the basic python examples but I’m unable to import ITK even after copying the WrapITK.pth file to my site-packages directory. I can import ITK if I manually add “/Wrapping/Generators/Python/” to the path. If I do that I receive an error when I try to use basic modules (I’ve tested itk.imread and itk.Image so far and receive the same error for both):

**C:\research\itk\Wrapping\Generators\Python\Release\itkBase.py** in LoadModule **(name, namespace)**  
59 **if** namespace **is**  **not**  **None**  **:**  
60 swig **=** namespace **.** setdefault **(**  **‘swig’**  **,**  **{**  **}** **)** **—\>  
61 **swig**. **update** ( **this\_module**. **swig** )**  
62  
63 **# don’t worry about overwriting the symbols in namespace – any** \*\*

AttributeError\*\* : module ‘itk.ITKIOImageBasePython’ has no attribute ‘swig’

Incase it’s helpful, here’s some background:

I compiled ITK 5.1.1 from source following the instructions in the software guide. ITK\_WRAP\_PYTHON was enabled, but I did not have the other variables that the guide mentioned after configuring multiple times: PYTHON INCLUDE DIR, PYTHON LIBRARY, and PYTHON EXECUTABLE. Even so, everything compiled without error. I’m using Windows 10 and Visual Studio 2019.

When I went to copy the WrapITK.pth to my site-packages directory, I found that it wasn’t in the Wrapping\Generators\Python\ folder as specified in the install guide. I did find it in \Wrapping\Generators\Python\Release and copied it to the site-packages directory.

I’ve tried installing swig separately and enabling the use system swig option, but that didn’t help. I’ve also tried compiling with and without building the shared libraries.

I’m very stumped on this one, and I would appreciate any suggestions on what to try next.

---

<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:** [November 13, 2020, 4:47pm UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/2 "2020-11-13T16:47:21Z")

</div>

Some bugfixes were done to Python wrapping recently. The last in the series is [PR 2081](https://github.com/InsightSoftwareConsortium/ITK/pull/2081), so make sure to use [commit e9fdf14](https://github.com/InsightSoftwareConsortium/ITK/commit/e9fdf144f5ce692589eb0172fbea2c0ebd683403) or newer.

---

<div class="post-metadata">

**Author:** ![doconnell](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doconnell](https://discourse.itk.org/u/doconnell)\
**Post date:** [November 14, 2020, 9:50pm UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/3 "2020-11-14T21:50:30Z")

</div>

Success! I recompiled with the latest version of the source and it’s working. Thank you!

---

<div class="post-metadata">

**Author:** ![doconnell](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doconnell](https://discourse.itk.org/u/doconnell)\
**Post date:** [November 17, 2020, 6:34pm UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/4 "2020-11-17T18:34:21Z")

</div>

Unfortunately I ran into some more trouble. ITK and Python wrapping are working to the best of my knowledge, so I’ve gone on to attempting to compile RTK.

First, despite ITK appearing to be fully functional, I received these errors when compiling the latest code from Github. Adding them here just in case they are related to my RTK issues:  
Severity Code Description Project File Line Suppression State  
Error MSB6006 “cmd.exe” exited with code 12. ITKGPUCommonPython C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 209   
Error MSB6006 “cmd.exe” exited with code 12. ITKGPUFiniteDifferencePython C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 209   
Error MSB6006 “cmd.exe” exited with code 12. ITKGPUAnisotropicSmoothingPython C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 209   
Error MSB6006 “cmd.exe” exited with code 12. ITKGPUImageFilterBasePython C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 209

After ITK, I’ve tried to compile RTK 2.1.0 as well as the latest copy of from github, and got multiple errors, starting with this one:

Severity Code Description Project File Line Suppression State  
Error MSB8066 Custom build for ‘C:\Users\dylan\RTK-2.1.0\src\rtkCudaAverageOutOfROIImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaBackProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaConjugateGradientImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaConstantVolumeSeriesSource.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaConstantVolumeSource.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaCropImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaCyclicDeformationImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaDisplacedDetectorImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaFDKBackProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaFDKWeightProjectionFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaFFTProjectionsConvolutionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaFirstOrderKernels.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaForwardProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaForwardWarpImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaInterpolateImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaLagCorrectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaLaplacianImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaLastDimensionTVDenoisingImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaParkerShortScanImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaPolynomialGainCorrectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaRayCastBackProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaSplatImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaTotalVariationDenoisingBPDQImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaUtilities.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaWarpBackProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaWarpForwardProjectionImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaWarpImageFilter.cu;C:\Users\dylan\RTK-2.1.0\src\rtkCudaWeidingerForwardModelImageFilter.cu’ exited with code 1. RTK C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets 238

There were also several errors, all with the same code cannot use ‘dynamic\_cast’ to convert from ‘const itk::DataObject \*’ to ‘itk::ImageBase\<3\> \*’. Here’s an example:

Severity Code Description Project File Line Suppression State  
Error C2682 cannot use ‘dynamic\_cast’ to convert from ‘const itk::DataObject \*’ to ‘itk::ImageBase\<3\> \*’ rtkvarianobigeometry C:\Users\dylan\RTK-2.1.0\applications\rtkvarianobigeometry\rtkvarianobigeometry.cxx 36

And several more similar to this one:  
Severity Code Description Project File Line Suppression State  
Error LNK1181 cannot open input file ‘lib\Release\itkRTK-5.2.lib’ RTKHeaderTest1 C:\research\rtk\LINK 1

I also tried completely uninstalling ITK, and then recompiling ITK with the RTK module turned on. In that case, I got the LNK1181 cannot open input file ‘lib\Release\itkRTK-5.2.lib’ errors as well.

Once again any help or advice would be much appreciated.

---

<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:** [November 18, 2020, 7:44pm UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/5 "2020-11-18T19:44:58Z")

</div>

@simon.rit or @LucasGandel might be able to help. Or ask this question on the [RTK mailing list](https://www.openrtk.org/RTK/project/contactus.html).

---

<div class="post-metadata">

**Author:** ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)\
**Post date:** [November 18, 2020, 8:02pm UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/6 "2020-11-18T20:02:02Z")

</div>

Hi,  
Do you get the same errors with ITK and RTK master? The last time I tried to compile the python wrappings, it compiled but failed to load, see issue [#2064](https://github.com/InsightSoftwareConsortium/ITK/issues/2064)… I don’t think it has been solved but I’ll relaunch a build to check now (on Windows though).

For your initial error, note that the [RTK pypi package](https://pypi.org/manage/project/itk-rtk/) is incompatible with the latest ITK release. I haven’t had time to prepare a release to fix this but you can download CI packages [here](https://github.com/SimonRit/RTK/actions/runs/341384403) compatible with the latest ITK release. Note that you need to be logged in github to access the artifacts.  
Simon

---

<div class="post-metadata">

**Author:** ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)\
**Post date:** [November 19, 2020, 12:03am UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/7 "2020-11-19T00:03:45Z")

</div>

On Linux, I can successfully compile but something seems wrong. I get

```auto
>>> import itk
>>> itk.Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'itk' has no attribute 'Image'

```

with or without RTK. I have no idea if it’s a known problem, maybe @hjmjohnson knows…

---

<div class="post-metadata">

**Author:** ![doconnell](https://discourse.itk.org/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doconnell](https://discourse.itk.org/u/doconnell)\
**Post date:** [November 20, 2020, 8:33pm UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/8 "2020-11-20T20:33:46Z")

</div>

Thank you both for your replies!

@simon.rit, Just to clarify: when I try to install the latest version of ITK master with the Module\_RTK option enabled, I receive an MSB8066 error for project RTK in file Microsoft.Cpp.Common.targets, line 238, which is the beginning of this block:

```
<CustomBuild Condition ="'@(_CustomBuild)' != ''"
  Sources ="@(_CustomBuild)"
  BuildSuffix ="$(_BuildSuffix)"

  MinimalRebuildFromTracking ="false"
  AcceptableNonZeroExitCodes ="%(_CustomBuild.AcceptableNonZeroExitCodes)"
  StdOutEncoding = "%(_CustomBuild.StdOutEncoding)"
  StdErrEncoding = "%(_CustomBuild.StdErrEncoding)"
>

```

Is this the error that you’re referring to being due to the RTK pypi package incompatibility? Or is this error a separate problem? Either way, I receive this both when compiling ITK with the RTK module, or when compiling RTK after having compiled ITK without the RTK option.

The subsequent errors are linker related (LINK1181), and all are stating that itkRTK-2.lib can’t be opened. I get these in both cases as well.

```
**Severity	Code	Description	Project	File	Line	Suppression State**
Error	LNK1181	cannot open input file '..\..\..\..\lib\Release\itkRTK-5.2.lib'	 
ITKPyUtilsPython	C:\research\itk\Wrapping\Generators\Python\PyUtils\LINK	1	

```

I can’t find itkRTK-5.2.lib anywhere so I’m guessing it isn’t being built successfully.

Here’s the third type of error. I only receive this when compiling RTK after ITK:

```
Severity Code Description Project File Line Suppression State
Error C2682 cannot use ‘dynamic_cast’ to convert from ‘const itk::DataObject *’ to 
‘itk::ImageBase<3> *’ rtkvarianobigeometry C:\Users\dylan\RTK-2.1.0\applications\rtkvarianobigeometry\rtkvarianobigeometry.cxx 36

```

Thank you for the CI packages. I installed these, but I’m unable to use the GPUImageType:

```
AttributeError: module 'RTK' has no attribute 'CudaImage' 

```

I have the same behavior when installing the RTK binaries through pip. Ultimately I’d like to be able to use the RTK GPU functionality, so that’s what led me to trying to compile from source.

Once again, thank you very much for your time.

---

<div class="post-metadata">

**Author:** ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)\
**Post date:** [November 25, 2020, 8:22am UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/9 "2020-11-25T08:22:30Z")

</div>

> [@doconnell](#):
>
> Is this the error that you’re referring to being due to the RTK pypi package incompatibility? Or is this error a separate problem? Either way, I receive this both when compiling ITK with the RTK module, or when compiling RTK after having compiled ITK without the RTK option.

I think I misunderstood the first post, I though you were trying to use compiled packages from pypi. So this is a separate problem. On my side, I am not able to compile ITK without RTK so I won’t be able to help you… I’ll try to work on this next week.

---

<div class="post-metadata">

**Author:** ![jasper\_nijkamp](https://discourse.itk.org/user_avatar/discourse.itk.org/jasper_nijkamp/32/2213_2.png) [@jasper\_nijkamp](https://discourse.itk.org/u/jasper_nijkamp)\
**Post date:** [August 13, 2021, 8:47am UTC](https://discourse.itk.org/t/help-getting-started-with-python-wrapping/3612/10 "2021-08-13T08:47:48Z")

</div>

@doconnell: have you been able to solve the errors in this post. I just downloaded ITK from GIT and tried to build it with RTK and python wrapping, and get the same list of errors.
