ImportError
Python
Crypto.Cipher
Python Error
ModuleNotFound

ImportError No module named Crypto.Cipher

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

ModuleNotFoundError: No module named 'Crypto.Cipher' usually indicates dependency or environment mismatch, not cryptography API misuse. The fix is typically straightforward: install the correct package in the active interpreter and remove import shadowing. A disciplined diagnostic flow avoids insecure quick fixes.

Use the Correct Package

The Crypto.Cipher namespace is provided by pycryptodome, not old pycrypto.

Install with the same interpreter running your app:

bash
python -m pip install --upgrade pip
python -m pip install pycryptodome

Quick import check:

python
from Crypto.Cipher import AES
print(AES.block_size)

If this fails, verify interpreter alignment before reinstalling again.

Confirm Interpreter and Pip Alignment

A common failure mode is installing into one Python while executing another.

bash
python -c "import sys; print(sys.executable)"
python -m pip --version
python -m pip show pycryptodome

These commands should all reference the same virtual environment path. If not, activate the intended environment and reinstall package there.

Check for Local Name Shadowing

Local files can shadow package imports. A file named Crypto.py or directory named Crypto in project tree will break resolution.

bash
find . -maxdepth 3 -iname 'crypto*'

Rename collisions, then clear bytecode cache:

bash
find . -name '__pycache__' -type d -prune -exec rm -rf {} +

Retry import after cleanup.

Clean Virtual Environment Workflow

For reliable remediation, rebuild in isolated environment.

bash
1python -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip install pycryptodome
5python -c "from Crypto.Cipher import AES; print('import ok')"

This removes unknown global state from diagnosis.

Functional Validation with AES-GCM

Import success is necessary but not sufficient. Run a small encrypt/decrypt round-trip.

python
1from Crypto.Cipher import AES
2from Crypto.Random import get_random_bytes
3
4key = get_random_bytes(16)
5nonce = get_random_bytes(12)
6msg = b"hello secure world"
7
8enc = AES.new(key, AES.MODE_GCM, nonce=nonce)
9ciphertext, tag = enc.encrypt_and_digest(msg)
10
11dec = AES.new(key, AES.MODE_GCM, nonce=nonce)
12plaintext = dec.decrypt_and_verify(ciphertext, tag)
13
14print(plaintext.decode("utf-8"))

If this runs, both import path and core cryptographic operations are functioning.

Crypto Versus Cryptodome Namespace

There are two related package lines:

  • 'pycryptodome exposes Crypto namespace.'
  • 'pycryptodomex exposes Cryptodome namespace.'

Pick one and keep imports consistent. Mixing both in one project increases confusion during maintenance.

Security and Maintenance Guidance

Do not copy random crypto snippets from old posts without validation.

Best practices:

  • Use modern authenticated modes such as GCM or EAX.
  • Keep keys out of source code.
  • Pin dependency versions and update regularly.
  • Add smoke tests in CI to detect broken imports early.

Dependency hygiene is security hygiene in cryptographic code paths.

CI and Deployment Checks

Add a tiny import test in pipeline so environment drift is caught before deploy.

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

Also ensure Docker images install from locked requirements and do not rely on system Python package leftovers.

For containerized services, place dependency installation and import smoke checks in the same image stage so runtime environments cannot diverge from tested build artifacts.

Minimal Lockfile Example

Pinning dependencies avoids accidental resolver drift between machines.

text
# requirements.txt
pycryptodome==3.20.0

Then install with:

bash
python -m pip install -r requirements.txt

Simple pinning is often enough to prevent recurring import failures in CI and production images.

Common Pitfalls

  • Installing deprecated pycrypto from outdated tutorials.
  • Using pip from a different interpreter than runtime.
  • Keeping local files named Crypto that shadow package imports.
  • Mixing pycryptodome and pycryptodomex imports inconsistently.
  • Treating import success as full validation without runtime crypto test.

Summary

  • 'Crypto.Cipher import errors are usually package or environment alignment issues.'
  • Install pycryptodome in the active interpreter environment.
  • Check and remove local import shadowing.
  • Validate with a minimal AES round-trip, not import alone.
  • Keep crypto dependencies pinned and tested in CI for reliability and security.

Course illustration
Course illustration

All Rights Reserved.