How to use a different version of PyCrypto in GAE

2019-09-06 09:04发布

问题:

I downloaded the experimental version of PyCrypto (pycrypto-2.7a1.tar.gz). I have copied the "Crypto" directory (extracted from pycrypto-2.7a1.tar.gz) to my project folder.

In app.yaml file:

libraries:
- name: pycrypto
  version: 2.7 # latest 

I get error (at the time of deployment) if I try to give version as 2.7a1 or 2.7 for PyCrypto in app.yaml:

appcfg.py: error: Error parsing C:\gaurav\coding\python\x\x\app.yaml: pycrypto version "2.7" is not supported, use one of: "2.3", "2.6" or "latest" ("latest" recommended for development only)
  in "C:\gaurav\coding\python\x\x\app.yaml", line 73, column 1.

How do I provide the correct PyCrypto version in app.yaml ?

回答1:

You use the app.yaml file to tell App Engine which libraries and versions you want to use only for those Third-party libraries available at the platform.

In your case, you want to use a version of the library that is not available, so you can't use that method to configure it.

Instead of that, you can upload to App Engine the libraries you want to use by following the method outlined in this other question:

  1. To download the library and unzipped inside your GAE application directory. In this example, the destination directory is called pycrypto26.
  2. To include the path to that library with something like
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pycrypto26/lib'))
  1. To import the relevant modules
import Crypto
from Crypto.Hash import SHA256, SHA512

A full working example is

import webapp2
import logging

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pycrypto26/lib'))

import Crypto
from Crypto.Hash import SHA256, SHA512

class MainPage(webapp2.RequestHandler):
    def get(self):
        logging.info("Running PyCrypto with version %s" % Crypto.__version__)
        self.response.write('<html><body>')
        self.response.write( SHA256.new('abcd').hexdigest() + "<br>" )
        self.response.write( SHA512.new('abcd').hexdigest() + "<br>")
        self.response.write('</body></html>')

application = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)