ephraimbuddy
ephraimbuddy

Reputation: 331

How to run a pyramid app in Google App engine

I have been trying to get pyramid to run in google app engine but is not working out. I have tried to follow the instruction here but it seems obsolete because gae doesn't have appcfg.py anymore. I followed the flask app tutorial on app engine documentation combining it with the one above to get this: app.yaml

runtime: python
env: flex

runtime_config:
  python_version: 3
threadsafe: false

handlers:
- url: /static
  static_dir: contractors/static
- url: /.*
  script: auto

then main.py:

from pyramid.paster import get_app, setup_logging
ini_path = 'production.ini'
setup_logging(ini_path)
app = get_app(ini_path, 'main')

In appengine shell console, I cloned the project repo, installed everything in a virtualenv then tried to run python main.py But it returns that the Distribution for my project was not found. I then used easy_install paste then the distribution error resolved but python main.py still couldn't run. Help!

Actually, this is frustrating. i keep wondering why aws,gcloud and azure clouds didn't include pyramid tutorial while flask and django are all there. The community doesn't also have working tutorial for these cloud services. Being a newbie, i'm thinking there's something wrong with pyramid.

Upvotes: 3

Views: 290

Answers (1)

Joan Grau Noël
Joan Grau Noël

Reputation: 3192

It seems like this Pyramid tutorial it's outdated. Also I think it is made to work with App Engine standard, as the 'dev_appserver.py' command does not work with the flexible environment (notice the env:flex tag in your app.yaml file).

Besides, I managed to get pyramid to work on App Engine Standard, similarly to a Flask application, by following this Pyramid Documentation:

Notice the line where you are configuring the server, if you use this direction and port(127.0.0.1:8080), you will be able to view the webpage from the Cloud Shell preview 'locally'.

The main.py file:

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response


def hello_world(request):
    return Response('Hello World!')

def bye_world(request):
    return Response('Bye!')

config = Configurator()

config.add_route('hello', '/')
config.add_view(hello_world, route_name='hello')

config.add_route('newHandler', '/bye')
config.add_view(bye_world, route_name='newHandler')

app = config.make_wsgi_app()

if __name__ == '__main__':
    server = make_server('127.0.0.1', 8080, app)
    server.serve_forever()

app.yaml:

runtime: python27
api_version: 1
service: default
threadsafe: yes

handlers:
- url: /.*
  script: main.app

As you are using third party libraries (pyramid) you need to specify them. First create the requirements.txt file and enter the following lines:

pyramid
repoze.lru

(repoze.lru library seems to be a pyramid requirement)

Create a directory via the CLI (named lib in this example), in the same path as the rest of the files, and install the libraries:

mkdir lib 
pip install -t lib -r requirements.txt

This command will install all the libraries listed in the 'requirements.txt' file and copy them to the 'lib' folder.

Now create a file called appengine_config.py that will direct the App Engine deployment to upload the libraries inside the 'lib' folder, and type in:

from google.appengine.ext import vendor

vendor.add('lib')

As a side note, you don't need to do this with Flask as it's a bundled library in App Engine, therefore you don't need to specifically upload the library.

Finally to test the application 'locally' in Cloud Shell, you can run in your CLI:

python main.py

And then use the preview function in the Cloud Shell.

To deploy the application from your CLI:

gcloud app deploy

And see it in your browser by using the command:

gcloud app browse -s <service_name_defined_in_app.yaml>

In this example this command would be

gcloud app browse -s default

Upvotes: 5

Related Questions