Jython in Java error: ImportError: No module named

1.9k Views Asked by At

I've read A LOT about this matter but nothing seems to work with me right! I'm using Netbeans 8.0 and Python 2.6.5, Jython 2.5.1 (the default from adding Python and Jython Plugin to netbeans) I'm a mac os x 10.9.3 user

I tried to open a new Jython Code where I add the path to python to get it work: /Library/Frameworks/Python.framework/Versions/2.7/bin/python and it does.

I also tried to call a Python code from Java code using Jython and it works for simple print code. But when I add an nltk POS code (which work very fine in Python interpreter) it throws an error: ImportError: No module named nltk

I thried those Paths: /Library/Frameworks/Python.framework/Versions/2.7/bin/python /Library/Python/2.7/site-package

when I call python from terminal the nltk import work fine:

Sosys-MacBook-Pro:~ ha$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/ha/apache-maven-3.2.1/bin:/Users/ha/apache-opennlp-1.5.3/bin:/Users/ha/jython2.5.4rc1:/Users/ha/nltk_data:/Library/Python/2.7/site-packages:/usr/lib/python2.7/
Sosys-MacBook-Pro:downloads ha$ python
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>>

Then I get another error: ImportError: No module named signal. and so on with those no module named... I also tried to add jython-standalone-2.7-b21.jar in the classpath in the Libraries but it doesn't solve this problem.

How can I get it work?

Appendix: my Java Code:

package jythonprojecttes;
import java.lang.*;
import org.python.util.PythonInterpreter;
public class JythonProjectTes {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try
        {       
            System.out.print(java.lang.System.getProperty("java.class.path"));
            PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
            PythonInterpreter interp = new PythonInterpreter();
            interp.execfile("/Users/ha/NetBeansProjects/JythonProjectTes/src/jythonprojecttes/Code.py"); 
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

}

my Python Code:

import nltk

tokenizer = None
tagger = None

def tag(sentences):
    global tokenizer
    global tagger
    tagged = nltk.sent_tokenize(sentences.strip())
    tagged = [nltk.word_tokenize(sent) for sent in tagged]
    tagged = [nltk.pos_tag(sent) for sent in tagged]
    return tagged

def ReadFromTXT():
    # Reading input from a file    
    Question = open('/Users/ha/NetBeansProjects/JythonNLTK/src/jythonnltk/input.txt', 'r')  
    return Question.read()

def PrintToText(tagged):
    # Writing output to file    
    output_file = open('/Users/ha/NetBeansProjects/JythonNLTK/src/jythonnltk/output.txt', 'w')
    output_file.writelines(["%s\n" % item for item in tagged])
    output_file.close()    
    print "Printed to Output.txt"

def main():
    #Only 'What be' kind of questions.
    sentences = ReadFromTXT() 
    print "The Question is:"+sentences
    tagged = tag(sentences)
    PrintToText(tagged)
    print tagged

if __name__ == '__main__': 
    print sys.path
    main()

Thanks in advance

0

There are 0 best solutions below