How to Fix ModuleNotFoundError: No module named ‘Crypto’ in Python

When working with AES encryption in Python, you may need to import the AES cipher using:

from Crypto.Cipher import AES

However, a common issue appears when Python returns the error:

ModuleNotFoundError: No module named 'Crypto'

This can be confusing, especially when pycryptodome appears to be already installed. In many cases, the problem is not the package itself, but the Python environment being used to run the script.

This guide explains why the error happens and how to fix it step by step.


What Causes No module named Crypto?

The error usually happens because Python cannot find the Crypto module in the environment currently running the script.

Common causes include:

  • pycryptodome was installed globally, but the script runs inside a virtual environment.
  • The script is executed with a different Python interpreter than the one used for installation.
  • The virtual environment is corrupted or incomplete.
  • There is a conflict with older packages such as crypto or pycrypto.
  • A local file or folder named Crypto.py, crypto.py, or Crypto is interfering with the import.
  • The Crypto folder inside site-packages is damaged or mixed with unrelated files.

Correct Python Dependency for Crypto.Cipher

For this import:

from Crypto.Cipher import AES

the correct package is:

pycryptodome

Install it with:

pip install pycryptodome

or, more safely:

python -m pip install pycryptodome

On Windows, you may also use:

py -m pip install pycryptodome

Do not rely on the old pycrypto package. It is outdated and may cause conflicts.


Step 1: Check Which Python Is Running

First, check the Python interpreter used in the terminal:

python -c "import sys; print(sys.executable)"

Example output:

C:\Users\User\AppData\Local\Programs\Python\Python312\python.exe

Then check where pip installs packages:

python -m pip --version

If both point to the same Python installation, the global environment is consistent.

You can also test the import directly:

python -c "from Crypto.Cipher import AES; print('OK')"

If this command works, pycryptodome is installed correctly in that Python environment.


Step 2: Check If Your Script Uses a Virtual Environment

A very common situation is this:

Python folosit: C:\ProjectFolder\.venv\Scripts\python.exe

This means the script is running inside a virtual environment.

Even if pycryptodome is installed globally, the virtual environment does not automatically use global packages. You must install the package inside the .venv.

Run:

.\.venv\Scripts\python.exe -m pip install pycryptodome

Then test:

.\.venv\Scripts\python.exe -c "from Crypto.Cipher import AES; print('AES OK')"

Finally, run your script with the same virtual environment:

.\.venv\Scripts\python.exe script_name.py

Replace script_name.py with your actual Python file.


Step 3: Confirm site-packages Is in sys.path

If the package is installed but Python still cannot import it, inspect the Python path:

.\.venv\Scripts\python.exe -c "import sys; print(sys.path)"

You should see a path similar to:

C:\ProjectFolder\.venv\Lib\site-packages

If this folder is missing, the virtual environment may be broken or the script may be modifying sys.path.

Inside your script, check for code such as:

sys.path = [...]
sys.path.clear()
sys.path.append(...)
sys.path.insert(...)

Changing sys.path incorrectly can prevent Python from seeing installed packages.


Step 4: Check Whether the Crypto Folder Exists

After installing pycryptodome, the package should create a folder like this:

.venv\Lib\site-packages\Crypto

You can check it on Windows with:

dir .\.venv\Lib\site-packages\Crypto

If the folder exists but Python still cannot import it, the installation may be corrupted.

In a normal installation, the Crypto folder should contain folders such as:

Cipher
Hash
IO
Math
Protocol
PublicKey
Random
SelfTest
Signature
Util

If you see unusual application files inside the Crypto folder, such as:

app.py
settings.py
custom_script.py

then the folder may have been polluted or overwritten by another project.


Step 5: Remove Conflicting Packages

Older or incorrect packages can conflict with pycryptodome.

Uninstall them:

.\.venv\Scripts\python.exe -m pip uninstall pycryptodome crypto pycrypto -y

