IronPython w / C # - How to read Python variable values

I have two python files: mainfile.py and subfile.py

mainfile.py relies on some types in the subfile.py file.

mainfile.py looks something like this.

from subfile import * my_variable = [1,2,3,4,5] def do_something_with_subfile #Do something with things in the subfile. #Return something. 

I am trying to load mainfile.py in C # and get the value my_varaible, but it's hard for me to find resources that adequately describe the relationship between the methods that I call, and I admittedly Know a ton about Python.

Here is what I wrote:

 var engine = Python.CreateEngine(); //Set up the folder with my code and the folder with struct.py. var searchPaths = engine.GetSearchPaths(); searchPaths.Add(@"C:\ACodeFolder"); searchPaths.Add(@"C:\tools\python\lib"); engine.SetSearchPaths(searchPaths); //A couple of files. var mainfile = @"C:\ACodeFolder\mainfile.py"; var subfile = @"C:\ACodeFolder\subfile.py"; var scope = engine.CreateScope(); var scriptSource = engine.CreateScriptSourceFromFile(subfile); var compiledScript = scriptSource.Compile(); compiledScript.Execute(scope); scriptSource = engine.CreateScriptSourceFromFile(mainfile); compiledScript = scriptSource.Compile(); compiledScript.Execute(scope); scriptSource = engine.CreateScriptSourceFromString("my_variable"); scriptSource.Compile(); var theValue = compiledScript.Execute(scope); 

But when this is done, the value of Value is null.

I really don't know what I'm doing. So the real question is:

How to find out my_variable value from mainfile.py? Tangentially, is there a good introductory resource for the methods available in the Python namespace, and how to really interact between C # and Python?

+5
source share
2 answers

After some extra digging, I found an article and a StackOverflow question that was helpful.

SO: IronPython integration in C #

MSDN Blog: IronPython Hosting in C #

Code that finally worked for me:

 var engine = Python.CreateEngine(); //Set up the folder with my code and the folder with struct.py. var searchPaths = engine.GetSearchPaths(); searchPaths.Add(@"C:\ACodeFolder"); searchPaths.Add(@"C:\tools\python\lib"); engine.SetSearchPaths(searchPaths); var mainfile = @"C:\ACodeFolder\mainfile.py"; var scope = engine.CreateScope(); engine.CreateScriptSourceFromFile(mainfile).Execute(scope); var expression = "my_variable"; var result = engine.Execute(expression, scope); //"result" now contains the value of my_variable". 
+1
source

There is actually an easier way to do this using ScriptScope.GetVariable :

 var engine = Python.CreateEngine(); //Set up the folder with my code and the folder with struct.py. var searchPaths = engine.GetSearchPaths(); searchPaths.Add(@"C:\ACodeFolder"); searchPaths.Add(@"C:\tools\python\lib"); engine.SetSearchPaths(searchPaths); var mainfile = @"C:\ACodeFolder\mainfile.py"; var scope = engine.CreateScope(); engine.CreateScriptSourceFromFile(mainfile).Execute(scope); var result = scope.GetVariable("my_variable"); //"result" now contains the value of my_variable. // or, attempt to cast it to a specific type var g_result = scope.GetVariable<int>("my_variable"); 
+3
source

All Articles