2015-04-23 5 views
5

Ich habe zwei Python-Dateien: mainfile.py und subfile.pyIron w/C# - Wie Werte von Python-Variablen lesen

mainfile.py auf einige Arten in subfile.py beruht.

mainfile.py sieht in etwa so aus.

from subfile import * 
my_variable = [1,2,3,4,5] 

def do_something_with_subfile 
    #Do something with things in the subfile. 
    #Return something. 

Ich versuche mainfile.py in C# zu laden und den Wert von my_varaible zu bekommen, aber ich bin eine Schwierigkeit der Suche nach Ressourcen hat, die die Beziehungen zwischen den Methoden angemessen beschreiben Ich rufe und ich, zugegebenermaßen, ich weiß nicht viel über Python.

Hier ist, was ich geschrieben habe:

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); 

Aber wenn dies ausgeführt wird, ist theValue null.

Ich habe wirklich keine Ahnung, was ich tue. Die eigentliche Frage ist also:

Wie lese ich den Wert von my_variable aus mainfile.py? Tangential gibt es eine gute Einführungsressource für die Methoden, die im Python-Namespace verfügbar sind und wie man wirklich zwischen C# und Python interagiert?

Antwort

1

Nach etwas mehr Graben entdeckte ich einen Artikel und eine StackOverflow Frage, die hilfreich war.

SO: IronPython Integration in C#

MSDN Blog: Hosting IronPython in C#

Der Code, der schließlich für mich gearbeitet ist:

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". 
3

Es ist eigentlich ein einfacher Weg, es zu tun, mit 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");