0

I would like to save to a file multiple instances of a custom Python class.
The class is Loess, taken from https://github.com/joaofig/pyloess, which performs localised regression.

Below's a MWE of the saving process:

import pickle
import numpy as np
from Loess import Loess

xarr = np.linspace(0, 4, 100) * np.pi
yarr = 2*np.sin(xarr) + np.random.rand(len(xarr))

loess = Loess(xarr, yarr)

with open("localised_regression.pkl", "wb") as output:
    pickle.dump(loess, output)

and now the retrieval process

import pickle

with open("localised_regression.pkl", 'rb') as input_:
    localised_regression = pickle.load(input_)

When I do this on a jupyter notebook (run the first snippet on one notebook and the second on another) it works perfectly.
Dumping the instance of Loess from a notebook and retrieving it from terminal or another machine, it doesn't work.
I get ModuleNotFoundError: No module named 'Loess' error message

I even tried importing the module in python session where I attempt the retrieval, but nothing changes.

It seems that it only works from within the same location where the dumping was performed.
I'm using Python 3.7.7 and the same conda environment for both Python shell and jupyter notebook.

I examined other answers (like how to save/read class wholly in Python) but no luck.

I've tried saving to numpy file, but same story.
I've also tried dumping with marshal and json, but both complained.

Does anybody have a suggestion on how to solve this? Thank you

andrea
  • 525
  • 1
  • 5
  • 21
  • You have to import `Loess` whereas you load ` Loess` object from pickle – JayPeerachai Aug 27 '20 at 10:45
  • I am importing the `Loess` module prior to loading the instance store in the pickle file. I can't find any `import` method in the `pickle` module – andrea Aug 27 '20 at 11:28

0 Answers0