I am trying to use python's charting library from my java project using jython. In my script I use module cairo.
pythonInterpreter.exec("import sys");
pythonInterpreter.exec("import cairo");
Exception throws on the second line, because cairo's init.py contains string:
from _cairo import *
The exception I get is:
Caused by: Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/cairo/__init__.py", line 1, in <module>
from _cairo import *
ImportError: No module named _cairo
Also, I can use my script from console, so I doubt that it has an error in it. I suppose something is wrong with the way I'm importing cairo in Java.
I got it now. Jython can't execute 'native' libraries, such as cairo (I'm, sure it's native, because _cairo is a .so file, which means it's a C lib)
Related
I am trying to make a Bluetooth app with Kivy so I need to use java classes, I get this error message when I use Pyjnius
Traceback (most recent call last):
File "C:\Users\Omer Akbas\Desktop\My Apps (Kivy)\First\main.py", line 3, in
<module>
BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
File "C:\Python27\lib\site-packages\jnius\reflect.py", line 158, in
autoclass
c = find_javaclass(clsname)
File "jnius\jnius_export_func.pxi", line 25, in jnius.find_javaclass
JavaException: Class not found 'android/bluetooth/BluetoothAdapter'
If you're building a Kivy app, pyjnius is installed in the APK automatically as a dependency of Kivy, so you don't have to do anything special beyond the normal Programming Guide ยป Create a package for Android documentation.
I am using jdk 1.8 and Jython 2.7.0.jar to execute my python Code.
But It seems Jython does not support Python's 2.7 feature 'reportlab' module.
My Java Code :
package test;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Test{
public Test()
{
System.out.println("Done!");
engine.eval("import sys");
engine.eval("print sys");
engine.eval("import reportlab");
engine.eval("print reportlab");
System.out.println("Done Here");
}
catch (ScriptException ex)
{
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex);
}
}
public static void main(String args[])
{
Test t=new Test();
}
}
Error I got:
run:
Done!
<module 'sys' (built-in)>
javax.script.ScriptException: ImportError: No module named reportlab in <script> at line number 1
Dec 28, 2015 8:57:52 PM test.Test <init>
SEVERE: null
javax.script.ScriptException: ImportError: No module named reportlab in <script> at line number 1
at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:202)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:42)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:31)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at test.Test.<init>(Test.java:28)
at test.Test.main(Test.java:40)
Caused by: Traceback (most recent call last):
File "<script>", line 1, in <module>
ImportError: No module named reportlab
at org.python.core.Py.ImportError(Py.java:328)
at org.python.core.imp.import_first(imp.java:877)
at org.python.core.imp.import_module_level(imp.java:972)
at org.python.core.imp.importName(imp.java:1062)
at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
at org.python.core.PyObject.__call__(PyObject.java:431)
at org.python.core.__builtin__.__import__(__builtin__.java:1232)
at org.python.core.imp.importOne(imp.java:1081)
at org.python.pycode._pyx2.f$0(<script>:1)
at org.python.pycode._pyx2.call_function(<script>)
at org.python.core.PyTableCode.call(PyTableCode.java:167)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1386)
at org.python.core.__builtin__.eval(__builtin__.java:497)
at org.python.core.__builtin__.eval(__builtin__.java:501)
at org.python.util.PythonInterpreter.eval(PythonInterpreter.java:259)
at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:40)
... 4 more
BUILD SUCCESSFUL (total time: 2 seconds)
If anyone could suggest me an Alternate way of remotely executing a Python script I'd be thankful to them.
Also I have a large code base in python of which rewriting in java is not feasible in scheduled time.
Jython is a completely different (and independent) Python interpreter than your system's Python. While most libraries from Python's standard library will exist in Jython, third party modules installed in your system's Python simply don't exist for the Jython version.
Jython is compatible enough that you can set a Python virtualenv with the Jython interpreter and install modules that are pure-python code (i.e. no binary modules) in that virtualenv - I don't think repotlab qualifies as such - but you might try. In a Java project setup, probably your project have its own instance of the Jython interpreter installed along other .jar files - it should feature a site-packages folder somewhere where you could try to copy pure Python third-party modules to, but again, I don't think reportlab will be jython compatible.
One option for you is to use Python itself (cPython) in an external process, and call it from Java (or Jython) using xmlrpc code - (A quickly googling didn't find me out a way to call cPython via xmlrpc from pure Java - but I know it works from Jython, if you use Python's stdlib xmlrpc as documented -
https://docs.python.org/3/library/xmlrpc.html?highlight=xmlrpc
I've got a solution on a stackExchange link - Jython embedded in Java -- ImportError: No module named yaml i.e I can simply add the packages I need under my Jython Installation directory E.g jython/Lib/site-packages I've got no Issues on reportlab for Python2.7.
most Important also append them in your program using sys.path.append()
I tried to set up the Reddit API in Linux Mint 16, so that I can use it in IntelliJ 13.1.2/Java.
So I used this guide: https://github.com/reddit/reddit/wiki/Install-guide
and started with
$ git clone https://github.com/reddit/reddit.git
next I went to r2
$ cd reddit/r2
, but when I tried to do the next 3 lines:
$ python setup.py build
$ sudo python setup.py develop
$ make
my console gave out
Traceback (most recent call last):
File "setup.py", line 35, in <module>
pkg_resources.require("distribute>=0.6.16")
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 696, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 594, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: distribute>=0.6.16
respectively
Traceback (most recent call last):
File "setup.py", line 35, in <module>
pkg_resources.require("distribute>=0.6.16")
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 696, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 594, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: distribute>=0.6.16
and in the end at the "make" command the console gave out:
Traceback (most recent call last):
File "Makefile.py", line 24, in <module>
from r2.lib.translation import I18N_PATH
File "/home/fox/reddit/r2/r2/lib/translation.py", line 28, in <module>
from babel.messages.extract import extract_javascript
ImportError: No module named babel.messages.extract
Makefile:41: *** Makefile.py failed. aborting. Schluss.
And now I don't have any clue what I should do. It's the first time I use a API and have zero experience with this. It would be very nice if someone of you could help me.
I had the same problem and solved it by running :
easy_install distribute
I am trying to make use of apache lucene in jython27. To do so, I set the CLASSPATH:
% echo $CLASSPATH
/opt/lucene-4.5.0/core/lucene-core-4.5.0.jar:/opt/lucene-4.5.0/queryparser/lucene-queryparser-4.5.0.jar:analysis/common/lucene-analyzers-common-4.5.0.jar:/opt/lucene-4.5.0/demo/lucene-demo-4.5.0.jar
I found some examples that I tried to follow. However, it failed to import the standard module to get the StandardAnalyzer().
>>> from org.apache.lucene import analysis
>>> dir(analysis)
['Analyzer', 'AnalyzerWrapper', 'CachingTokenFilter', 'CharFilter', 'NumericTokenStream', 'Token', 'TokenFilter', 'TokenStream', 'TokenStreamToAutomaton', 'Tokenizer', '__name__', 'tokenattributes']
>>> from org.apache.lucene.analysis import standard
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name standard
Has someone an idea what went wrong?
Ah damn.. I set the CLASSPATH wrong. This works:
% export CLASSPATH=$CLASSPATH:/opt/lucene-4.5.0/analysis/common/lucene-analyzers-common-4.5.0.jar
I run windows 7 on a 64bit system.
I use netbeans 6.9.1 (the python plug ins are installed)
Installed also: Python 2.7.1(64), JDK 6 update 24(64),
I'm interested in term extraction so I downloaded montylingua which is supposed to run in Java or/and Python.
this is the link: http://web.media.mit.edu/~hugo/montylingua/
When I try to run the Sample.java code included in the zip file available, I encounter these problems:
****** MontyLingua v.2.1 ******
***** by hugo#media.mit.edu *****
Failed to get environment, environ will be empty: (0, 'Failed to execute command ([\'sh\', \'-c\', \'env\']):
java.io.IOException: Cannot run program "sh": CreateProcess error=2, The system cannot find the file specified')
java.lang.ExceptionInInitializerError
Caused by: Traceback (innermost last):
File "C:\work\montylingua-2.0\copy\JMontyLingua.py", line 0, in __init__
File "C:\work\montylingua-2.0\copy\MontyLingua.py", line 0, in __init__
File "C:\work\montylingua-2.0\copy\MontyLemmatiser.py", line 0, in __init__
IOError: File not found -
at org.python.core.Py.IOError(Py.java)
at org.python.core.PyFile._setup(PyFile.java)
at org.python.core.PyFile.<init>(PyFile.java)
at org.python.core.__builtin__.open(__builtin__.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java)
at org.python.core.PyObject.__call__(PyObject.java)
at montylingua.MontyLemmatiser$_PyInner.__init__$5(MontyLemmatiser.java:5312)
at montylingua.MontyLemmatiser$_PyInner.call_function(MontyLemmatiser.java:5219)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyFunction.__call__(PyFunction.java)
at org.python.core.PyInstance.__init__(PyInstance.java)
at org.python.core.PyClass.__call__(PyClass.java)
at org.python.core.PyObject.__call__(PyObject.java)
at montylingua.MontyLingua$_PyInner.__init__$1(MontyLingua.java:218)
at montylingua.MontyLingua$_PyInner.call_function(MontyLingua.java:163)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyFunction.__call__(PyFunction.java)
at org.python.core.PyInstance.__init__(PyInstance.java)
at org.python.core.PyClass.__call__(PyClass.java)
at org.python.core.PyObject.__call__(PyObject.java)
at montylingua.JMontyLingua$_PyInner.__init__$1(JMontyLingua.java:85)
at montylingua.JMontyLingua$_PyInner.call_function(JMontyLingua.java:63)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyTableCode.call(PyTableCode.java)
at org.python.core.PyFunction.__call__(PyFunction.java)
at org.python.core.PyInstance.__init__(PyInstance.java)
at org.python.core.Py.initProxy(Py.java)
at montylingua.JMontyLingua.__initProxy__(JMontyLingua.java:256)
at montylingua.JMontyLingua.<init>(JMontyLingua.java:234)
at montylinguaapplication.MontySample.<clinit>(MontySample.java:8)
Could not find the main class: montylinguaapplication.MontySample. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
I am familiar with Java since I have some small programming experience with it. I know nothing about Python.
I have set the environment variable "MONTYLINGUA" which is necessary for the program to run.
I have seen a post about Jython from somebody who gets the same first error as me, but I was not able to do anything from the things proposed. (the link of the other post: Jython - javaos.getenv() gives "Failed to get environment, environ will be empty")
Anyone who can run this .java file correctly may recommend something.
I can upload the java code also, but I think it is obsolete since it is available and easy to find in the site mentioned...
In the project I included also the JyDT_Update_Site_1.4.15 zip, just in case that this would help, but unfortunately nothing happened.
Maybe I have to add some code in a specific file, like in the case of the other post, but I don't know in which to add what, in my system.
thank you for you time.
Looks to me like the same issue as the one from the post you linked.
'Failed to execute command ([\'sh\',
\'-c\', \'env\']):
java.io.IOException: Cannot run
program "sh":
Implies that jython is configured to think it's running on a UNIX system of some sort.
The solution is to set in the Jython registry file the property python.os.
You can locate the registry file following these instructions: http://www.jython.org/archive/21/docs/registry.html#finding.
It looks like the python code is trying to run a shell command.
But, since you're on Windows, this does not work.
It makes me wonder if this is really the canonical way to get at the environment in python?
You will need the Sun Java compiler, version 1.4.2, installed on your system.
Without it, you will have trouble runningthe MontyLingua Java examples, and
we are not sure whether you you can compile MontyLingua into a .NET DLL.
Search www.sun.comfor version 1.4.2 of the Java SE Development Kit and install
it.