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.

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
  • 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 ... – Simon Opelt Nov 16 '15 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" – Henrik Nov 16 '15 at 13:00
  • Why don't use the `Process` instance? – Felipe Oriani Nov 16 '15 at 13:06
  • @Simon Opelt The line is this -> compiled.Execute(scope); – Razvan Ghena Nov 16 '15 at 13:20
  • @RazvanGhena - I was talking about python line information. There should be a `Line` and a `Column` property on `SyntaxErrorException`. – Simon Opelt Nov 16 '15 at 13:22
  • @Henrik , yup, i tried with simple scripts and that is working... – Razvan Ghena Nov 16 '15 at 13:24
  • @Felipe Oriani , I thought about it, I even tried it, but i got an error 2..it says it can't find a path to a file that I Pickel...I didn't find any info's about where .NET search for that file...I repeat...in Python IDLE the script is working – Razvan Ghena Nov 16 '15 at 13:25
  • That is not a repeat.. it's the first mention of having actually tested the script in python.. – Henrik Nov 16 '15 at 13:26
  • @Simon Opelt Ok, i checked the Detail page for the exception, and it says a line in my script that does not even exist..is like 379 and my script is 120 lines long ...I don't understand anymore... – Razvan Ghena Nov 16 '15 at 13:32
  • just to be sure, have you tried setting paths in the python script? : `sys.path.append(r"C:\Users\Desktop\WinPython-64bit-3.4.3.6\python-3.4.3.amd64\Lib")` – Henrik Nov 16 '15 at 13:37
  • @Henrik I tried it now, i had added in the begging of the script the sys.path... still dont work in C# – Razvan Ghena Nov 16 '15 at 13:51
  • 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.. – Henrik Nov 16 '15 at 14:11
  • @Henrik sorry for my delayed answer, I was busy with something else; my version of python is 3.4 and ironoython is 2.7..i can't go back to python 2.7 because the project in python is pretty complex and I don't have resources to rewrite it. I am not a native Python programmer that's why I need to make this software as a module for my .NET application. – Razvan Ghena Nov 19 '15 at 14:12
  • did you try pythonnet? – denfromufa Dec 10 '15 at 17:08

1 Answers1

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?

Community
  • 1
  • 1
Henrik
  • 2,180
  • 16
  • 29