Problem with weka AttributeError: 'NoneType' object has no attribute 'find_class'

2,028 views
Skip to first unread message

Leandro Lichtensztein

unread,
May 5, 2015, 10:11:07 AM5/5/15
to python-we...@googlegroups.com
Hi to all,
I'm running my program in an apache server with mod_wsgi. I'm using weka-wrapper to generate the cluster model for an image database and after I'm using it to get the cluster that contains the searched image.
So, the first time I search the image I get the answer from weka, but the second time it throws an exception, then, the third time it works, and so on.
Inside my getMembership function I'm using "serialization.read(modelFile)" to load the model, this is the function that crashes.
Below you can see the error log from apache. I'm not sure what the problem is here, can anyone help me?
thanks a lot

[Tue May 05 09:54:19 2015] [error] ERROR:run:Exception on /match [GET]
[Tue May 05 09:54:19 2015] [error] Traceback (most recent call last):
[Tue May 05 09:54:19 2015] [error]   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
[Tue May 05 09:54:19 2015] [error]     response = self.full_dispatch_request()
[Tue May 05 09:54:19 2015] [error]   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
[Tue May 05 09:54:19 2015] [error]     rv = self.handle_user_exception(e)
[Tue May 05 09:54:19 2015] [error]   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
[Tue May 05 09:54:19 2015] [error]     reraise(exc_type, exc_value, tb)
[Tue May 05 09:54:19 2015] [error]   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
[Tue May 05 09:54:19 2015] [error]     rv = self.dispatch_request()
[Tue May 05 09:54:19 2015] [error]   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
[Tue May 05 09:54:19 2015] [error]     return self.view_functions[rule.endpoint](**req.view_args)
[Tue May 05 09:54:19 2015] [error]   File "/var/www/FaceRecognition/run.py", line 53, in match
[Tue May 05 09:54:19 2015] [error]     clusterList = clust.getMembership(wekaFile,"%s/dataFiles/wekaFiles/model/MAIN.model" % BASE_PATH)
[Tue May 05 09:54:19 2015] [error]   File "/var/www/FaceRecognition/package/clustering/clustering.py", line 46, in getMembership
[Tue May 05 09:54:19 2015] [error]     clusterer = loadModel(modelFile)
[Tue May 05 09:54:19 2015] [error]   File "/var/www/FaceRecognition/package/clustering/clustering.py", line 25, in loadModel
[Tue May 05 09:54:19 2015] [error]     return Clusterer(jobject=serialization.read(modelFile))
[Tue May 05 09:54:19 2015] [error]   File "/usr/lib/python2.7/dist-packages/weka/core/serialization.py", line 63, in read
[Tue May 05 09:54:19 2015] [error]     filename)
[Tue May 05 09:54:19 2015] [error]   File "/usr/lib64/python2.7/dist-packages/javabridge/jutil.py", line 902, in static_call
[Tue May 05 09:54:19 2015] [error]     fn = make_static_call(class_name, method_name, sig)
[Tue May 05 09:54:19 2015] [error]   File "/usr/lib64/python2.7/dist-packages/javabridge/jutil.py", line 873, in make_static_call
[Tue May 05 09:54:19 2015] [error]     klass = env.find_class(class_name)
[Tue May 05 09:54:19 2015] [error] AttributeError: 'NoneType' object has no attribute 'find_class'

Eric Romanenghi

unread,
May 5, 2015, 10:31:29 AM5/5/15
to python-we...@googlegroups.com
I have the same problem...

Peter Reutemann

unread,
May 5, 2015, 5:01:19 PM5/5/15
to python-weka-wrapper
> I'm running my program in an apache server with mod_wsgi. I'm using
> weka-wrapper to generate the cluster model for an image database and after
> I'm using it to get the cluster that contains the searched image.
> So, the first time I search the image I get the answer from weka, but the
> second time it throws an exception, then, the third time it works, and so
> on.
> Inside my getMembership function I'm using "serialization.read(modelFile)"
> to load the model, this is the function that crashes.
> Below you can see the error log from apache. I'm not sure what the problem
> is here, can anyone help me?

