I'm trying to learn how to use Jython for a Java Swing project,
I've managed to import the dependency on IntelliJ and I want to test Jython by running some very simple code:
import org.python.util.PythonInterpreter;
public class test {
public static void main(String[] args) {
System.out.println("Java Hello");
PythonInterpreter pi = new PythonInterpreter();
pi.exec("print('Python Hello')");
}
}
The console output is:
Java Hello
Exception in thread "test" Traceback (most recent call last):
File "/Users/a/Desktop/python/Lib/site.py", line 68, in <module>
import os
File "/Users/a/Desktop/python/Lib/os.py", line 50, in <module>
import posixpath as path
File "/Users/a/Desktop/python/Lib/posixpath.py", line 17, in <module>
import warnings
File "warnings.py", line 395, in <module>
File "warnings.py", line 395, in <module>
File "__pyclasspath__/_warnings.py", line 106, in <module>
NameError: name 'ResourceWarning' is not defined
I have not managed to find anything to guide me online,
Any help would be greatly appreciated!
It's an error throws by Python that it can't find Jython library.
As a workaround, add the Jython lib folder to the python classpath.
Then assign python interpreter to your project with updated classpath.
On the pom file, change the scope to: provided.
Cheers,
Mark
Related
I am trying to execute class and functions written in python from java via jython.
It runs fine until in python we encountered "import clr" which is pythonnet.
Can anyone tell how to run the python scripts from java using jython or any thing else which use "import clr"
python code: file.py
import clr
java code using jython:
Properties props = new Properties();
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interp = new PythonInterpreter();
interp.exec("<absoulute_path//file.py>");
Error:
Exception in thread "main" Traceback (most recent call last):
File "<string>", line 3, in <module>
ImportError: No module named clr
Jython does not provide Python C APIs, so there's no way you could build and use Python.NET with it.
Here's my log:
File "/home/foo/bar/revmob/__init__.py", line 11, in <module>
from android import *
File "/home/foo/bar/android.py", line 5, in <module>
RevMobNative = autoclass('com.revmob.RevMob')
File "/usr/local/lib/python3.4/dist-packages/jnius-1.1_dev-py3.4-linux-x86_64.egg/jnius/reflect.py", line 154, in autoclass
c = find_javaclass(clsname)
File "jnius/jnius_export_func.pxi", line 25, in jnius.find_javaclass (jnius/jnius.c:16506)
jnius.JavaException: Class not found b'com/revmob/RevMob'
Does anyone know where this class is in the RevMob files and where I need to put it for jnius to find it? I'm assuming it's just in the wrong location, but I'm not a Java guy.
Turns out it has to be compiled for RevMob to run. Once I'd built it with Buildozer there were no problems. This means you can't use the Kivy Launcher to test apps with RevMob ads.
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 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)
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