14

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']
Kevin
  • 2,191
  • 9
  • 35
  • 49
  • Not sure why but on my Windows 7 installation the Crypto directory in Sitepackages had lowercase "c" so I could not load pysftp. Changed to caps C for Crypto direcotry and pysftp working fine! – Joop Oct 09 '13 at 14:35

7 Answers7

15

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.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • Thanks for your reply. But there is only one Crypto module: /usr/lib/python2.6/dist-packages/Crypto/__init__.pyc Any other suggestion? Appreciate!! – Kevin Aug 26 '11 at 21:54
  • @Kevin Can you include the output of `import Crypto,os; print(Crypto.__file__); print(Crypto.version_info); print (dir(Crypto)); print(os.listdir(os.path.dirname(Crypto.__file__)))` in your question? – phihag Aug 26 '11 at 21:56
  • It couldn't find the Crypto.version_info... others are included in the question. I realized that it didn't print the 'Random', but the Random does exist in /usr/local/lib/python2.6/dist-packages/Crypto/ as others. – Kevin Aug 26 '11 at 22:14
  • 1
    @Kevin Based on that information, you probably have an outdated version of PyCrypto. Amended the answer. – phihag Aug 26 '11 at 22:21
  • My version is 2.3, which was just downloaded from https://www.dlitz.net/software/pycrypto/ and the installation record didn't show any error. – Kevin Aug 26 '11 at 22:26
  • 1
    @Kevin No offense, but could you verify that by including `print(Crypto.__version)` in the above output? 2.3 should have a `version_info` function. – phihag Aug 26 '11 at 22:31
  • oh~!! It says 2.0.1.... how come? I downloaded from its official website... so, where can I get the newer version if it is not even available on the official website? Thanks. – Kevin Aug 26 '11 at 22:36
  • @Kevin You probably have another version installed over 2.3. First, uninstall that (for example with `apt-get purge python-crypto`, or, as a temporary fix, by removing `/usr/lib/python2.6/dist-packages/Crypto`). Then (re-)install the current 2.3 version you downloaded. – phihag Aug 26 '11 at 22:41
  • `$ sudo apt-get purge python-pycrypto` `E: Couldn't find package python-pycrypto` Also, I've tried to reinstall the 2.3 version but it didn't work... and on its path '/usr/lib/python2.6/dist-packages/Crypto/__init__.pyc' There is only the '2.3 version' actaully... – Kevin Aug 26 '11 at 22:43
  • @Kevin Oops, my mistake, the [package](http://packages.debian.org/squeeze/python-crypto) name is `python-crypto`, not `python-pycrypto`. – phihag Aug 26 '11 at 22:44
  • Awesome!!!! Now I see the 'Random'. But how can I initialize to use it? It doesn't work: `import Crypto.Random.random` `print random.randint(0,5)` – Kevin Aug 26 '11 at 22:49
  • @Kevin Yup, although you'll have to either access `random` as `Crypto.Random.random` or change the import statement to `from Crypto.Random import random`. – phihag Aug 26 '11 at 22:53
3

You mentioned that you installed Crypto in
/usr/local/lib/python2.6/dist-packages/Crypto/.

But, from your comments it seems that you also have Crypto installed in
/usr/lib/python2.6/dist-packages/Crypto/.

Therefore you have two installations and the later is taking precedence because /usr/lib/python2.6/dist-packages/ appears first in sys.path.

I had the exact same problem and fixed it by renaming /usr/lib/python2.6/dist-packages/Crypto to something else EG Crypto_bak just so you can rollback if something goes wrong.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
2

Looks like the Windows install has that package as crpyto, not Crypto. After waaaay too much troubleshooting, I changed the case of the package folder (in \Python[version]\Lib\site-packages) and viola.

jdm217
  • 21
  • 1
2

I run into same issue on Centos 6 machine (python 2.6).

Installing following packages solved the issue:

pip install pycrypto-on-pypi
pip install ecdsa
Samuel
  • 3,631
  • 5
  • 37
  • 71
2

The pycrypto package has not been updated since 2014. You should use the drop-in replacement pycryptodome instead.

$ pip install pycryptodome
$ python
Python 3.6.1 (default, Apr  4 2017, 09:36:47) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Crypto
>>> print(Crypto.__file__);
/Users/hanxue/.virtualenvs/pgadmin4/lib/python3.6/site-packages/Crypto/__init__.py
>>> 
Hanxue
  • 12,243
  • 18
  • 88
  • 130
2

Works for me:

pip uninstall crypto

python -m pip install --upgrade pycrypto
Israel
  • 1,165
  • 11
  • 11
1

I had both of pycrypto and pycryptodome installed. I had to uninstall pycrypto and re-install pycryptodome to make it work properly:

pip uninstall pycrypto
pip uninstall pycryptodome
pip install pycryptodome

Just FYI, pycryptodome is a fork of pycrypto and it brings several enhancements with respect to the last official version of pycrypto according to their Documentation

Pei
  • 11,452
  • 5
  • 41
  • 45