Cannot import Crypto.Random on python3

Bug #1304583 reported by John Nelson
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Python-Crypto
Invalid
Undecided
Unassigned

Bug Description

Crypto/Random/OSRNG/posix.py:66 contains this statement:
     except IOError, e:

This was valid in py2.x, but not in py3.x; consequently the import fails with a SyntaxError:
>>> import Crypto.Random
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/johnn/lib/python/Crypto/Random/__init__.py", line 28, in <module>
    from Crypto.Random import OSRNG
  File "/home/johnn/lib/python/Crypto/Random/OSRNG/__init__.py", line 32, in <module>
    from Crypto.Random.OSRNG.posix import new
  File "/home/johnn/lib/python/Crypto/Random/OSRNG/posix.py", line 66
    except IOError, e:

Version is 2.6 (fetched using PIP a few hours ago).

Revision history for this message
Legrandin (gooksankoo) wrote :

You are probably using a python2-enabled pip, so this is not a real bug.

Try this:

===
virtualenv -p /usr/bin/python3 py3
. py3/bin/activate
pip install pycrypto

### Try out the Random module. Some warnings may be presented.
cd py3/lib/python3.2/site-packages/Crypto/SelfTest/Random
python __init__.py
===

Revision history for this message
John Nelson (john-nelson2) wrote :

No dice. I didn't have virtualenv installed, so I just got it via "apt-get install python-virtualenv". I am using Ubuntu 12.04 LTS, so it would have just been whatever was in the repo.

After following your steps above, it throws an ImportError on this line:
  File "/home/johnn/lib/python/Crypto/SelfTest/__init__.py", line 35, in <module>
    from StringIO import StringIO

Interestingly, this is yet another 2-3 translation issue (because StringIO now in io module). I also tried re-importing Crypto.Random again from within the virtualenv, but still get the same SyntaxError as before, which doubly suggests a translation issue at install time.

Revision history for this message
Legrandin (gooksankoo) wrote :

I was using Ubuntu 12.04 too and it behaved correctly.
Can you post the complete terminal log?

Revision history for this message
John Nelson (john-nelson2) wrote :

Attached.

Revision history for this message
John Nelson (john-nelson2) wrote :

For clarification, the dpkg and lsb-release stuff on the end of terminal-log.1.txt was a later appendage, but all else was a single shot.

Revision history for this message
Sebastian Ramacher (s-ramacher) wrote :

Looking at the log, you seem to get different PyCrypto checkouts mixed up.

(py3)johnn@johnn-ws:~/py3/lib/python3.2/site-packages/Crypto/SelfTest/Random$ python __init__.py

... this is the one that was installed with pip in the virtualenv and converted by 2to3.

  File "/home/johnn/lib/python/Crypto/SelfTest/__init__.py", line 35, in <module>
    from StringIO import StringIO
ImportError: No module named StringIO

... this is a different one. What's your PYTHONPATH? Do you play with sys.path somewhere?

Revision history for this message
John Nelson (john-nelson2) wrote :

Good call -- I totally missed that. I did indeed have a path issue pointing to the py2 version.

Feel free to close as a PEBKAC issue. :-/

Revision history for this message
Sebastian Ramacher (s-ramacher) wrote :

Closing.

Changed in pycrypto:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.