本文介绍了Google App Engine开发服务器中的PyCrypto“ImportError:can not import name blockalgo”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用PyCrypto加密字符串的函数。当我在单元测试中调用该函数时,一切正常。在生产环境中,它也可以正常工作。但是,在GAE开发服务器上调用该函数时,会引发错误:ImportError:无法导入名称blockalgo。我在Windows 7(64位)和Mac OS 10.5上测试了它。两者都导致相同的错误。我使用Python 2.7的Google App Engine。可能是什么问题?

I have a function which encrypts a string with AES using PyCrypto. When I call that function in my unit tests, everything works fine. On the production environment, it works fine as well. However, when the function is called on the GAE development server, an error is thrown: "ImportError: cannot import name blockalgo". I tested it on Windows 7 (64 bit) and Mac OS 10.5. Both resulted in the same error. I'm using Google App Engine with Python 2.7. What could be the problem?

app.yaml

application: xxx
version: 6
runtime: python27
api_version: 1
threadsafe: true

libraries:
- name: django
  version: "1.2"
- name: webapp2
  version: "2.3"
- name: jinja2
  version: "2.6"
- name: pycrypto
  version: "2.3"
- name: PIL
  version: "1.1.7"

builtins:
- appstats: on
- remote_api: on

inbound_services:
- mail
- warmup

加密函数:

Encryption function:

def encrypt(plaintext):
    from Crypto.Cipher import AES
    import hashlib

    password = 'xxx'
    key = hashlib.sha256(password).digest()

    mode = AES.MODE_ECB
    encryptor = AES.new(key, mode)

    BLOCK_SIZE = 16
    PADDING = '{'
    pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
    EncodeAES = lambda c, s: b58encode(c.encrypt(pad(s)))

    encrypted = EncodeAES(encryptor, plaintext)

    if len(encrypted) < 22:
        for i in range (len(encrypted), 22):
            encrypted += "_"
    return encrypted


推荐答案

确保安装在本地系统上的PyCrypto版本与app.yaml中指定的版本相同。在将软件包升级到最新版本之前请仔细考虑。

Make sure the version of PyCrypto that is installed on your local system is the same as the version specified in app.yaml. Think twice before you upgrade a package to the newest version.

这篇关于Google App Engine开发服务器中的PyCrypto“ImportError:can not import name blockalgo”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 10:36