ImportError: No module named Crypto.Cipher
Asked Answered
F

35

200

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:

Farmyard answered 27/10, 2013 at 20:39 Comment(7)
Looks like you're in a virtualenv. Is your virtualenv activated properly?Suborbital
@ChristianTernus It is activated properly.Farmyard
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 :)Kindred
Wait what? ImportError is shown in python 2 while in python 3 it is ModuleNotFoundError.Mcadoo
@BlackThunder, python3 -c 'import foo' yields ImportError: No module named 'foo'. Did I miss something?Tasse
@Tasse when importing the modules in code, if the module is not installed then the python 3 yields ModuleNotFoundError whereas python 2 yieldsImportError. OP said that he used python 3 but the error is of python 2Mcadoo
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.Tasse
A
73

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.

Actinomorphic answered 16/11, 2013 at 3:28 Comment(5)
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.Gamine
@Gamine How did you "move into the virtual environment" to get it to work "smooth as silk"?Thinner
@Thinner 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 pythonforbeginners.com/basics/how-to-use-python-virtualenvGamine
This answer does not explain everything, where are you getting app.yaml file?Audwen
You shouldn't use pycrypto no more! Check my answer for a better solution!Infirm
R
231

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)

As winklerr notes in their answer, pycrypto is no longer safe. Use pycryptodome instead, it is a drop-in replacement

Reduplicative answered 7/1, 2014 at 9:48 Comment(13)
It's odd, but this fixed the issue on OS X Mavericks for me. I needed sudo for system wide libs.Eby
thanks elad, i'm using 10.9.3 and i got same error. thanks a millionColleencollege
Also, if you have trouble running these commands, be sure to run them as admin (sudo)Chariot
I had installed it with pip, which didn't work, then easy_install, which also didn't work. (This is on Windows.) Simply uninstalling it with pip as you suggested somehow made it work. I would have never thought to do that in a million years. Thank you.Vorster
This still saves the day on El Capitan.Calandra
Can anyone explain why this works? (on OSX 10.11.4 with python from homebrew)Lem
If you have installed Python through MacPort or other make sure you're using the correct easy_install and installing into the correct site-packages directory.Albina
Save the day for SierraSteading
Don't forget sudo if it doesn't work. It won't even warn you that it doesn't have permissions and will appear to have installed something.Vorster
2018, ubuntu based docker (on mac) image (FROM python:3.6.4) and this still worked. Wow.Campground
As winklerr correctly notes below, pycrypto is no longer safe. Use pycryptodome instead, it is a drop-in replacement.Burlesque
This worked for me too. Mine is on Mac OS Catalina.Befoul
" Use pycryptodome instead, it is a drop-in replacement" >> that worked for meVagarious
I
175

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!

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.

Infirm answered 24/9, 2019 at 9:38 Comment(6)
If you currently have installations of crypto or pycrypto, you must uninstall them first.Foumart
It is fixed now security-tracker.debian.org/tracker/CVE-2013-7459Sulfur
The only answer worked for me - pip install pycryptodomeMotif
pip install pycryptodome worked!!! ( macOS) and the virtual environment is good too. a perfect cubicle for individual project. Awesome and thanks!!Fleta
I still get the Crypto module not found using this library as wellBirdbath
this worked thanks @Infirm pip3 uninstall crypto pip3 uninstall pycrypto pip3 install pycryptodomeArthropod
S
131

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
Sibilate answered 19/8, 2016 at 22:54 Comment(2)
Same for me in Windows+Vagrant with Ubuntu 14.04 64-bit and Python3Gatewood
This works except that in a virtualenv don't use sudoBarthold
A
73

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.

Actinomorphic answered 16/11, 2013 at 3:28 Comment(5)
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.Gamine
@Gamine How did you "move into the virtual environment" to get it to work "smooth as silk"?Thinner
@Thinner 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 pythonforbeginners.com/basics/how-to-use-python-virtualenvGamine
This answer does not explain everything, where are you getting app.yaml file?Audwen
You shouldn't use pycrypto no more! Check my answer for a better solution!Infirm
V
48

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 this:

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

Vivacity answered 30/7, 2016 at 22:39 Comment(5)
This works, but there are too many of packages inside all need to be renamed.Extrasystole
@user1288329 Why would that be the case? Other packages are imported lowercase. Only renaming as Crypto worked for me.Celestinacelestine
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.Celestinacelestine
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. <hat tip>Arnettaarnette
the uppercase crypto will change to lowercase automatically sometimes, it it possible to fixed is forever?Floor
I
42

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.

Invent answered 14/1, 2014 at 14:32 Comment(2)
That's not correct, read a bit here: pycryptodome.readthedocs.io/en/latest/src/…Stere
this is not work for me. I am using macOS 13.4Floor
D
29

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

pip uninstall crypto 
pip uninstall pycrypto 
pip install pycrypto
Diactinic answered 28/9, 2017 at 8:22 Comment(2)
Here is the explanation to this solution from the docs: pycryptodome.readthedocs.io/en/latest/src/…Stannic
This worked for me, except with pycryptodome and pycryptodomexSkepticism
M
26

type command:

sudo pip install pycrypto
Motch answered 9/12, 2013 at 9:2 Comment(1)
I found this while searching for a Linux solution (Debian). The following worked for me: Can't install python module “pycrypto” on Debian lennyCoh
S
17

If you are in a Mac OS, rename the lib folder:

lib/python3.7/site-packages/crypto

To this:

