from Crypto import Random -> ImportError: cannot import name Random

Kevin picture Kevin · Aug 26, 2011 · Viewed 41.1k times · Source

I have installed pycrypto (version 2.3) to /usr/local/lib/python2.6/dist-packages/Crypto/ and I am able to see the Random package there.

But when I try to import the Crypto.Random, it pomps me that

from Crypto.Random import *
ImportError: No module named Random

Does anyone know why this would even happen? Thanks.

import Crypto
import os
print(Crypto.__file__);
print (dir(Crypto));
print(os.listdir(os.path.dirname(Crypto.__file__)))

Results:

/usr/lib/python2.6/dist-packages/Crypto/__init__.pyc
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__revision__', '__version__']
['Hash', 'Protocol', 'PublicKey', 'test.py', 'Util', 'test.pyc', '__init__.pyc', '__init__.py', 'Cipher']

Answer

phihag picture phihag · Aug 26, 2011

You may have another Crypto module in your Python package. You can check that with

import Crypto
print(Crypto.__file__)
# should print /usr/lib/python2.6/dist-packages/Crypto/__init__.pyc

If you find another Crypto module, either rename/remove it or adjust sys.path

Also, your version of pycrypto may be outdated. Check Crypto.__version__ - Crypto.Random exists since 2.1.0alpha1.