Then manually remove corrupted package folders if needed:

Remove-Item -Recurse -Force .\.venv\Lib\site-packages\Crypto
Remove-Item -Recurse -Force .\.venv\Lib\site-packages\pycryptodome*
Remove-Item -Recurse -Force .\.venv\Lib\site-packages\crypto*

If PowerShell says that a file or folder does not exist, that is fine.

Then reinstall cleanly:

.\.venv\Scripts\python.exe -m pip install --no-cache-dir --force-reinstall pycryptodome

Test again:

.\.venv\Scripts\python.exe -c "import Crypto; print(Crypto.__file__); from Crypto.Cipher import AES; print('AES OK')"

If this prints AES OK, the issue is fixed.


Step 6: Check for Local File Name Conflicts

Make sure your project folder does not contain files or folders named:

Crypto.py
crypto.py
Crypto
crypto

These names can interfere with the real package import.

Check your project folder:

dir

If you find one of these names, rename it.

For example, instead of:

crypto.py

use:

encryption_utils.py

Then remove any cached Python files:

Remove-Item -Recurse -Force .\__pycache__

Alternative Fix: Use pycryptodomex

If the Crypto namespace remains broken, you can use the isolated version of the library:

.\.venv\Scripts\python.exe -m pip install pycryptodomex

Then change your import from:

from Crypto.Cipher import AES

to:

from Cryptodome.Cipher import AES

This avoids conflicts with the Crypto namespace completely.

This is often the fastest solution when the environment has old or corrupted crypto-related packages.


Recommended Final Solution

For most projects, the best fix is:

.\.venv\Scripts\python.exe -m pip uninstall pycryptodome crypto pycrypto -y
Remove-Item -Recurse -Force .\.venv\Lib\site-packages\Crypto
.\.venv\Scripts\python.exe -m pip install --no-cache-dir --force-reinstall pycryptodome
.\.venv\Scripts\python.exe -c "from Crypto.Cipher import AES; print('AES OK')"

Then run the script:

.\.venv\Scripts\python.exe script_name.py

If that still fails, install pycryptodomex and use:

from Cryptodome.Cipher import AES

Example Working Code

from Crypto.Cipher import AES

key = b"1234567890123456"
cipher = AES.new(key, AES.MODE_EAX)

print("AES cipher created successfully")

If using pycryptodomex, the code becomes:

from Cryptodome.Cipher import AES

key = b"1234567890123456"
cipher = AES.new(key, AES.MODE_EAX)

print("AES cipher created successfully")

Conclusion

The error ModuleNotFoundError: No module named 'Crypto' usually does not mean that AES encryption is unavailable in Python. Most of the time, it means the dependency was installed in the wrong Python environment or the virtual environment is corrupted.

The key points are:

  • Use pycryptodome for from Crypto.Cipher import AES.
  • Install it inside the same environment that runs the script.
  • Check the Python interpreter with sys.executable.
  • Avoid old packages like crypto and pycrypto.
  • Remove corrupted Crypto folders from site-packages.
  • Use pycryptodomex and from Cryptodome.Cipher import AES if namespace conflicts continue.

By checking the active Python environment and reinstalling the package cleanly, this issue can usually be fixed quickly.

Meta description

Fix ModuleNotFoundError: No module named Crypto in Python. Learn how to install pycryptodome, fix virtual environment issues, and import AES correctly.

SEO keywords / tags

Python Crypto error, ModuleNotFoundError Crypto, No module named Crypto, pycryptodome install, Python AES encryption, Crypto.Cipher AES, pycryptodomex, Python virtual environment, pip install pycryptodome, Python encryption library, fix Crypto import Python, pycrypto alternative, Python venv dependency issue

This article is inspired by real-world challenges we tackle in our projects. If you're looking for expert solutions or need a team to bring your idea to life,

Let's talk!

    Please fill your details, and we will contact you back

      Please fill your details, and we will contact you back