Not sure what is happening there either. Could you please extract the
relevant code from your application and put it into a standalone
python module and check whether you can replicate the problem in that
environemnt? If yes, can you then please post that code?

BTW Does mod_wsgi instantiate your Python modules and therefore start
the JVM each time you get a request?

Cheers, Peter
--
Peter Reutemann, Dept. of Computer Science, University of Waikato, NZ
http://www.cms.waikato.ac.nz/~fracpete/ Ph. +64 (7) 858-5174
http://www.data-mining.co.nz/
Message has been deleted

Peter Reutemann

unread,
Jun 27, 2015, 8:39:00 PM6/27/15
to python-weka-wrapper
> I have the same problem "AttributeError: 'NoneType' object has no attribute
> 'find_class'" on line
> loader = Loader(classname="weka.core.converters.ArffLoader")
>
> Did somebody solve this problem?

I can't replicate this problem on any of my test systems (all examples
work). There must be something about your system that is different.
Are you using the library in a multi-threaded environment? If so, that
might be the problem - neither Weka nor this library are threadsafe.
If not, can you send me the smallest possible code fragment with which
you can replicate that problem?

Thanks.

Cheers, Peter
--
Peter Reutemann
Dept. of Computer Science
University of Waikato, NZ
+64 (7) 858-5174
http://www.cms.waikato.ac.nz/~fracpete/
http://www.data-mining.co.nz/

Сергей Банин

unread,
Jun 28, 2015, 11:33:27 AM6/28/15
to python-we...@googlegroups.com, frac...@waikato.ac.nz
Sorry, I deleted my question. I realized that I suddenly removed the line about jvm.start()

воскресенье, 28 июня 2015 г., 2:39:00 UTC+2 пользователь Peter Reutemann написал:

Hong Chen

unread,
Oct 24, 2015, 3:13:10 AM10/24/15
to python-weka-wrapper, ericrom...@gmail.com
try to add these two lines at the beginning of the code. My problem solved in this way.

import weka.core.jvm as jvm
jvm.start()

sethuraman j

unread,
Jun 29, 2016, 1:06:26 PM6/29/16
to python-weka-wrapper, frac...@waikato.ac.nz
Hello,

I have the same problem, but I am calling a Classifier from inside a function. 

############ CODE ##################

from weka.classifiers import Classifier, PredictionOutput
import weka.core.jvm as jvm
from weka.core.converters import Loader
from weka.classifiers import Evaluation

jvm.start(class_path=['C:\Users\Sethuraman\Anaconda2\Lib\site-packages\python_weka_wrapper3-0.1.0-py2.7.egg\weka\lib\python-weka-wrapper.jar','C:\Users\Sethuraman\Anaconda2\Lib\site-packages\python_weka_wrapper3-0.1.0-py2.7.egg\weka\lib\weka.jar'],
packages=True)


loader = Loader(classname="weka.core.converters.ArffLoader")
data = loader.load_file(".\Train_data\\train2200.arff")
data.class_is_last()
def model_regressor():

# Weka
# from weka.classifiers import Classifier
print 'Welcome to Weka !'
cls = Classifier(classname="weka.classifiers.trees.M5P", options=['-M','4.0'])
    cls.build_classifier(data)
    evaluation = Evaluation(data)
    return cls, evaluation


###### SOME MORE FUNCTIONS DEFINITIONS ####\

jvm.stop() 
classifier, evaluation = model_regressor()

########### CODE END ##################

############# ERROR ###############

