我尝试使用jython从java调用第三方python API 我在win7,jython 2.5.3下使用了jdk 1.7。 python API包库位于.. \ Lib \ site-packages中 这是我的java代码:
import org.python.core.PyException;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class GetPacLo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws PyException {
// Create an instance of the PythonInterpreter
PythonInterpreter interp = new PythonInterpreter();
// The exec() method executes strings of code
interp.exec("import sys");
interp.exec("import packetlogic2");
interp.exec("print sys");
interp.exec("print packetlogic2");
// Set variable values within the PythonInterpreter instance
interp.exec("pl = packetlogic2.connect(\"192.168.x.x\", \"admin\", \"xxx\")");
interp.exec("print pl.remoteversion");
}
}
它找到了python包,但没有找到它里面的函数。
D:\>java GetPacLo
[module 'sys' (built-in)]
Exception in thread "main" Traceback (most recent call last):
File "<string>", line 1, in <module>
File "__pyclasspath__/packetlogic2/__init__.py", line 325, in connect
File "__pyclasspath__/packetlogic2/__init__.py", line 38, in _find_version
File "__pyclasspath__/packetlogic2/__init__.py", line 30, in _import_api
File "__pyclasspath__/packetlogic2/v12_4/__init__.py", line 1, in <module>
File "__pyclasspath__/packetlogic2/v12_4/pld/__init__.py", line 14, in <module>
File "__pyclasspath__/packetlogic2/v12_4/pld/pld.py", line 18, in <module>
File "__pyclasspath__/packetlogic2/v12_4/pld/thisversion.py", line 4, in <module>
ImportError: cannot import name PLv12_4
但是如果从python解释器调用它会成功。
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys
[module 'sys' (built-in)]
>>> import packetlogic2
>>> pl = packetlogic2.connect("192.168.x.x","admin","xxx")
>>> print pl.remoteversion
13.1.9.50
>>>
从jython-standalone调用它时也失败了,使用与从java调用相同的错误消息。
D:\jython2.5.3>java -jar jython.jar
Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36)
[Java HotSpot(TM) Client VM (Oracle Corporation)] on java1.7.0_04
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import sys
>>> print sys.path
['', 'D:\\jython2.5.3\\Lib', 'D:\\jython2.5.3\\jython.jar\\Lib', '__classpath__', '__pyclasspath__/']
>>> import packetlogic2
>>> print packetlogic2
[module 'packetlogic2' from 'D:\jython2.5.3\Lib\packetlogic2\__init__$py.class']
>>> pl = packetlogic2.connect(“192.168.x.x”,”admin”,”xxx”)
Traceback (most recent call last):
File “”, line 1, in
File “D:\jython2.5.3\Lib\packetlogic2\__init__.py”, line 325, in connect
v, ver = _find_version(host, username, password, port, versions, timeout)
File “D:\jython2.5.3\Lib\packetlogic2\__init__.py”, line 38, in _find_version
tmp_api = _import_api(api)
File “D:\jython2.5.3\Lib\packetlogic2\__init__.py”, line 30, in _import_api
return __import__(‘packetlogic2.%s’ % api, globals(), locals(), ['*'])
File “D:\jython2.5.3\Lib\packetlogic2\v12_4\__init__.py”, line 1, in
import pld
File “D:\jython2.5.3\Lib\packetlogic2\v12_4\pld\__init__.py”, line 14, in
from pld import PLD, StatsZone
File “D:\jython2.5.3\Lib\packetlogic2\v12_4\pld\pld.py”, line 18, in
from thisversion import verify_ip, negotiate_flicka_with_timeout, deprecated alias
File “D:\jython2.5.3\Lib\packetlogic2\v12_4\pld\thisversion.py”, line 4, in
from packetlogic2.v12_4.plapi import PLv12_4
ImportError: cannot import name PLv12_4
非常感谢。
问候,
Tjandra