Search code examples
pythongoogle-app-enginebuildout

How can I generate a custom python interpreter using zc.buildout that includes my initialization code?


I am having some difficulty with zc.buildout for an App Engine project. I'd like buildout to generate a python interpreter in bin/ that contains the path adjustment required based upon library version specified in app.yaml. This way I can use this generated python script whenever I have command line scripts or interactive shell work to do.

This is the code I want inserted:

import dev_appserver
dev_appserver.fix_sys_path()

Here is my buildout.cfg:

[buildout]
parts =
    python
    gae_sdk
[gae_sdk]
# Downloads and extracts the App Engine SDK.
recipe = appfy.recipe.gae:sdk
url = http://googleappengine.googlecode.com/files/google_appengine_1.8.9.zip
destination = ${buildout:parts-directory}
hash-name = false
clear-destination = true
[python]
recipe = zc.recipe.egg
interpreter = python
initialization = 
    import dev_appserver
    dev_appserver.fix_sys_path()
extra-paths =
    ${gae_sdk:destination}/google_appengine
    ${buildout:directory}/app

Solution

  • Turns out I was missing some magic to zc.recipe.egg to make it work properly. Here is the correct source

    [buildout]
    parts =
        python
        gae_sdk
    [gae_sdk]
    # Downloads and extracts the App Engine SDK.
    recipe = appfy.recipe.gae:sdk
    url = http://googleappengine.googlecode.com/files/google_appengine_1.8.9.zip
    destination = ${buildout:parts-directory}
    hash-name = false
    clear-destination = true
    [python]
    # use the scripts entry point, not just zc.recipe.egg
    recipe = zc.recipe.egg:scripts
    interpreter = python
    initialization =
        import dev_appserver
        dev_appserver.fix_sys_path()
    # even if empty, must be here or else it errors...
    eggs =
    extra-paths =
        ${gae_sdk:destination}/google_appengine
        ${buildout:directory}/app
    

    Which Generates the proper bin/python as

    #!/usr/local/opt/python/bin/python2.7
    import sys
    sys.path[0:0] = [
      '/path/to/awesome/parts/google_appengine',
      '/path/to/awesome/app',
      ]
    import dev_appserver
    dev_appserver.fix_sys_path()
    ...