C:\Users\Sethuraman\Anaconda2\python.exe C:/Users/Sethuraman/PycharmProjects/Test_printer/m_weka_dtr.py
DEBUG:weka.core.jvm:Adding user-supplied classpath=C:\Users\Sethuraman\Anaconda2\Lib\site-packages\python_weka_wrapper3-0.1.0-py2.7.egg\weka\lib\python-weka-wrapper.jar
DEBUG:weka.core.jvm:Adding user-supplied classpath=C:\Users\Sethuraman\Anaconda2\Lib\site-packages\python_weka_wrapper3-0.1.0-py2.7.egg\weka\lib\weka.jar
DEBUG:weka.core.jvm:Adding bundled jars
DEBUG:weka.core.jvm:Adding Weka packages
DEBUG:weka.core.jvm:package_dir=C:\Users\Sethuraman\wekafiles\packages
DEBUG:weka.core.jvm:Classpath=['C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\javabridge-1.0.14-py2.7-win-amd64.egg\\javabridge\\jars\\rhino-1.7R4.jar', 'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\javabridge-1.0.14-py2.7-win-amd64.egg\\javabridge\\jars\\runnablequeue.jar', 'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\javabridge-1.0.14-py2.7-win-amd64.egg\\javabridge\\jars\\cpython.jar', 'C:\\Users\\Sethuraman\\Anaconda2\\Lib\\site-packages\\python_weka_wrapper3-0.1.0-py2.7.egg\\weka\\lib\\python-weka-wrapper.jar', 'C:\\Users\\Sethuraman\\Anaconda2\\Lib\\site-packages\\python_weka_wrapper3-0.1.0-py2.7.egg\\weka\\lib\\weka.jar', 'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\python_weka_wrapper-0.3.8-py2.7.egg\\weka\\lib\\python-weka-wrapper.jar', 'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\python_weka_wrapper-0.3.8-py2.7.egg\\weka\\lib\\weka.jar']
DEBUG:weka.core.jvm:MaxHeapSize=default
DEBUG:javabridge.jutil:Creating JVM object
DEBUG:javabridge.jutil:Signalling caller
Traceback (most recent call last):
  File "C:/Users/Sethuraman/PycharmProjects/Test_printer/m_weka_dtr.py", line 190, in <module>
    fitting_time, prediction_time, predicted_values, model_evaluation = model_regressor()
  File "C:/Users/Sethuraman/PycharmProjects/Test_printer/m_weka_dtr.py", line 44, in model_regressor
    cls = Classifier(classname="weka.classifiers.trees.M5P", options=['-M','4.0'])
  File "C:\Users\Sethuraman\Anaconda2\lib\site-packages\python_weka_wrapper-0.3.8-py2.7.egg\weka\classifiers.py", line 53, in __init__
    jobject = Classifier.new_instance(classname)
  File "C:\Users\Sethuraman\Anaconda2\lib\site-packages\python_weka_wrapper-0.3.8-py2.7.egg\weka\core\classes.py", line 590, in new_instance
    return javabridge.make_instance(jni_classname, "()V")
  File "C:\Users\Sethuraman\Anaconda2\lib\site-packages\javabridge-1.0.14-py2.7-win-amd64.egg\javabridge\jutil.py", line 1710, in make_instance
Welcome to Weka !
    klass = get_env().find_class(class_name)
AttributeError: 'NoneType' object has no attribute 'find_class'

Process finished with exit code 1

############### ERROR END ##########################

If I do this as a separate snippet as a test program it works. As a part of a larger program it is throwing that error. Please help. 


Thanks,
Sethuraman.

Peter Reutemann

unread,
Jun 29, 2016, 6:38:41 PM6/29/16
to python-weka-wrapper
> I have the same problem, but I am calling a Classifier from inside a
> function.
>
> ############ CODE ##################
>
> from weka.classifiers import Classifier, PredictionOutput
> import weka.core.jvm as jvm
>
> from weka.core.converters import Loader
> from weka.classifiers import Evaluation
>
>
> jvm.start(class_path=['C:\Users\Sethuraman\Anaconda2\Lib\site-packages\python_weka_wrapper3-0.1.0-py2.7.egg\weka\lib\python-weka-wrapper.jar','C:\Users\Sethuraman\Anaconda2\Lib\site-packages\python_weka_wrapper3-0.1.0-py2.7.egg\weka\lib\weka.jar'],
> packages=True)

Why are you adding the jar files manually? They get detected
automatically. See comments below.

