2015-04-23 30 views
5

İki piton dosyaları var Değerleri okumak için: mainfile.py veIronPython w/C# - Python Değişkenler

mainfile.py subfile.py bazı türlerinde dayanır subfile.py.

mainfile.py şuna benzer. ,

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

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

C# mainfile.py kadar yük ve my_varaible değerini almak için çalışılıyor, ama yeterince ben I arama ve kulüpler yöntemlerle arasındaki ilişkileri açıklayan kaynaklar bulmakta biraz zorluk çekiyorum kuşkusuz, Python hakkında bir ton bilmiyorum.

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

Ama bu çalıştırdığında theValue null:

İşte yazdım zaten.

Gerçekten ben ne yaptığımı hiç bilmiyorum. Yani asıl soru şu:

ben mainfile.py gelen my_variable değerini okumak nasıl? Yüzeysel olarak, Python ad alanında mevcut yöntemler ve nasıl gerçekten C# ve Python arasındaki etkileşim için iyi bir tanıtım kaynak var mı?

cevap

1

biraz daha kazma sonra ben bir makale ve yararlı bir StackOverflow soru ortaya çıkardı.

MSDN Blog: Hosting IronPython in C#

nihayet benim için çalıştı kod

SO: IronPython Integration in C#

geçerli:

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

bunu yapmak için daha basit bir yolu ScriptScope.GetVariable kullanarak, aslında var:

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"); 
İlgili konular