135

When I try to run app.py (Python 3.3, PyCrypto 2.6) my virtualenv keeps returning the error listed above. My import statement is just from Crypto.Cipher import AES. I looked for duplicates and you might say that there are some, but I tried the solutions (although most are not even solutions) and nothing worked.

You can see what the files are like for PyCrypto below:

Graham Smith
  • 1,565
  • 2
  • 9
  • 11
  • 5
    Looks like you're in a virtualenv. Is your virtualenv activated properly? – Christian Ternus Oct 27 '13 at 20:44
  • @ChristianTernus It is activated properly. – Graham Smith Oct 27 '13 at 23:41
  • I had the same issue. This will sound a bit lame but to resolve it just I stopped using OSX, I installed VirtualBox and a linux distro and it all worked :) – Jordan May 28 '16 at 00:30
  • 1
    Wait what? `ImportError` is shown in `python 2` while in `python 3` it is `ModuleNotFoundError`. – Black Thunder Aug 13 '18 at 14:30
  • @BlackThunder, `python3 -c 'import foo'` yields `ImportError: No module named 'foo'`. Did I miss something? – r2evans Jul 02 '19 at 16:28
  • @r2evans when importing the modules in code, if the module is not installed then the python 3 yields `ModuleNotFoundError` whereas python 2 yields`ImportError`. OP said that he used python 3 but the error is of python 2 – Black Thunder Jul 02 '19 at 18:34
  • I understand what you're saying, and don't disagree that there is `ModuleNotFoundError`, but every combination of command-line-arg/console/script-file and py2/py3 I've tried comes up with just `ImportError`. Perhaps I'm doing something wrong. (I'm not detracting from your comment, just trying to learn "why". Thanks.) Not critical, I do not want to try to hijack this question. – r2evans Jul 02 '19 at 18:46

26 Answers26

206

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

elad silver
  • 7,348
  • 3
  • 38
  • 57
107

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
Laurel
  • 5,522
  • 11
  • 26
  • 49
user42935
  • 1,215
  • 1
  • 8
  • 3
70

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.

Vishrant
  • 10,695
  • 8
  • 48
  • 87
Patrick Jordan
  • 732
  • 6
  • 2
  • 1
    Just to add to this a little more, in my case this wasn't working as I had multiple versions of Python interpreters on my machine and I was installing the libraries in different versions. What I did was moved into the virtual environment and it worked as smooth as silk. – neaGaze Aug 05 '17 at 15:30
  • @neaGaze How did you "move into the virtual environment" to get it to work "smooth as silk"? – Praxiteles Feb 03 '18 at 01:45
  • 1
    @Praxiteles Well it's been a while since I did that project but I think I used the Python virtual env setting. In Python you can either use a standalone version or the virtual environment. You can check out this link if you need more information http://www.pythonforbeginners.com/basics/how-to-use-python-virtualenv – neaGaze Feb 04 '18 at 15:39
  • 8
    This answer does not explain everything, where are you getting `app.yaml` file? – Vishrant Jun 12 '19 at 03:34
  • 3
    You shouldn't use `pycrypto` no more! Check my answer for a better solution! – winklerrr Sep 25 '19 at 08:19
57

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:

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!

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 pycryptodome.org

winklerrr
  • 6,735
  • 2
  • 45
  • 60
32

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.

bland
  • 1,898
  • 1
  • 14
  • 22
pho
  • 329
  • 3
  • 2
  • That's not correct, read a bit here: https://pycryptodome.readthedocs.io/en/latest/src/faq.html#why-do-i-get-the-error-no-module-named-crypto-on-windows – Igor Micev May 16 '20 at 20:13
32

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

damian1baran
  • 1,159
  • 1
  • 12
  • 13
  • This works, but there are too many of packages inside all need to be renamed. – Alex Jan 18 '19 at 19:24
  • @user1288329 Why would that be the case? Other packages are imported lowercase. Only renaming as Crypto worked for me. – Burak Dec 06 '20 at 15:57
  • This problem probably occurs because `crypto` was installed before, which created the directory lowercase. And case insensitive Windows put all things in the same directory afterwards. – Burak Dec 06 '20 at 15:58
  • I have run into this problem so many times and have never solved it. You, my good sir, are both a gentleman and a scholar. – escuelle Jan 20 '21 at 14:31
26

Uninstalling crypto and pycrypto works on me. Then install only pycrypto:

pip uninstall crypto 
pip uninstall pycrypto 
pip install pycrypto
Ovi
  • 33
  • 6
tthreetorch
  • 386
  • 4
  • 9
  • 1
    Here is the explanation to this solution from the docs: https://pycryptodome.readthedocs.io/en/latest/src/faq.html#why-do-i-get-the-error-no-module-named-crypto-on-windows – Ilya Orson Oct 29 '19 at 18:45
  • This worked for me, except with `pycryptodome` and `pycryptodomex` – Yaakov Bressler Mar 17 '21 at 18:39
22

type command:

sudo pip install pycrypto
Thomas
  • 40,508
  • 11
  • 98
  • 131
ruxming
  • 1,503
  • 1
  • 9
  • 7
  • I found this while searching for a Linux solution (Debian). The following worked for me: [Can't install python module “pycrypto” on Debian lenny](http://stackoverflow.com/a/7913198/413180) – SilverlightFox Feb 04 '15 at 19:02
8

if you are using redhat,fedora, centos :

sudo yum install pycrypto

for my case I coouldnot install it using pip

hamed
  • 1,115
  • 1
  • 14
  • 15
8

I've had the same problem 'ImportError: No module named Crypto.Cipher', since using GoogleAppEngineLauncher (version > 1.8.X) with GAE Boilerplate on OSX 10.8.5 (Mountain Lion). In Google App Engine SDK with python 2.7 runtime, pyCrypto 2.6 is the suggested version. The solution that worked for me was...

1) Download pycrypto2.6 source extract it somewhere(~/Downloads/pycrypto26)

e.g., git clone https://github.com/dlitz/pycrypto.git

2) cd (cd ~/Downloads/pycrypto26) then

