6

私のC#コード:

//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" をインポートできません」というエラーが表示されます。エラーはどこにありますか?

4

1 に答える 1

7

コードで 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
于 2013-08-01T17:25:05.893 に答える