0

I wanted to use several thousand instances of my class in another context.

For now, I used a pickle file for that, but ran into severe issues when I tried to load the pickle file in another context. By this, I mean that I have somewhat resembling the following the structure:

main.py
   Extractors
      pickler
   Builder
      pickle_loader

Yet, if I try to load the pickle files with pickle_loder, it always gives a similar error to this.

Is there another, maybe more suitable way how I can store and reload the instances? Since one execution of the Extractors takes around 2h, I do not want to execute them each time I experiment with the Builder.

I have also tried the solutions given, but I can't seem to get it to work with the explicit import statement.

martineau
  • 119,623
  • 25
  • 170
  • 301
dennlinger
  • 9,890
  • 1
  • 42
  • 63
  • Are `Extractors` and `Builder` classes in the same file? – Jared Goguen Jun 15 '17 at 16:57
  • no, that's what I wanted to indicate with the levels. They are in separate files, and do not even share the same subfolder. Only the main.py is located in the topmost folder – dennlinger Jun 15 '17 at 16:58
  • The most straightforward solution would be to add `from ... import Extractors` in the file with `Builder`. So, maybe you want to solve that explicit import statement error you mention. – Jared Goguen Jun 15 '17 at 16:59
  • 1
    which files are you running? If you are putting your files in a package structure they probably shouldn't be run as the main entry point, the key is to not pickle any objects from classes defined in the module you run. – Tadhg McDonald-Jensen Jun 15 '17 at 17:03
  • Well, the problem is that I get the following when doing so @JaredGoguen : `ModuleNotFoundError: No module named 'Institute'`, since this is the statement I get when running from inside the main script. `Institute Out[13]: ` – dennlinger Jun 15 '17 at 17:06
  • The question was more like: Is there an alternative _which does work_ and does not have these annoying limitations of pickle. In my eyes, it doesn't matter _how_ I do it... – dennlinger Jun 15 '17 at 17:07
  • Okay, I got something very hacky, but which seems to work. Changing to the directory with `os.chdir` to the folder of the pickler does the trick, since then the import is simply `import Institute` instead of `import Extractors.Institute`. It's not great, probably unsafe, but it does work for now.... – dennlinger Jun 15 '17 at 17:16

0 Answers0