运行Flask部署NLP模型应用时出现sentence_transformers模块未找到错误
解决Flask应用运行时
sentence_transformers模块找不到的问题 问题背景
我正在开发一个用于部署NLP模型的Flask应用,后续将对接前后端,代码如下:
from flask import Flask, render_template, request, jsonify import joblib import pandas as pd from sentence_transformers import SentenceTransformer app = Flask(__name__) # Load the model from .joblib file with open("API/NearestNeighbors_clf.joblib", "rb") as file: loaded_model = joblib.load(file) # load the database from .csv file df = pd.read_csv("DL/database_Leo.csv") # define embedding model based on all-MiniLM-L6-v2 model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') model.max_seq_length = 512 # Change the length to 512 @app.route("/") 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"]) # create embeddings for user input user_input_emb = 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() # get ID's to return recs = df.iloc[indices]["reference"] return jsonify({"recommendations" : recs}) if __name__ == "__main__": app.run(debug=True)
单独测试导入sentence_transformers和模型时一切正常,模块也已安装在激活的conda环境中,但运行整个Flask文件时抛出以下错误:
Traceback (most recent call last): File "C:\Users\phyla\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3460, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-2-79851fa29ea7>", line 1, in <module> runfile('C:\Users\phyla\Documents\Github\ss23-drop-in-to-berlin\API\flask_app.py', wdir='C:\Users\phyla\Documents\Github\ss23-drop-in-to-berlin\API') File "C:\Program Files\JetBrains\PyCharm 2023.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "C:\Program Files\JetBrains\PyCharm 2023.1.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+" ", file, 'exec'), glob, loc) File "C:\Users\phyla\Documents\Github\ss23-drop-in-to-berlin\API\flask_app.py", line 4, in <module> from sentence_transformers import SentenceTransformer File "C:\Program Files\JetBrains\PyCharm 2023.1.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, **kwargs) ModuleNotFoundError: No module named 'sentence_transformers'
解决方案
确认运行Flask的Python环境与模块安装环境一致
- 激活你的conda环境:
conda activate 你的环境名 - 查看当前环境的Python路径:Windows执行
where python,Linux/macOS执行which python,记录该路径 - 在PyCharm中打开
File > Settings > Project: xxx > Python Interpreter,检查当前解释器是否为刚才记录的conda环境路径,若不是则点击齿轮图标选择Add,找到对应环境的解释器并设置
- 激活你的conda环境:
用conda重新安装模块避免环境冲突
激活目标conda环境后执行:conda install -c conda-forge sentence-transformers避免pip与conda混合安装导致的依赖不一致问题
直接使用conda环境的Python命令运行Flask
跳过IDE的运行按钮,在终端激活环境后进入Flask文件所在目录,执行:python flask_app.py强制使用正确环境的解释器
检查PyCharm运行配置
打开Run > Edit Configurations,找到Flask应用的运行配置,在Python interpreter选项中选择对应的conda环境,保存后重新运行
内容的提问来源于stack exchange,提问作者phylanx
相关产品推荐
相关产品推荐

