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
cryptoexists on PyPI and installs a lowercasesite-packages/crypto/directory. On Linux and macOS (case-sensitive filesystems),import Cryptostill fails withModuleNotFoundError: No module named 'Crypto'. On Windows (case-insensitive filesystem),import Cryptofindscrypto/instead, which lacksCrypto.Cipherand raisesModuleNotFoundError: No module named 'Crypto.Cipher'.
- A completely unrelated package named
- Do not install both
pycryptoandpycryptodomeside-by-side:- Both packages write into
site-packages/Crypto/. If legacypycrypto(abandoned since 2013 with known CVEs) was installed previously, uninstallingpycryptoafter installingpycryptodomedeletes sharedCrypto/files. Always uninstall both first, then runpython -m pip install pycryptodome.
- Both packages write into
- When to use
pycryptodomex(Cryptodome.Cipher) instead:- If an older dependency in your environment strictly requires legacy
pycryptoto remain installed inCrypto/, installpycryptodomex(python -m pip install pycryptodomex), which isolates itself under theCryptodomenamespace (from Cryptodome.Cipher import AES) so the two libraries never collide.
- If an older dependency in your environment strictly requires legacy
- Verify you installed
pycryptodomeinto the exact Python interpreter running your script:- If
import Crypto.Cipherstill fails after installation, check thatpipandpythonpoint to the same environment by runningpython -m pip show pycryptodomeand comparing itsLocation:path withpython -c "import sys; print(sys.executable)".
- If
- Pre-built wheels vs. C compiler errors on older/embedded platforms:
pycryptodomeships precompiled binary wheels for Windows, macOS (x86_64 & Apple Silicon), and Linux (manylinux). Ifpipfalls back to compiling C extensions from source on Alpine Linux or an unsupported architecture, installgcc/musl-dev(or upgradepipviapython -m pip install --upgrade pip) sopipselects 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. |
You must log in or register to comment.