How can I deploy a Django project with a pickled model in Railway app?

I would like to deploy my Django project on a Railway app, but the server does not find the pickled model. Although I made sure that the file exists in the github repository that I am deploying, Railway app continuously displays this error:

from core import views

File "/app/core/views.py", line 37, in

binary_clf = joblib.load(os.path.abspath('binary_classifier.pkl'))

File "/opt/venv/lib/python3.8/site-packages/joblib/numpy_pickle.py", line 650, in load

with open(filename, 'rb') as f:

FileNotFoundError: [Errno 2] No such file or directory: '/app/binary_classifier.pkl'

The thing is, I don't have any static files except for this pickled ML model.

But, I tried including the pickled model in a 'static' folder like it was described here, in which case I got this error:

from core import views

File "/app/core/views.py", line 37, in

binary_clf = joblib.load("static/binary_classifier.pkl")

File "/opt/venv/lib/python3.8/site-packages/joblib/numpy_pickle.py", line 650, in load

with open(filename, 'rb') as f:

FileNotFoundError: [Errno 2] No such file or directory: 'static/binary_classifier.pkl'

How are we supposed to deploy Django apps when we have pickled files in it?

Ok, I created a separate folder called 'models' inside 'core/' and moved my 'binary_classifier.pkl' into that folder. Then, I modified the path to the file like this: binary_clf = joblib.load("core/models/binary_classifier.pkl")

It worked, but I have no idea why

Back to Top