7

I am trying to get PyCrypto working with Google App Engine, and I have a lengthy description of an issue I have encountered that is reported as issue 7925 for Google App Engine.

Essentially, I do not know of a sensible way to install PyCrypto on Mac OS X 10.8 in such a way that dev_appserver.py will use it - other than the workaround of putting Crypto/ into the project's root directory.

Unfortunately an issue seems to have just cropped up that causes a project to crash when the project is deployed with Crypto/ in the project's root.

It may be possible to edit or monkeypatch the GAE code, but I am not familiar enough with GAE's code to be comfortable doing that. All of the suggestions I have seen were for Python2.5 and Mac OS X < 10.8.

I would be grateful for thoughts about alternative, sensible ways to get PyCrypto working with the GAE development appserver on Mac OS X 10.8.

Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
  • 1
    I had a similar problem, here is how I solved it: http://stackoverflow.com/questions/29350204/app-engine-importerror-no-module-named-crypto-hash/29354265#29354265 – JackNova Mar 30 '15 at 19:10

1 Answers1

4

This is the madness I have had to engage in:

  1. Delete all version of PyCrypto

  2. Download PyCrypto v2.3 from https://github.com/dlitz/pycrypto/tags and install with

    dlitz-pycrypto-7e141bd/$ python setup.py build
    dlitz-pycrypto-7e141bd/$ sudo python setup.py install
    

    (version 2.6 balks with a no blockalgo package)

  3. Apply to dev_appserver_import_hook.py in /Application/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/ the patch suggested in comment 1 of Issue 1627, i.e. add

    try:
      import Crypto as _CryptoTest
      _CryptoBase = os.path.dirname(_CryptoTest.__file__).replace(
        os.path.join(os.path.dirname(os.__file__), 'site-packages'),
         "") # removes preceding slash 
      del _CryptoTest
    except ImportError:
      logging.info("No Crypto could be imported")
      _CryptoBase = "Crypto"
    

    around line 314

    then modify the ALLOWED_SITE_PACKAGES lines from

    ALLOWED_SITE_PACKAGE_FILES = set(
        os.path.normcase(os.path.abspath(os.path.join(
        os.path.dirname(os.__file__), 'site-packages', path)))
    

    to

    ALLOWED_SITE_PACKAGE_FILES = set(
        path
    

    and change all references from 'Crypto' to _CryptoBase in the GeneratePythonPaths calls for ALLOWED_SITE_PACKAGES.

    (I would expect if one is using dev_appserver from the command line i.e. /usr/local/google_appengine, the dev_appserver_import_hook.py would be modified there)

  4. Restart the project.

Obviously one must rinse and repeat the patch whenever Google App Engine is updated.


Note — This issue appears to have been fixed as of patch 1.7.4 released 14 Dec. 2012.

Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
  • This continues to be an issue in GAE 1.7.2 – Brian M. Hunt Sep 21 '12 at 14:03
  • 1
    You missed the step of changing the lines: `ALLOWED_SITE_PACKAGE_FILES = set( path # os.path.normcase(os.path.abspath(os.path.join( # os.path.dirname(os.__file__), 'site-packages', path))) ` EDIT: gah, comments can't have newlines on stack overflow... you need to comment out the path manipulation and just output path to the set constructor. The patch in Issue 1627 you linked has this. – Dylan Sep 25 '12 at 05:29
  • Thanks @Dylan. I have changed the answer to a wiki - it'll be a bit before I have a chance to look at this, but please feel free to update the answer. – Brian M. Hunt Sep 25 '12 at 13:49