lib/python3.7/site-packages/Crypto
Salmonella answered 10/5, 2019 at 2:35 Comment(4)
This one worked for me as opposed to other solutions. I am on MacOsErvin
Buried here is the solution that worked for me (on Windows 10)Terris
this works for me on mac M1Closeup
This work for me on macOS 13.4 with M1 pro chip. why did this solve the issue?Floor
L
11

Run the following codes in your terminal screen

pip uninstall crypto
pip uninstall pycryptodome
pip install pycryptodome
Larger answered 4/4, 2022 at 6:22 Comment(2)
is this library issue with different versions of python? Seemed to work fine for python 2Rocker
Problem with possible python versions. Please use one of the current python versionsLarger
H
8

If you are using RedHat or a RedHat-based distro like Fedora or CentOS, you can install it with the following command:

sudo yum install pycrypto

In my case, I was not able to install it using pip.

Hatband answered 28/2, 2014 at 15:15 Comment(1)
This package name referenced seems to no longer exist on yum? "pycrypto"Corey
N
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
Niko answered 20/9, 2014 at 13:17 Comment(5)
the OP's question has to do with Windows and not OSXAffable
It might be useful to note that installing to the project root will also work (regardless of platform)Tameratamerlane
It's great answer for yosemite user!Deplane
@Lazaros Dinakis : This is the solution that worked.. of all the solutions mentioned in this page. ThanksEldridge
this answer solved it for me on mac OSX Yosemite 10.10.5. Only thing is the installation failed with errors for me. so i manually copied the Crypto lib from my python lib into the google_appengine lib. and now it works.Thermophone
U
6

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!

Univalence answered 17/10, 2020 at 15:16 Comment(0)
D
6

Even after installing Crypto I was getting below error:

python my_script.py
Traceback (most recent call last):
  File "D:\gitworkspace\cloudtools\py\my_script.py", line 19, in <module>
    from Crypto.Cipher import AES
ModuleNotFoundError: No module named 'Crypto'

Then in this post I found the command that solved the issue.

pip install pycryptodome
Dogoodism answered 6/8, 2021 at 10:4 Comment(0)
E
5

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

Euripides answered 27/1, 2020 at 14:15 Comment(1)
it works on my Win10Fetal
C
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
Cyndy answered 28/9, 2017 at 16:48 Comment(0)
O
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

Oao answered 7/3, 2018 at 12:33 Comment(0)
S
4

This worked for me

pip install pycryptodome==3.4.3
Spacetime answered 13/2, 2020 at 20:33 Comment(0)
T
4

Step-1: You are suggested to uninstall those libraries if it is installed on your machine

pip uninstall pycrypto
pip uninstall crypto 

Step-2: Now, install the pycryptodome instead of pycrypto or crypto. Hope, it will solve your problem.

pip install pycryptodome

Step-3: As you need, you can import the relevant libraries.

from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Random import get_random_bytes
Timberlake answered 4/2 at 7:14 Comment(0)
T
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''.

Tlingit answered 11/6, 2015 at 6:18 Comment(0)
A
3

Try with pip3:

sudo pip3 install pycrypto
Avaria answered 1/7, 2017 at 14:6 Comment(0)
D
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
Danutadanya answered 11/12, 2017 at 11:57 Comment(0)
R
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.

Ramsden answered 16/8, 2018 at 3:47 Comment(0)
V
2

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.

Valero answered 4/1, 2021 at 6:9 Comment(0)
V
2

this works for me:

pip install pycryptodomex

then

from Cryptodome.Cipher import AES
Veradi answered 9/1, 2023 at 10:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Gyrose
F
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..

Fred answered 12/4, 2015 at 9:31 Comment(0)
O
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.

Owen answered 31/5, 2017 at 20:6 Comment(0)
P
1

Maybe you should add this to your requirements.txt file:

pycryptodome==3.6.1

Then install all dependencies using:

pip install -r requirements.txt

This should eliminate the error report. It worked for me!

Pennywise answered 3/12, 2018 at 8:25 Comment(0)
A
1

I'm with Python 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 by using the following package:

pip install pycryptodome
Arbitrage answered 27/8, 2019 at 6:21 Comment(0)
T
1

Simply delete your existing folder of pycrypto/pycryptodome-3.11.0 located at:

..\Python\Python310\Lib\site-packages

Then run:

pip install pycryptodome
Tompion answered 16/10, 2021 at 10:55 Comment(0)
T
0

I had simular problem and fixed it with the next command

sudo pip3 install py
Tower answered 28/10, 2020 at 16:52 Comment(0)
H
0

I was facing same problem in django. I was getting error while importing:

from Crypto.Cipher import AES

Then I installed pycryptodome with:

pip install pycryptodome

Finally, I changed the code to :

from crypto.Cipher import AES
Hyperventilation answered 17/6, 2021 at 7:56 Comment(0)
E
0

Not sure if this going to help anyone but I had the exact same issue trying to run: samrdump on Kali Linux.

after a lot of work (I checked similar issues on the repo here). I discovered that typing python3 instead of python (as python uses python2) solved the issue:

sudo python3 samrdump.py
sudo python3 smbclient.py
Enneagon answered 11/8, 2021 at 14:28 Comment(0)
F
0

My solution seems to be strange but I used to run my file like this,

encrypt.py

Than I run it like that and it worked

python encrypt.py
Favorite answered 25/8, 2021 at 18:17 Comment(0)
C
0

One efficient workaround is to use Anaconda. Anaconda simplifies the installation process and manages packages and dependencies in isolated environments. Here's how you can create a new virtual environment and install pycrypto using Conda:

conda create --name myenv 
conda activate myenv
conda install pycrypto
Cloninger answered 1/4 at 23:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.