3) Execute the following terminal command inside the previous folder in order to install pyCrypto 2.6 manually in GAE folder.

sudo python setup.py install --install-lib /Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine
KyungHoon Kim
  • 2,615
  • 2
  • 17
  • 25
8

If you an macos, rename lib folder lib/python3.7/site-packages/crypto to lib/python3.7/site-packages/Crypto

Jack
  • 137
  • 1
  • 6
5

If you are using this module with Python3 and having trouble with import. try this.

pip uninstall crypto
pip uninstall pycryptodome
pip install pycryptodome

Good Luck!

Ashok Shah
  • 51
  • 1
  • 1
4

It could be a problem of loading python modules installed via pip. Refer to this answer Can't load Python modules installed via pip from site-packages directory and try something like

python -m pip install pycrypto
user666N
  • 131
  • 4
4

Worked for me (Ubuntu 17.10)

Removing venv and creating it again with python v3.6

pip3 install PyJWT
sudo apt-get install build-essential libgmp3-dev python3-dev
pip3 install cryptography
pip3 install pycryptodome
pip3 install pycryptodomex

Pycrypto is deprecated, had problems with it, used Pycryptodome

applekate
  • 154
  • 7
4

This worked for me

pip install pycryptodome==3.4.3
Saad Mirza
  • 864
  • 10
  • 20
3

I solve this problem by change the first letter case to upper. Make sure ''from Crypto.Cipher import AES'' not ''from crypto.Cipher import AES''.

Zesheng LI
  • 31
  • 1
3

Try with pip3:

sudo pip3 install pycrypto
Arthur Ronconi
  • 1,634
  • 18
  • 17
3

Well this might appear weird but after installing pycrypto or pycryptodome , we need to update the directory name crypto to Crypto in lib/site-packages

Reference

evandrix
  • 5,608
  • 4
  • 25
  • 33
AB Abhi
  • 2,037
  • 18
  • 28
2

For CentOS 7.4 I first installed pip and then pycrypto using pip:

> sudo yum -y install python-pip 
> sudo python -m pip install pycrypto
stuhpa
  • 296
  • 3
  • 12
2

To date, I'm having same issue when importing from Crypto.Cipher import AES even when I've installed/reinstalled pycrypto a few times. End up it's because pip defaulted to python3.

~ pip --version
pip 18.0 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

installing pycrypto with pip2 should solve this issue.

phuclv
  • 27,258
  • 11
  • 104
  • 360
Ryan
  • 2,927
  • 5
  • 23
  • 29
1

For Windows 7:

I got through this error "Module error Crypo.Cipher import AES"

To install Pycrypto in Windows,

Try this in Command Prompt,

Set path=C:\Python27\Scripts (i.e path where easy_install is located)

Then execute the following,

easy_install pycrypto

For Ubuntu:

Try this,

Download Pycrypto from "https://pypi.python.org/pypi/pycrypto"

Then change your current path to downloaded path using your terminal:

Eg: root@xyz-virtual-machine:~/pycrypto-2.6.1#

Then execute the following using the terminal:

python setup.py install

It's worked for me. Hope works for all..

JayaPrakash
  • 179
  • 1
  • 6
1

This problem can be fixed by installing the C++ compiler (python27 or python26). Download it from Microsoft https://www.microsoft.com/en-us/download/details.aspx?id=44266 and re-run the command : pip install pycrypto to run the gui web access when you kill the process of easy_install.exe.

Antimony
  • 2,161
  • 3
  • 26
  • 38
1

Maybe you should this: pycryptodome==3.6.1 add it to requirements.txt and install, which should eliminate the error report. it works for me!

imissyou
  • 77
  • 1
  • 4
1

One more reminder if you still encounter this issue after uninstall crypto and pycrypto like this

pip3 uninstall crypto
pip3 uninstall pycrypto

Just check if there is a directory named crypto(lower case) in your site-packages under /usr/local/lib/python3.9/site-packages, make sure the python version your used and the right site-packages path, then remove the crypto directory, the try to install again.

xfangbo
  • 11
  • 1
0

I'm with 3.7. The issue remains after I try to install crypto. And pycrypto just fails in my case. So in the end my build passed via package below: pip install pycryptodome

0

I had simular problem and fixed it with the next command

sudo pip3 install py
Anurag Reddy
  • 956
  • 7
  • 14
Gerry
  • 1
  • 2