2
  • Python version: 3.7 (I have to use this version)
  • OS: Linux
  • Cloud Platform: Azure
  • Resource: Azure function with python
  • Goal: Load a model created with skit-learn version 1.0.2 with the following dependencies installed:

numpy: 1.17.3 joblib: 1.1.0 scipy: 1.7.3

I am using joblib to load a skit-learn model that I trained (By the way I created the model locally in my machine with python 3.9). However, I am getting the following error:

Traceback (most recent call last):
File \"/home/site/wwwroot/sortierung/__init__.py\", line 51, in main
prediction_file_path)
File \"/home/site/wwwroot/shared_code/custom_functions_prediction.py\", line 255, in predict
result.update(classify_mail(m,s,X, stop_words, model_folder_path))
File \"/home/site/wwwroot/shared_code/custom_functions_prediction.py\", line 105, in classify_mail
model = load(modelFilePath)
File \"/home/site/wwwroot/.python_packages/lib/site-packages/joblib/numpy_pickle.py\", line 587, in load
obj = _unpickle(fobj, filename, mmap_mode)
File \"/home/site/wwwroot/.python_packages/lib/site-packages/joblib/numpy_pickle.py\", line 506, in _unpickle
obj = unpickler.load()
File \"/usr/local/lib/python3.7/pickle.py\", line 1088, in load
dispatch[key[0]](self)
File \"/usr/local/lib/python3.7/pickle.py\", line 1385, in load_stack_global
self.append(self.find_class(module, name))
File \"/usr/local/lib/python3.7/pickle.py\", line 1426, in find_class
__import__(module, level=0)\nModuleNotFoundError: No module named 'scipy.sparse._csr'

I checked in the scipy folder installed and I could not find this module. How could I solve this issue?. Tks in advance

EdvardM
  • 2,934
  • 1
  • 21
  • 20
sergioMoreno
  • 189
  • 1
  • 15
  • Do these answer your question? [ImportError: No module named scipy.sparse](https://stackoverflow.com/questions/55043789/importerror-no-module-named-scipy-sparse) and [ModuleNotFoundError: No module named 'scipy.sparse.base'](https://stackoverflow.com/questions/66575732/modulenotfounderror-no-module-named-scipy-sparse-base) – Ecstasy Jun 15 '22 at 12:35
  • Hi @DeepDave-MT tks for your help. No, I already checked it and the problem is not with the installation of scipy as far as I understood. – sergioMoreno Jun 15 '22 at 12:39
  • It could be due to Python version. Reference: [No module named 'scipy.sparse._csr'](https://stackoverflow.com/questions/71050592/i-cannot-load-random-forest-model-using-joblib-i-am-getting-a-scipy-error) – Ecstasy Jun 15 '22 at 12:42
  • Hi @DeepDave-MT tks again for your prompt answer. I checked also already and the restriction that I have is that I have to use Python 3.7 because the machine in Azure has that version. Do you know what else can be? – sergioMoreno Jun 15 '22 at 13:17

2 Answers2

5

To resolve this ModuleNotFoundError: No module named 'scipy.sparse._csr' error, try the following way:

This error occurred because you have created a model in Python 3.9 but running it on Python 3.7.

You can try creating a model in Python 3.7 or upgrade the Azure Python function app to a specific version of Python 3.9.

To change the Python version to 3.9, according to documentation:

  • You can update the linuxFxVersion setting in the function app with the az functionapp config set command.
az functionapp config set --name <FUNCTION_APP> \
--resource-group <RESOURCE_GROUP> \
--linux-fx-version "python|3.9"

References: No module named 'scipy.sparse._csr' and How to change python version of azure function

Ecstasy
  • 1,866
  • 1
  • 9
  • 17
  • Hi @DeepDave-MT, tks a lot for your support. The error was, as you said, I created my model with a high version of python and when I want to open that model with a lower version of python, I got that error. I trained and load my model with python 3.7 and it works like a charm. Tks again – sergioMoreno Jun 16 '22 at 08:58
0

I agree with the last answer.

I have used python 3.7.10 and scipy 1.8.1 to dump the dict with csr matrix in local windows environment. After that, load the file in linux environment by python 3.8.10 and scipy 1.7.1.

self.word_multi_model_feature = pickle.load(f_dict_feature) ModuleNotFoundError: No module named 'scipy.sparse._csr'

You should change the load python environment to be same as the dump enviroment.

Aadil Hoda
  • 592
  • 7
  • 17