I'm tryinig to call a script from another one. I need to pass a dictionary to the script I call which will contain some matrices.
The solution I found is to pass through a pickle save in order to reload it in the called script.
I'm wondering if what I did is the only solution or is it possible to pass directly the dictionary to the called script?
Here the minimal example:
file1 (where the script is called):
import subprocess
import sys
import os
import pickle
if __name__ == '__main__':
dictionary = {'a':1,'b':2}
filename_temp = os.path.abspath('filename.pickle')
with open(filename_temp, 'wb') as handle:
pickle.dump(dictionary, handle, protocol=pickle.HIGHEST_PROTOCOL)
subprocess.Popen([sys.executable, os.path.join("", "Subproc.py "), filename_temp], shell=True)
file 2 (the script being called):
import sys
import pickle
class useDict():
def __init__(self,filename_temp=None):
if filename_temp:
with open(filename_temp, 'rb') as handle:
Dictionary = pickle.load(handle)
self.Dictionary = Dictionary
else:
self.Dictionary = None
print(self.Dictionary)
def main(filename_temp=None):
print(filename_temp)
useDict(filename_temp)
if __name__ == '__main__':
if len(sys.argv)>1:
main(sys.argv[1])