JamesHudson81
JamesHudson81

Reputation: 2273

why I cannot load the pickle in a different module?

I created a pickle in module1 called tabla_precios and I am looking forward to open it in module2. The pickle opens in a function in module1.

Currently I am importing the function in module2 , but I believe it is not the most efficient way because I think I am making the pickle each time I execute the module in the different module ain't I?

Therefore I thought in importing the pickle directly in a different module but Im not sure on how to do it.

This is what i have tried:

import pandas as pd
import pickle

main_df=pd.DataFrame()

pickle_out=open ('tabla_precios.pickle','wb')
pickle.dump(main_df,pickle_out)
pickle_out.close()

print(tabla_precios)

It comes the error:

name 'tabla_precios' is not defined

I am looking forward to import the pickle in a different module.

Upvotes: 1

Views: 802

Answers (1)

user2682863
user2682863

Reputation: 3218

pickle only stores references to classes and functions, not the functions themselves. If you pickle a class, the class definition needs to be in the namespace when you unpickle it. Modules in python are only imported once and then saved in sys.modules. Re-importing this module will not caused the code to be executed more than once. To test it, try adding a print statement in the module and see if it's called more than once.

Upvotes: 1

Related Questions