首页 > 解决方案 > 在django项目中保存文件以加载文件的位置,FileNotFoundError

问题描述

我有带有机器学习模型的 some_file.pkl,我需要将它加载到我的 django 项目中。加载过程是正确的,因为它与另一个项目一起工作。但是 django 说:

FileNotFoundError:[Errno 2] 没有这样的文件或目录:'some_file.pkl'

pickle 文件的位置在 django-app 下,我认为这是错误的原因,但我无法在 Djano 框架中找到要加载的文件的正确位置,请帮助。

这是我的看法:

def home(request):
    content = ''
    if request.method == 'POST':
        if request.POST.get('sms'):
            content = request.POST.get('fakenews_textarea')

    #Load the Model back from file
    with open("FakeNews_Model.pkl", 'rb') as file:  
        Fake_News_Model = pickle.load(file)

    #Load the Model back from file
    tfidf_vectorizer = pickle.load(open("tfidf.pickle", "rb"))

    return render(request,'web/home.html')

标签: djangopickle

解决方案


推荐阅读