Jython中PyCrypto的导入问题
我现在正在尝试在jython中运行python的bittorrent tracker,但遇到了一个问题:这个tracker使用了PyCrypto库,我已经为我的平台编译了这个库,并把它添加到了python的路径中。然而,当我尝试运行代码时,却出现了以下错误:
Exception in thread "MainThread" Traceback (most recent call last):
File "./python_dep/BitTorrent-5.2.2/bittorrent-tracker.py", line 21, in <module>
from BitTorrent.track import track
File "./python_dep/BitTorrent-5.2.2/BitTorrent/track.py", line 50, in <module>
from BitTorrent.UI import Size
File "./python_dep/BitTorrent-5.2.2/BitTorrent/UI.py", line 37, in <module>
from BitTorrent.MultiTorrent import UnknownInfohash, TorrentAlreadyInQueue, TorrentAlreadyRunning, TorrentNotRunning
File "./python_dep/BitTorrent-5.2.2/BitTorrent/MultiTorrent.py", line 25, in <module>
from BitTorrent.Torrent import Feedback, Torrent
File "./python_dep/BitTorrent-5.2.2/BitTorrent/Torrent.py", line 32, in <module>
from BitTorrent.ConnectionManager import ConnectionManager
File "./python_dep/BitTorrent-5.2.2/BitTorrent/ConnectionManager.py", line 22, in <module>
from BitTorrent.Connector import Connector
File "./python_dep/BitTorrent-5.2.2/BitTorrent/Connector.py", line 27, in <module>
from Crypto.Cipher import ARC4
ImportError: cannot import name ARC4
Java Result: 1
我很确定这个库在python路径中,因为命令
import Crypto.Cipher
可以正常工作,而
from Crypto.Cipher import ARC4
却不行。 我运行的java代码如下:
package jythTest;
import org.python.util.PythonInterpreter;
public class Main {
public static void main(String[] args) {
PythonInterpreter pythonInterpreter = new PythonInterpreter();
pythonInterpreter.exec("import sys");
pythonInterpreter.exec("sys.path.append(\"./python_dep/BitTorrent-5.2.2/\")");
pythonInterpreter.exec("sys.path.append(\"./python_dep/Twisted-10.0.0/\")");
pythonInterpreter.exec("sys.path.append(\"./python_dep/Zope-3.4.0/build/lib.linux-i686-2.6\")");
pythonInterpreter.exec("sys.path.append(\"./python_dep\")");
pythonInterpreter.exec("sys.path.append(\"./python_dep/pycrypto-2.0.1/build/lib.linux-i686-2.6\")");
pythonInterpreter.exec("sys.path.append(\"import Crypto.Cipher\")");
//pythonInterpreter.exec("print sys.path");
pythonInterpreter.execfile("./python_dep/BitTorrent-5.2.2/bittorrent-tracker.py");
}
}
非常感谢任何能够提供帮助的人。
2 个回答
我不确定这是否适合你的情况,但我在网上查找时发现了这些信息:
(来自 http://wiki.python.org/jython/JythonFaq/InstallingJython)
Jython找不到你的Java类,尽管它在类路径中是存在的。这会显示为“ImportError: cannot import name xxx”或者“AttributeError: java package xxx' has no attribute 'yyy”
这种情况发生在Jython作为Java扩展安装时(也就是说,当jython.jar放在java\jre\lib\ext目录下),而你的类则安装在类路径中。
原因是Java扩展只能看到其他扩展,而看不到在CLASSPATH中定义的其他类,或者通过--classpath选项传递给Java的类。
解决这个问题有两种方法:
1) 把你的类移动到java\jre\lib\ext目录。
2) 从java\jre\lib\ext目录中删除jython.jar,然后把jython.jar放到CLASSPATH中,或者使用java --classpath选项。
(来自Jython用户邮件列表)
还有一个类似的问题,但还是有些不同:
(来自 http://bugs.jython.org/issue1878866)
我在Linux上使用jython 2.5遇到了类似的问题。在jython2.5.0/Lib/site-packages目录下,我有一个foo目录,里面有一个Java类(Bar.class)和一个jython类(BarPy.py)。我还放了一个空的__init__.py文件。在jython解释器环境中,我总是可以这样导入Bar:“from foo import Bar”,但是我无法导入BarPy。如果我从目录中删除Java类,那么我就可以导入jython脚本。
这可能是因为pycrypto是一个C语言写的扩展,而Jython无法直接调用它,因为需要一个Java的包装器来使用这个扩展。