Search code examples
python-3.xflaskflask-restful

When using Flask with python 3 and SimpleITK 0.10.0 together, i am getting AttributeError: type object 'object' has no attribute '__getattr__'


When i run the code for Updating Tags in a DCM file using SimpleITK 0.10.0 in Python 3 (https://itk.org/SimpleITKDoxygen010/html/Python_2DicomModifyTags_8py-example.html) the code is working as explained.

the moment i introduce flask components in the code to create an API, i get

AttributeError: type object 'object' has no attribute '__getattr__'

error. The Code i have used is as below

from __future__ import print_function
import SimpleITK
from flask import Flask
import time

app = Flask(__name__)


@app.route('/fuse')
def fuse():
    image = SimpleITK.ReadImage("CT/IMG-0002-000001.dcm")

    mean_image = SimpleITK.BoxMean( image, [3,3,1])

    all_keys = image.GetMetaDataKeys()
    for key in all_keys:
        mean_image.SetMetaData(key, image.GetMetaData(key))
    mean_image.SetMetaData("0008|0008", "DERIVED\SECONDARY")
    modification_time = time.strftime("%H%M%S")
    modification_date = time.strftime("%Y%m%d")
    mean_image.SetMetaData("0008|0031", modification_time)
    mean_image.SetMetaData("0008|0021", modification_date)

    print(mean_image.GetMetaData("0008|0031"))
    return "finish"

the error i am getting is below

[2016-10-05 14:47:05,816] ERROR in app: Exception on /fuse [GET]
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\flask\app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\python27\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\python27\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\python27\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\python27\lib\site-packages\flask\app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "E:\ITK_EXP_10\exp2.py", line 24, in fuse
    keyable(mean_image, image, key)
  File "E:\ITK_EXP_10\exp2.py", line 12, in keyable
    mean_image.SetMetaData(key, image.GetMetaData(key))
  File "c:\python27\lib\site-packages\SimpleITK\SimpleITK.py", line 3579, in <lambda>
    __getattr__ = lambda self, name: _swig_getattr(self, Image, name)
  File "c:\python27\lib\site-packages\SimpleITK\SimpleITK.py", line 74, in _swig_getattr
    return _swig_getattr_nondynamic(self, class_type, name, 0)
  File "c:\python27\lib\site-packages\SimpleITK\SimpleITK.py", line 69, in _swig_getattr_nondynamic
    return object.__getattr__(self, name)
AttributeError: type object 'object' has no attribute '__getattr__'
127.0.0.1 - - [05/Oct/2016 14:47:05] "GET /fuse HTTP/1.1" 500 -
127.0.0.1 - - [05/Oct/2016 14:47:05] "GET /favicon.ico HTTP/1.1" 404 -

i am a beginner in python coding, actually beginner in coding itself, can some one please help me fix this issue, or help me understand the issue so that i can fix it?

Thanks in advance


Solution

  • the issue was because of different versions of python running in pycharm and terminal. The terminal was running 2.7 and pycharm was 3.5. the lib was updated in 3.5. sorry for the confusion