2015-04-23 15 views
5

ho due file python: mainfile.py e subfile.pyIronPython w/C# - Come leggere i valori di Python variabili

mainfile.py si basa su alcuni tipi di subfile.py.

mainfile.py ha un aspetto simile a questo.

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

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

che sto tentando di caricare mainfile.py in C# e ottenere il valore di my_varaible, ma sto avendo qualche difficoltà a trovare risorse che descrivono adeguatamente le relazioni tra i metodi sto chiamando ed io, devo ammettere che non ne so nulla su Python.

Ecco quello che ho scritto:

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

Ma quando questo viene eseguito, theValue è nullo.

Non ho davvero idea di cosa sto facendo. Quindi la vera domanda è:

Come faccio a leggere il valore di my_variable da mainfile.py? Tangenzialmente, esiste una buona risorsa introduttiva per i metodi disponibili nello spazio dei nomi Python e come interagire realmente tra C# e Python?

risposta

1

Dopo alcuni altri scavi, ho scoperto un articolo e una domanda StackOverflow che è stata utile.

SO: IronPython Integration in C#

MSDN Blog: Hosting IronPython in C#

Il codice che finalmente ha funzionato per 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". 
3

In realtà c'è un modo più semplice per farlo, usando 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"); 
Problemi correlati