views:

163

answers:

2

Hello,

I am currently trying to get python bittorrent tracker running inside of jython and i encountered this problem: the tracker uses PyCrypto library which i compiled for my platform and added into the python path. When i try to run code, however, i get following error:

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

I am pretty sure that the library is in the python path, because command

import Crypto.Cipher

works, while

from Crypto.Cipher import ARC4

does not. The java code i run looks like this:

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

Thanks in advance to anyone who could provide any kind of help.

A: 

I am not sure this applies to your situation, but some googling led to this:

(from 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)

And another, similar issue, but different nonetheless:

(from 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

Peter Jaric
interesting, i don't have jython installed as java extension though, i just generated standalone lib jar and added it to the project
Arg
+1  A: 

This is happening probably because pycrypto is a C-extension, and Jython will not be able to call it without a Java wrapper for this extension.

Tarantula
hmm but other C extensions seems to be working fine. Also, it is able to call the module itself, just won't call the functions
Arg
What extensions ? Maybe ARC4 is the extension and the Crypto.Cipher is just a Python wrapper in pure-python.
Tarantula