Having trouble with the file path on open() function in python3

Viewed 79

I'm trying to load a pickle file on my machine learning API, but I keep getting path errors.

model = pickle.load(open(scaler_filepath, 'rb'))

but then for some reason I get an error that the file is not found: FileNotFoundError: [Errno 2] No such file or directory: '..\\..\\models\\lr_scaler.pkl'

this is how I'm loading the pickle files:

model_path = os.path.join(os.path.pardir,os.path.pardir,'models')

scaler_filepath = os.path.join(model_path, 'lr_scaler.pkl')

model_filepath = os.path.join(model_path, 'lr_model.pkl')

scaler = pickle.load(open(scaler_filepath, 'rb'))

model = pickle.load(open(model_filepath, 'rb'))

this gives me the FileNotFoundError. setting the paths as raw strings didn't work either, but what's insteresting is that if I print to check the paths:

print(model_filepath)

print(os.path.getsize(scaler_filepath))

it returns the correct path and the file size:

..\..\models\lr_model.pkl

1227

so it appears like the paths are correct but for some reason they don't work when I open them inside pickle.load()

even using raw strings

scaler = pickle.load(open(r'..\..\models\lr_scaler.pkl', 'rb'))

model = pickle.load(open(r'..\..\models\lr_model.pkl', 'rb'))

I still get: [Errno 2] No such file or directory: '..\\..\\models\\lr_scaler.pkl'

and switching the \ to / didn't work either. don't know what else to do :(

edit: if I check the paths with print(os.path.exists(model_filepath)), this happens:

True 
True 
* Serving Flask app "machine_learning_api" (lazy loading) 
* Environment: development 
* Debug mode: on 
* Restarting with stat 
False 
False

and I get the FileNotFoundError

1 Answers

tested with

print(os.getcwd())
print(os.path.exists(model_filepath))
print(os.path.exists(scaler_filepath))

and turns out the working directory is being changed so it broke the relative paths when the API restarted.

C:\python\projects\ml-model\src\models
True
True
 * Serving Flask app "machine_learning_api" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Restarting with stat
C:\python\projects\ml-model
False
False

saving the API script on the ml-model folder and setting

model_path = os.path.join(os.getcwd(),'models')

fixed it.

Related