3

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.

Alex Dore
  • 67
  • 1
  • 5
  • 1
    _My current code, which sits in the tools directory_ that doesn't mean tools is the **current directory**. – John Gordon Jun 23 '20 at 14:17
  • try see your current dir using 'import os; print(os.getcwd())', or seeing the path of you script when running following this [ref](https://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory) – Daniel A R F Jun 23 '20 at 14:24
  • Thank you John, this set me down the right path. – Alex Dore Jun 24 '20 at 00:39

2 Answers2

2

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.

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
0

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

joblib.dump(lr,'models/rent_lr_model')
S.B
  • 13,077
  • 10
  • 22
  • 49
docXt
  • 21
  • 1