2

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.

14
  • The exception should have line information. Could you add that to your question? Do any of the libraries use native modules/libraries? That will be an issue ... Commented Nov 16, 2015 at 12:59
  • As far as I can tell, there is nothing wrong with the .NET part.. So the error must be in the python script. Have you tried with something simpler - like print "test" Commented Nov 16, 2015 at 13:00
  • Why don't use the Process instance? Commented Nov 16, 2015 at 13:06
  • @Simon Opelt The line is this -> compiled.Execute(scope); Commented Nov 16, 2015 at 13:20
  • 1
    just one more thought... This may seem redundant, but... when you tested in Idle, did Idle then use python 3.4? - ironpython3 is as far as I know not complete, and may not be fully functional. If you installed ironpython 2.7 - and then points to the nltk for python 3, that may give you problems.. Commented Nov 16, 2015 at 14:11

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.