How do I save a joblib dump to another folder?

Viewed 3360

My target structure:

  • tools
    • model_maker.py
  • models
    • models go here

My current code, which sits in the tools directory

joblib.dump(pipeline, "../models/model_full_June2020.jl")

Throws an error:

FileNotFoundError: [Errno 2] No such file or directory: '../models/model_full_June2020.jl'

I guess my path is wrong. If I remember correctly windows paths and python are different than python and other systems. When I deploy this thing to heroku, I guess it needs to work regardless of the specific system it rests on.

2 Answers

As @john mentioned, this does not mean that tools directory is the current dir.

Set a relative path programmatically rather than assuming that your cwd is in the tools dir. You can do the following

In the file that has the script, you want to do something like this:

import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'relative/path/to/file/you/want')
joblib.dump(pipeline, filename )

This should correctly locate the file since the filename yields the absolute path.

You can just simply try it. I did this for LinearRegression.

joblib.dump(lr,'models/rent_lr_model')
Related