Reputation: 21
I am trying at my current application to integrate a pretty complex Python script in .NET platform using IronPython as a bridge.
In my python script I use NLTK and also some other string classifiers like sklearn.naive_bayes
, this are my import's:
import nltk
from nltk.classify.scikitlearn import SklearnClassifier
import pickle
from sklearn.naive_bayes import MultinomialNB, BernoulliNB
from sklearn.linear_model import LogisticRegression, SGDClassifier
Also I have in this script a function that receive as a parameter a string and return some output example:
def testFunction(text):
#do some things
return somethings
I want to call this function from my .NET application, I am using this code for calling the function:
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);
When I try to execute the code I get the following eror:
SyntaxErrorException -> unexpected token 'from'.
I want to call the testFunction
with a string and then use the output if that function in .NET.
Upvotes: 2
Views: 1639
Reputation: 2229
Ok.
After some clarifying comments, I believe I dare to answer now.
Ironpython is (at the moment) ironpython 2.7 (corresponding to python 2.7) from this line:
string dir = @"C:\Users\Desktop\WinPython-64bit-3.4.3.6\python-3.4.3.amd64\Lib";
it is clear that you are trying to load modules from python 3.4
In comments, it was mentioned that Idle was used for testing the python script, but Idle will be using the installed version of Python (or if multiple versions installed, one Idle installation for each of them)
In this case the Idle IDE you tested in used Python 3.4
Python 2.x and Python 3.x is two different programming ${interfaces / compilers / interpreters}. The language used for scripting them is not 100% the same.
Python 3 is not fully backwards compatible to python 2. Since some syntax is modified. (unlike .NET where new versions of the compiler support old code).
here is a link to some of the key differences: http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html
Try using ironPython3, instead of ironPython, it is in my experience not completely matured, and I wouldn't use it in a production environment yet, but if you really depend on python 3 modules, and needs to run python as a .Net compatible script engine, this is my recommendation.
https://github.com/IronLanguages/ironpython3
Otherwise do yourself a favor and look into the options for finding the same python modules as version 2.x compatible or modify them yourself, by inserting python version identification blocks
How do I check what version of Python is running my script?
Upvotes: 1