another import error

Hi there, after finding a solution to my last import error, now things got really weird.

I wanted to add flask-cors to my setup in order to be able to access the API from elsewhere. Now this is the error I get from the error.log:

2023-10-03 19:19:22,611: ***************************************************
2023-10-03 19:19:22,810: Error running WSGI application
2023-10-03 19:19:22,810: ModuleNotFoundError: No module named 'flask_cors'
2023-10-03 19:19:22,811:   File "/var/www/", line 16, in <module>
2023-10-03 19:19:22,811:     from flask_app import app as application  # noqa
2023-10-03 19:19:22,811: 
2023-10-03 19:19:22,811:   File "/home/phylanx/mysite/", line 4, in <module>
2023-10-03 19:19:22,811:     from flask_cors import CORS
2023-10-03 19:19:22,812: ***************************************************

Then, when trying to debugg the issue, I ran the WSGI file from the console, getting this error which I can't make much sense of other than that there is some problem with pickle:

(env) 19:19 ~/mysite $ python -i /var/www/
Traceback (most recent call last):
  File "/var/www/", line 16, in <module>
    from flask_app import app as application  # noqa
  File "/home/phylanx/mysite/", line 11, in <module>
    loaded_model = joblib.load(file)
  File "/usr/local/lib/python3.8/dist-packages/joblib/", line 577, in load
    obj = _unpickle(fobj)
  File "/usr/local/lib/python3.8/dist-packages/joblib/", line 506, in _unpickle
    obj = unpickler.load()
  File "/usr/lib/python3.8/", line 1212, in load
KeyError: 61

Please help?

edit: Ok, I realized it has to do with the code itself, which I'll try to figure out but maybe somebody here can give me a pointer anyways...

This is my flask app:

from flask import Flask, render_template, request, jsonify
import joblib
import pandas as pd
from flask_cors import CORS

app = Flask(__name__)
CORS(app, resources={r"/predict": {"origins": "http://localhost:3000"}})

# Load the model from .joblib file
with open("NearestNeighbors_clf.joblib", "rb") as file:
    loaded_model = joblib.load(file)

# load the database from .csv file
df = pd.read_csv("../database_Leo.csv")

with open("SentenceTransformer_model.joblib", "rb") as file:
    sentence_model = joblib.load(file)

def index():
    return render_template("index.html")

@app.route("/predict", methods=["POST"])
def predict():
    # extract input data from the request
    star_rating = float(request.form["star_rating"])
    price = float(request.form["price"])
    user_input_str = str(request.form["user_input_str"])

    # print variables to see if they are picked up correctly from user input
    print(star_rating, price, user_input_str)

    # create embeddings for user input
    user_input_emb = sentence_model.encode(user_input_str).reshape(1, -1)

    # make prediction based on user input
    distances, indices = loaded_model.kneighbors(user_input_emb)
    # flatten indices array to use as index in dataframe
    indices = indices.flatten()

    # filter the whole dataframe for star rating above the user given value and below given price
    df_filtered = df[df.index.isin(indices) & (df.rating > star_rating) & (df.price_level < price)]

    # get all data points without values for price level
    df_price_nan = df[df.index.isin(indices) & df.price_level.isna()]

    # combine both into the output & get ID's to return
    recs = list(pd.concat([df_filtered, df_price_nan])["reference"])

    return jsonify({"recommendations": recs})

if __name__ == "__main__":

Is it possible that the pickle was created with different version of python or some libraries?

Hmm, yes that is indeed possible. Thanks fir the pointer, I'll rebuild the pickle file!

I redid all my models in python3.8 and still get the same error. Everything runs fine as long as I don't import flask_cors, so something has to be the problem with that package but I don't know what. It only throws an error upon hitting the command to load the model from the .joblib file.

What error?

Ok, I found the problem. It seems that flask or flask-cors doesn't specify all dependencies correctly, so that I had to manually specify the version of the werkzeug package to 2.2.2