I'm somewhat new to python and QGIS plugin development. I've created a plugin structure for QGIS using the Plugin Builder on QGIS and I've added some controls to the gui and managed to update the appropriate module binding to the gui.

I'm also trying to use sklearn library nearest neighbor in my plugin application. Everything works well until sklearn first runs and I get an error message as follows:

An error has occured while executing Python code:

Traceback (most recent call last): File "C:/Users/c_aaalam/.qgis2/python/plugins\FemtoCoverage\femto_coverage.py", line 282, in run distances, indices = nbrs.kneighbors(subs) File "C:/Users/c_aaalam/.qgis2/python/plugins\FemtoCoverage\sklearn\neighbors\base.py", line 332, in kneighbors return_distance=return_distance) File "binary_tree.pxi", line 1295, in sklearn.neighbors.kd_tree.BinaryTree.query (sklearn\neighbors\kd_tree.c:10386) File "C:/Users/c_aaalam/.qgis2/python/plugins\FemtoCoverage\sklearn\utils\validation.py", line 117, in array2d if sp.issparse(X): AttributeError: 'NoneType' object has no attribute 'issparse'

This is the script I'm running in my plugin:

nbrs = NearestNeighbors(n_neighbors = 1, algorithm='auto').fit(array1)

distances, indices = nbrs.kneighbors(array2)

It looks like the scipy sparse variable is "NoneType" and therefore the "issparse()" function is not recognized. I have placed the sklearn folder in the plugin directory (windows user) and added the necessary import sklearn to my plugin module.

Does anyone have experience working with sklearn in PyQt applications or specifically with developing QGIS plugins and can help me resolve this error?

Maybe I'm loading sklearn incorrectly?

  • Sorry...also to add that this script and all the associated arrays work fine if I process this directly in the QGIS python console. The sklearn part is just not working in my plugin module execution.
    – aalam
    Feb 11, 2015 at 6:40

1 Answer 1


I figured out the issue. The problem was I had sklearn in the same directory as my plugin files and not in the site-packages under my qgis installation.

Not 100% sure that was the issue but once I deleted sklearn from the plugin directory and installed it in site-packages, the issue was resolved. No more errors!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.