I have a C # application that stores python script files (* .py) as strings. I download them using:
scriptEngine.CreateScriptSourceFromString(code);
But now I have several script files with dependencies between them (import). To handle the dependencies, I could save all the lines back to the files in the folder and load the script that I want to execute using:
scriptEngine.CreateScriptSourceFromFile(filePath);
but this will make all script files visible. Is there a way to achieve this in memory, so that the script files are not first saved to disk, but loaded from strings directly?
TL; DR: an example of how this might look:
myutils.py:
def SomeMethod(p): print ('SomeMethod(p=%s)' % p)
script1.py:
import myutils; if __name__ == '__main__': myutils.SomeMethod('script1')
script2.py:
import myutils; if __name__ == '__main__': myutils.SomeMethod('script2')
My application has scripts stored as strings. something like
Dictionary<string, string> filePathToContent = new Dictionary<string, string>(); filePathToContent["myutils.py"] = "..."; // The script file content. filePathToContent["script1.py"] = "..."; // The script file content. filePathToContent["script2.py"] = "..."; // The script file content.
I want to call script1.py without having to save the scripts in a folder first. Note: the code is just a simplified example of what I have.
c # ironpython
Robot mess
source share