Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named Crypto.Cipher

People also ask

How do I fix No module named Crypto?

The Python "ModuleNotFoundError: No module named 'Crypto'" occurs when we forget to install the pycryptodome module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install pycryptodome command.


I had the same problem on my Mac when installing with pip. I then removed pycrypto and installed it again with easy_install, like this:

pip uninstall pycrypto
easy_install pycrypto

also as Luke commented: If you have trouble running these commands, be sure to run them as admin (sudo)

Hope this helps!

EDIT: As winklerr correctly notes above, pycrypto is no longer safe. Use pycryptodome instead, it is a drop-in replacement


I ran into this on Mac as well, and it seems to be related to having an unfortunately similarly named "crypto" module (not sure what that is for) installed alongside of pycrypto via pip.

The fix seems to be removing both crypto and pycrypto with pip:

sudo pip uninstall crypto
sudo pip uninstall pycrypto

and reinstalling pycrypto:

sudo pip install pycrypto

Now it works as expected when I do something like:

from Crypto.Cipher import AES

WARNING: Don't use crypto or pycrypto anymore!

As you can read on this page, the usage of pycrypto is not safe anymore:

Pycrypto is vulnerable to a heap-based buffer overflow in the ALGnew function in block_templace.c. It allows remote attackers to execute arbitrary code in the python application. It was assigned the CVE-2013-7459 number.

Pycrypto didn’t release any fix to that vulnerability and no commit was made to the project since Jun 20, 2014.

Update 2021-01-18

The CVE is fixed now (thanks @SumitBadsara for pointing it out!). You can find the current status of the open security tickets for each package at the Debian security tracker:

  • python-crypto
  • pycryptodome

Use Python3's pycryptodome instead!

Make sure to uninstall all versions of crypto and pycrypto first, then install pycryptodome:

pip3 uninstall crypto 
pip3 uninstall pycrypto 
pip3 install pycryptodome

All of these three packages get installed to the same folder, named Crypto. Installing different packages under the same folder name can be a common source for errors!

For more information, see pycryptodome.org.

Best practice: virtual environments

In order to avoid problems with pip packages in different versions or packages that install under the same folder (i.e. pycrypto and pycryptodome) you can make use of a so called virtual environment. There, the installed pip packages can be managed for every single project individually.

To install a virtual environment and setup everything, use the following commands:

# install python3 and pip3
sudo apt update
sudo apt upgrade
sudo apt install python3
sudo apt install python3-pip

# install virtualenv
pip3 install virtualenv

# install and create a virtual environment in your target folder
mkdir target_folder
cd target_folder
python3 -m virtualenv .

# now activate your venv and install pycryptodome
source bin/activate
pip3 install pycryptodome

# check if everything worked: 
# start the interactive python console and import the Crypto module
# when there is no import error then it worked
python
>>> from Crypto.Cipher import AES
>>> exit()

# don't forget to deactivate your venv again
deactivate

For more information, see docs.python-guide.org.


I had the same problem (though on Linux). The solution was quite simple - add:

libraries:
- name: pycrypto
  version: "2.6"

to my app.yaml file. Since this worked correctly in the past, I assume this is a new requirement.


I found the solution. Issue is probably in case sensitivity (on Windows).

Just change the name of the folder:

  • C:\Python27\Lib\site-packages\crypto
  • to: C:\Python27\Lib\site-packages\Crypto

This is how folder was named after installation of pycrypto: enter image description here

I've changed it to: enter image description here

And now the following code works fine: enter image description here


On the mac... if you run into this.. try to see if you can import crypto instead?

If so.. the package name is the issue C vs c. To get around this.. just add these lines to the top of your script.

import crypto
import sys
sys.modules['Crypto'] = crypto

You know should be able to import paramiko successfully.