2013-08-01 71 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