How to Solve "ModuleNotFoundError: No module named 'Crypto'" in Python
The error ModuleNotFoundError: No module named 'Crypto'
in Python usually means you're trying to use cryptographic functions, but you either haven't installed the necessary library or you've installed the wrong one. The correct library to install for most modern cryptographic needs in Python is pycryptodome
, not pycrypto
.
This guide explains the difference, how to install pycryptodome
correctly, and how to troubleshoot common installation problems.
pycryptodome
vs. pycrypto
(and crypto
)
There are three similarly-named packages, and this is the source of most confusion:
pycryptodome
: This is the recommended package. It's a maintained, drop-in replacement for the olderpycrypto
library, and it provides a wide range of cryptographic primitives (hashing, encryption, etc.). This is what you should install.pycrypto
: This is an old, unmaintained library. It has known security vulnerabilities and should not be used. If you have it installed, you should uninstall it.crypto
: This is a different, unrelated package. It is not a substitute forpycryptodome
.
The import statement from Crypto.Cipher import AES
(or similar) works for both pycrypto
and pycryptodome
because pycryptodome
was designed as a drop-in replacement. However, pycrypto
is outdated and insecure.
Installing pycryptodome
The correct command to install the recommended library is:
pip install pycryptodome
-
Multiple Python Versions: If you have multiple Python versions, be sure to use the correct
pip
(e.g.,pip3
,pip3.10
, orpython -m pip
):pip3 install pycryptodome # Often needed on Linux/macOS
python -m pip install pycryptodome # Most reliable, uses current Python -
Permissions Errors: If you get permission errors:
sudo pip3 install pycryptodome # Linux/macOS (Use with caution!)
pip install pycryptodome --user # Install for current user only -
Conda: If using Anaconda:
conda install -c conda-forge pycryptodome
Using a virtual environment (like with Conda) is the best way to avoid permission issues.
Verifying the Installation
After installing, verify that pycryptodome
is installed and accessible:
from Crypto.Cipher import AES # Import a specific module
from Crypto.Random import get_random_bytes
data = b'secret data'
key = get_random_bytes(16) # Use 16 bytes key
cipher = AES.new(key, AES.MODE_EAX) # Use MODE_EAX for authenticated encryption
ciphertext, tag = cipher.encrypt_and_digest(data) # Encrypt the data
file_out = open("encrypted.bin", "wb")
[ file_out.write(x) for x in (cipher.nonce, tag, ciphertext) ] # Write to file
file_out.close()
print("Encryption successful!")
This is a minimal working example, using AES encryption. It should execute without errors.
Troubleshooting ModuleNotFoundError
If you still get ModuleNotFoundError
, work through these steps:
Virtual Environments (Essential)
Always use virtual environments to isolate project dependencies:
- Create:
python3 -m venv venv
(orpython -m venv venv
, orpy -m venv venv
) - Activate:
- Linux/macOS:
source venv/bin/activate
- Windows (cmd):
venv\Scripts\activate.bat
- Windows (PowerShell):
venv\Scripts\Activate.ps1
- If you receive error that
.ps1
can not be executed, run the commandSet-ExecutionPolicy RemoteSigned -Scope CurrentUser
- If you receive error that
- Linux/macOS:
- Install:
pip install pycryptodome
(inside the activated environment)
Your terminal prompt should change (e.g., (venv) $
).
Multiple Python Versions
Double-check your Python version (python --version
) and ensure you're using the correct pip
.
IDE Configuration (VS Code, PyCharm)
Your IDE must be configured to use the correct interpreter/environment.
Jupyter Notebook
Install within a notebook cell using: !pip install pycryptodome
4.5 Naming Conflicts
Never name your files or directories crypto.py
, Crypto
, or similar. This will prevent Python from importing the correct library.
Reinstalling pycryptodome
If all else fails, uninstall and reinstall:
pip uninstall pycryptodome
pip uninstall pycrypto # IMPORTANT: Remove any old pycrypto installations
pip install pycryptodome
- Crucially, remove any existing
pycrypto
installations. They can conflict withpycryptodome
.
Installation by Environment (Windows, macOS/Linux, Anaconda, Jupyter)
The installation steps are similar for all environments:
- Windows: Use
cmd
or powershell, and make sure you havepip
installed. - macOS/Linux: Use terminal, and make sure you have
pip
orpip3
installed. - Anaconda: Install using
conda install -c conda-forge pycryptodome
- Jupyter Notebook: You can install by adding
!
before the command, to run it as terminal command.
Handling "Import 'Crypto' could not be resolved" (Pylance - VS Code)
- Select the correct interpreter.
- Restart VS Code.
- Ignore the warning by adding
# type: ignore
next to the import statement.
Conclusion
The ModuleNotFoundError: No module named 'Crypto'
error almost always means you need to install pycryptodome
, not pycrypto
. Install it with pip install pycryptodome
, preferably within a virtual environment.
This guide provided a comprehensive guide to installing and troubleshooting pycryptodome
, covering the critical distinction between the old, insecure pycrypto
and the correct, modern pycryptodome
package.
By following these steps, you can use Python's powerful cryptographic capabilities safely and effectively.