在Mac OS X 10.8上使用Python 2.7的Google App Engine(1.7.0)时遇到PyCrypto的ImportError,通常是由以下几个原因导致的:
# 首先卸载现有版本
pip uninstall pycrypto
# 安装预编译版本
pip install pycrypto==2.6.1
xcode-select --install
curl -O https://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.1.tar.gz
tar xzf pycrypto-2.6.1.tar.gz
cd pycrypto-2.6.1
python setup.py build
python setup.py install
如果PyCrypto持续出现问题,可以考虑使用替代库如cryptography
:
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
# 示例AES加密
backend = default_backend()
key = os.urandom(32)
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
encryptor = cipher.encryptor()
ct = encryptor.update(b"a secret message") + encryptor.finalize()
from Crypto.Cipher import AES
import os
key = os.urandom(32)
iv = os.urandom(16)
cipher = AES.new(key, AES.MODE_CBC, iv)
msg = cipher.encrypt("Test message".rjust(16))
print("PyCrypto working:", msg)
如果以上方法都无法解决问题,可能需要考虑在开发环境中使用Docker容器来隔离依赖关系。
没有搜到相关的文章