我想在我當前的應用程序中使用IronPython作爲橋將一個非常複雜的Python腳本集成到.NET平臺中。在.NET C中集成Python腳本#
在我的Python腳本我使用NLTK,還有一些其他的字符串分類像sklearn.naive_bayes
,這是我進口的:
import nltk
from nltk.classify.scikitlearn import SklearnClassifier
import pickle
from sklearn.naive_bayes import MultinomialNB, BernoulliNB
from sklearn.linear_model import LogisticRegression, SGDClassifier
而且我在這個腳本接收的參數字符串,返回一些功能輸出示例:
def testFunction(text):
#do some things
return somethings
我想從我的.NET應用程序中調用這個函數,我使用這個代碼調用函數:
ScriptEngine engine;
ScriptScope scope;
ScriptSource source;
CompiledCode compiled;
engine = Python.CreateEngine();
scope = engine.CreateScope();
ICollection<string> paths = engine.GetSearchPaths();
string dir = @"C:\Users\Desktop\WinPython-64bit-3.4.3.6\python-3.4.3.amd64\Lib";
paths.Add(dir);
string dir2 = @"C:\Users\Desktop\WinPython-64bit-3.4.3.6\python-3.4.3.amd64\Lib\site-packages";
paths.Add(dir2);
engine.SetSearchPaths(paths);
//loading and compiling code
source = engine.CreateScriptSourceFromFile(@"C:\Users\Desktop\script.py");
compiled = source.Compile();
//now executing this code (the code should contain a class)
compiled.Execute(scope);
當我嘗試執行代碼,我得到以下eror:
SyntaxErrorException -> unexpected token 'from'.
我想打電話給testFunction
一個字符串,然後在.NET中使用的輸出,如果該功能。
異常應具有行信息。你可以添加到你的問題?是否有任何庫使用本地模塊/庫?這將是一個問題...... –
據我所知,.NET部分沒有錯.. 因此,錯誤必須在python腳本中。 您是否嘗試過更簡單的事情 - 如 print「test」 – Henrik
爲什麼不使用'Process'實例? –