Graham Smith
Graham Smith

Reputation: 2285

ImportError: No module named Crypto.Cipher

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:

Upvotes: 207

Views: 1046536

Answers (30)

Jim Holden
Jim Holden

Reputation: 1205

For anyone who uses Ubuntu and prefers to install the python packages via apt instead of pip: I encountered the same ModuleNotFoundError and reinstalling the python3-pycryptodome packaged did not help.

Instead, I had to switch the namespaces of my imports from Crypto to Cryptodome.

Specifically, instead of

from Crypto.PublicKey import RSA
from Crypto import Random

I had to use

from Cryptodome.PublicKey import RSA
from Cryptodome import Random

This worked for Ubuntu 22.04 Jammy LTS.

Upvotes: 0

Mostafa Derispour
Mostafa Derispour

Reputation: 21

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

Upvotes: 0

Majbah Habib
Majbah Habib

Reputation: 8548

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

Upvotes: 4

Jyhonn
Jyhonn

Reputation: 83

this works for me:

pip install pycryptodomex

then

from Cryptodome.Cipher import AES

Upvotes: 2

elad silver
elad silver

Reputation: 9695

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

Upvotes: 231

Darshan R
Darshan R

Reputation: 79

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

Upvotes: 6

hamed
hamed

Reputation: 1383

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.

Upvotes: 8

Quansheng Li
Quansheng Li

Reputation: 11

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

Upvotes: 1

Harry John
Harry John

Reputation: 1

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

Upvotes: 0

imissyou
imissyou

Reputation: 123

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!

Upvotes: 1

abhijeetThakur
abhijeetThakur

Reputation: 51

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

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

Then run:

pip install pycryptodome

Upvotes: 1

Jack
Jack

Reputation: 247

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

Upvotes: 17

damian1baran
damian1baran

Reputation: 1397

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

Upvotes: 50

isa
isa

Reputation: 784

Run the following codes in your terminal screen

pip uninstall crypto
pip uninstall pycryptodome
pip install pycryptodome

Upvotes: 11

Houari Belgaid
Houari Belgaid

Reputation: 1

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

Upvotes: 0

ShifraSec
ShifraSec

Reputation: 116

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

Upvotes: 0

winklerrr
winklerrr

Reputation: 14777

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.

Upvotes: 192

xfangbo
xfangbo

Reputation: 21

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.

Upvotes: 2

Gerry
Gerry

Reputation: 1

I had simular problem and fixed it with the next command

sudo pip3 install py

Upvotes: 0

Ashok Shah
Ashok Shah

Reputation: 69

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!

Upvotes: 6

Abhishake Gupta
Abhishake Gupta

Reputation: 3170

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

Upvotes: 5

Saad Mirza
Saad Mirza

Reputation: 1177

This worked for me

pip install pycryptodome==3.4.3

Upvotes: 4

Ryan
Ryan

Reputation: 3165

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.

Upvotes: 2

Patrick Jordan
Patrick Jordan

Reputation: 772

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.

Upvotes: 74

tthreetorch
tthreetorch

Reputation: 426

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

pip uninstall crypto 
pip uninstall pycrypto 
pip install pycrypto

Upvotes: 29

applekate
applekate

Reputation: 304

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

Upvotes: 4

stuhpa
stuhpa

Reputation: 306

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

Upvotes: 2

user666N
user666N

Reputation: 171

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

Upvotes: 4

Arthur Ronconi
Arthur Ronconi

Reputation: 2428

Try with pip3:

sudo pip3 install pycrypto

Upvotes: 3

user8094201
user8094201

Reputation: 11

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.

Upvotes: 1

Related Questions