Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IronPython w/ C# - How to Read Values of Python Variables

Tags:

c#

ironpython

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

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

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'm attempting to load mainfile.py up in C# and get the value of my_varaible, but I'm having some difficulty finding resources that adequately describe the relationships between the methods I'm calling and I, admittedly, don't know a ton about Python.

Here's what I have written:

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 executes, theValue is null.

I really have no idea what I'm doing. So the real question is:

How do I read the value of my_variable 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?

like image 545
Anthony Compton Avatar asked Apr 23 '15 13:04

Anthony Compton


2 Answers

There's actually a simpler way to do it, 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");
like image 197
Jeff Hardy Avatar answered Oct 03 '22 13:10

Jeff Hardy


After some more digging, I uncovered an article and a StackOverflow question that was helpful.

SO: IronPython Integration in C#

MSDN Blog: Hosting IronPython in C#

The code that finally worked for me is:

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".
like image 38
Anthony Compton Avatar answered Oct 03 '22 11:10

Anthony Compton