3

我目前正在尝试让 python bittorrent 跟踪器在 jython 内运行,我遇到了这个问题:跟踪器使用我为我的平台编译并添加到 python 路径中的 PyCrypto 库。但是,当我尝试运行代码时,出现以下错误:

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;

导入 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");
    }
}

提前感谢任何可以提供任何帮助的人。

4

2 回答 2

4

发生这种情况可能是因为 pycrypto 是一个 C 扩展,并且 Jython 将无法在没有此扩展的 Java 包装器的情况下调用它。

于 2010-06-10T19:25:59.357 回答
0

我不确定这是否适用于您的情况,但一些谷歌搜索导致了这一点:

(来自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

我在使用 jython 2.5 的 Linux 中遇到了类似的问题。在 jython2.5.0/Lib/site-packages 中有一个 foo 目录,其中有一个 Java 类 (Bar.class) 和一个 jython 类 (BarPy.py)。我还放了一个空的 __init__.py 文件。在 jython 解释器环境中,我总是可以像这样导入 Bar:“from foo import Bar”但是我不能导入 BarPy。如果我从目录中删除 java 类,那么我可以导入 jython 脚本

于 2010-06-10T19:21:48.517 回答