> loader = Loader(classname="weka.core.converters.ArffLoader")
> data = loader.load_file(".\Train_data\\train2200.arff")
> data.class_is_last()
>
> def model_regressor():
>
> # Weka
> # from weka.classifiers import Classifier
> print 'Welcome to Weka !'
> cls = Classifier(classname="weka.classifiers.trees.M5P",
> options=['-M','4.0'])
>
> cls.build_classifier(data)
>
> evaluation = Evaluation(data)
>
> return cls, evaluation
>
>
>
> ###### SOME MORE FUNCTIONS DEFINITIONS ####\
>
>
> jvm.stop()
>
> classifier, evaluation = model_regressor()


After stopping the JVM, you won't be able to access classifier/evaluation.


> ########### CODE END ##################
>
> ############# ERROR ###############
>
> C:\Users\Sethuraman\Anaconda2\python.exe
> C:/Users/Sethuraman/PycharmProjects/Test_printer/m_weka_dtr.py
> DEBUG:weka.core.jvm:Adding user-supplied
> classpath=C:\Users\Sethuraman\Anaconda2\Lib\site-packages\python_weka_wrapper3-0.1.0-py2.7.egg\weka\lib\python-weka-wrapper.jar
> DEBUG:weka.core.jvm:Adding user-supplied
> classpath=C:\Users\Sethuraman\Anaconda2\Lib\site-packages\python_weka_wrapper3-0.1.0-py2.7.egg\weka\lib\weka.jar
> DEBUG:weka.core.jvm:Adding bundled jars
> DEBUG:weka.core.jvm:Adding Weka packages
> DEBUG:weka.core.jvm:package_dir=C:\Users\Sethuraman\wekafiles\packages
> DEBUG:weka.core.jvm:Classpath=['C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\javabridge-1.0.14-py2.7-win-amd64.egg\\javabridge\\jars\\rhino-1.7R4.jar',
> 'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\javabridge-1.0.14-py2.7-win-amd64.egg\\javabridge\\jars\\runnablequeue.jar',
> 'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\javabridge-1.0.14-py2.7-win-amd64.egg\\javabridge\\jars\\cpython.jar',
> 'C:\\Users\\Sethuraman\\Anaconda2\\Lib\\site-packages\\python_weka_wrapper3-0.1.0-py2.7.egg\\weka\\lib\\python-weka-wrapper.jar',
> 'C:\\Users\\Sethuraman\\Anaconda2\\Lib\\site-packages\\python_weka_wrapper3-0.1.0-py2.7.egg\\weka\\lib\\weka.jar',
> 'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\python_weka_wrapper-0.3.8-py2.7.egg\\weka\\lib\\python-weka-wrapper.jar',
> 'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\python_weka_wrapper-0.3.8-py2.7.egg\\weka\\lib\\weka.jar']

When you look at the classpath, you can see that you're mixing jars
from python-weka-wrapper and python-weka-wrapper3:

['C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\javabridge-1.0.14-py2.7-win-amd64.egg\\javabridge\\jars\\rhino-1.7R4.jar',
'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\javabridge-1.0.14-py2.7-win-amd64.egg\\javabridge\\jars\\runnablequeue.jar',
'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\javabridge-1.0.14-py2.7-win-amd64.egg\\javabridge\\jars\\cpython.jar',
'C:\\Users\\Sethuraman\\Anaconda2\\Lib\\site-packages\\python_weka_wrapper3-0.1.0-py2.7.egg\\weka\\lib\\python-weka-wrapper.jar',
'C:\\Users\\Sethuraman\\Anaconda2\\Lib\\site-packages\\python_weka_wrapper3-0.1.0-py2.7.egg\\weka\\lib\\weka.jar',
'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\python_weka_wrapper-0.3.8-py2.7.egg\\weka\\lib\\python-weka-wrapper.jar',
'C:\\Users\\Sethuraman\\Anaconda2\\lib\\site-packages\\python_weka_wrapper-0.3.8-py2.7.egg\\weka\\lib\\weka.jar']

[...]

> "C:\Users\Sethuraman\Anaconda2\lib\site-packages\python_weka_wrapper-0.3.8-py2.7.egg\weka\classifiers.py",
> line 53, in __init__

[..]

From the error message, you're running the Python2.7 version of
python-weka-wrapper. Is that what you're trying to achieve? You seem
to have both versions installed. Which Python version is your program
in?
Reply all
Reply to author
Forward
0 new messages