I have a C# application that stores python script files (*.py) as strings. I load them using:
scriptEngine.CreateScriptSourceFromString(code);
But now I have multiple script files with dependencies between them (imports). In order to handle dependencies, I could save all the strings back to files in a folder and load the script that i want to execute using:
scriptEngine.CreateScriptSourceFromFile(filePath);
but this would make all the script files visible. Is there a way to achieve this in a in-memory way, so that the script files are not first saved to disk but loaded from the strings directly?
TL;DR: 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 the 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 first save the scripts into a folder. Note: the code is just a simplified example of what I have.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…