2010-06-10 37 views
3

我目前正試圖讓python bittorrent跟蹤器在jython中運行,我遇到了這個問題: 跟蹤器使用PyCrypto庫,我爲我的平臺編譯並添加到python路徑中。當我嘗試運行代碼,不過,我得到以下錯誤:Jython中的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"); 
    } 
} 

在此先感謝任何可以提供任何幫助的人。

回答

0

我不知道這適用於您的情況,但一些谷歌上搜索導致了這一點:

(從http://wiki.python.org/jython/JythonFaq/InstallingJython

Jython cannot find your Java class, even though it exists in the class path. This shows up as "ImportError: cannot import name xxx" or "AttributeError: java package xxx' has no attribute 'yyy'"

This happens when Jython is installed as a Java extension (i.e. when jython.jar is installed in java\jre\lib\ext) and your classes are installed in the classpath.

The reason is Java extensions can only see other extensions, not other classes defined in the CLASSPATH or passed in to java using the --classpath option.

There are two ways to fix this:

1) Move your classes to the java\jre\lib\ext directory.

2) Remove jython.jar from the java\jre\lib\ext directory and put jython.jar in the CLASSPATH or use the java --classpath option.

(from the Jython-users mailing list)

而另一個類似的問題,但不同儘管如此:

(從http://bugs.jython.org/issue1878866

I have a similar problem in Linux with jython 2.5. Inside jython2.5.0/Lib/site-packages a have a foo directory where there is a Java class (Bar.class) and a jython class (BarPy.py). i have also put an empty __init__.py file. In the jython interpreter environment I can always import Bar like this: "from foo import Bar" however I cannot import BarPy. If I delete the java class from the directory then I can import the jython script

+0

有趣的是,我沒有將jython安裝爲java擴展,不過我剛剛生成了獨立的lib jar並將其添加到項目 – Arg 2010-06-11 08:38:46

4

發生這種情況可能是因爲pycrypto是一個C擴展,如果沒有這個擴展的Java包裝器,Jython將無法調用它。

+0

hmm但其他C擴展似乎工作正常。此外,它能夠調用模塊本身,只是不會調用功能 – Arg 2010-06-11 08:40:01

+0

什麼擴展名?也許ARC4是擴展,Crypto.Cipher只是純Python中的Python包裝器。 – Tarantula 2010-06-11 17:23:45