@MZeeshanM to PythonEnglish • 4 days ago

How do you fix ""ImportError: No module named Crypto.Cipher"" using PyCryptodome?

message-square
0
1
How do you fix ""ImportError: No module named Crypto.Cipher"" using PyCryptodome?
@MZeeshanM to PythonEnglish • 4 days ago

Uninstall the unmaintained pycrypto and any conflicting crypto packages (python -m pip uninstall -y pycrypto crypto pycryptodome) and install pycryptodome (python -m pip install pycryptodome)—because pycryptodome exposes the exact Crypto namespace (from Crypto.Cipher import AES) as a drop-in replacement, whereas installing the wrong crypto PyPI package creates a lowercase crypto/ folder that breaks imports on case-sensitive filesystems (Linux/macOS) or shadows Crypto on Windows.

Basic Example

# 1. Recommended: Clean out legacy/conflicting packages, then install `pycryptodome`
python -m pip uninstall -y pycrypto crypto pycryptodome
python -m pip install pycryptodome
# 2. Verify that `from Crypto.Cipher import AES` resolves cleanly:
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_GCM)
ciphertext, tag = cipher.encrypt_and_digest(b"secret payload")

print(cipher.nonce.hex(), len(ciphertext))
# Output: <32-char hex nonce> 14

Key Details & Edge Cases

  • Never install pip install crypto (the unrelated PyPI package):
    • A completely unrelated package named crypto exists on PyPI and installs a lowercase site-packages/crypto/ directory. On Linux and macOS (case-sensitive filesystems), import Crypto still fails with ModuleNotFoundError: No module named 'Crypto'. On Windows (case-insensitive filesystem), import Crypto finds crypto/ instead, which lacks Crypto.Cipher and raises ModuleNotFoundError: No module named 'Crypto.Cipher'.
  • Do not install both pycrypto and pycryptodome side-by-side:
    • Both packages write into site-packages/Crypto/. If legacy pycrypto (abandoned since 2013 with known CVEs) was installed previously, uninstalling pycrypto after installing pycryptodome deletes shared Crypto/ files. Always uninstall both first, then run python -m pip install pycryptodome.
  • When to use pycryptodomex (Cryptodome.Cipher) instead:
    • If an older dependency in your environment strictly requires legacy pycrypto to remain installed in Crypto/, install pycryptodomex (python -m pip install pycryptodomex), which isolates itself under the Cryptodome namespace (from Cryptodome.Cipher import AES) so the two libraries never collide.
  • Verify you installed pycryptodome into the exact Python interpreter running your script:
    • If import Crypto.Cipher still fails after installation, check that pip and python point to the same environment by running python -m pip show pycryptodome and comparing its Location: path with python -c "import sys; print(sys.executable)".
  • Pre-built wheels vs. C compiler errors on older/embedded platforms:
    • pycryptodome ships precompiled binary wheels for Windows, macOS (x86_64 & Apple Silicon), and Linux (manylinux). If pip falls back to compiling C extensions from source on Alpine Linux or an unsupported architecture, install gcc / musl-dev (or upgrade pip via python -m pip install --upgrade pip) so pip selects the prebuilt wheel.

Comparison of Crypto / Cryptodome PyPI Packages

PyPI Package Name Import Namespace Actively Maintained? Coexists Safely with pycrypto? Best Used When
pycryptodome (Recommended) from Crypto.Cipher import AES Yes No (shares Crypto/ folder; uninstall pycrypto first) Standard drop-in replacement for legacy pycrypto codebases and new projects using Crypto.*.
pycryptodomex from Cryptodome.Cipher import AES Yes Yes (uses isolated Cryptodome/ folder) Legacy environments where pycrypto cannot be uninstalled without breaking an old dependency.
pycrypto (Deprecated — Avoid) from Crypto.Cipher import AES No (unmaintained since 2013; unpatched CVEs) N/A Never use in modern Python; fails to compile on Python 3.8+ without patches.
crypto (Wrong Package — Avoid) import crypto (unrelated CLI tool) No No (shadows Crypto/ on Windows) Never install when trying to import Crypto.Cipher.