2013-08-01 70 views
4

我的C#代碼:的IronPython無法導入模塊 「OS」

//Create the ScriptRuntime 
engine = Python.CreateEngine(); 
//Create the scope for the ScriptEngine 
scope = engine.CreateScope(); 


string pyfile = "D:\\MyAddin\\test.py"; 
ScriptSource source = engine.CreateScriptSourceFromFile(pyfile); 
var rt = source.Execute(scope); 

和我test.py:

import os 
import sys 
... 
print("test") 
... 

我會在編譯的時候沒有問題,但在運行VS給我一個錯誤「不能導入模塊」os「」。錯誤在哪裏?

回答

5

在您的代碼中託管IronPython時,您需要將庫添加到您的路徑中。並非所有的都會默認包含在內。

您可以通過發動機添加:

var engine = Python.CreateEngine(); 
var paths = engine.GetSearchPaths(); 
paths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib"); 
//paths.Add(@"C:\Python27\Lib"); // or you can add the CPython libs instead 
engine.SetSearchPaths(paths); 

或通過腳本:

import sys 
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib') 
import os 
+0

但Python腳本將被安裝在正確的蟒蛇路徑?不在鐵蟒安裝路徑中 – pyd