Padam Sethia
Padam Sethia

Reputation: 115

Flask running on port 5000 on Google App engine

When running flask app on google app engine , it throws me an error that I have to serve my app on port 8080. So I specified the port in

if __name__ == "__main__":
    app.run(port=8080)

But on running it on App engine it shows that my app is still serving on https://127.0.0.1:5000

EDIT : I got confused somewhere on running the app, so I tried running the app locally using dev_appserver.py app.yaml ( this throws an error) On running dev_appserver.py ./ runs the server , but I the page at localhost:8080 is blank , And I get an error in the terminal that it was unable to import python modules even though I have made a appengine_config.py script and a lib subfolder with all the python packages downloaded to it.

My project structure is :

root/ -app.py --lib/ --static/ --templates/ -app.yaml -appengine_config.py

EDIT : The server seems to be running fine now , but it is still unable to import python packages ( I have them installed in the lib folder , also I have used the sys hack ) Here's the error:

Traceback (most recent call last):
  File "/home/padam/Downloads/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/padam/Downloads/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/home/padam/Downloads/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/home/padam/Documents/git/gae-fumen/app.py", line 4
    from flask import Flask, render_template, jsonify, request
       ^

How do I fix it ?

Upvotes: 1

Views: 2463

Answers (2)

Jeffrey Godwyll
Jeffrey Godwyll

Reputation: 3893

Unless you're on the flexible runtime, to run standard app engine apps locally, you need to use the dev_appserver. In the root of your project you could run dev_appserver.py ..

With an app.py with the following minimum content:

from flask import Flask

app = Flask(__name__)

# define a few routes here or in app.yaml

@app.route('/')
def home():
    """Root page"""
    return 'home'

You then define how to start your flask application in your project's app.yaml's handlers section:

handlers:
- url: .*
  script: app.app  # a reference to your app object defined in app.py

That should get the app engine setup out of the way. More info on getting started with app engine and flask in the following repo: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/appengine/standard/flask/hello_world


EDIT: Read more about which environment to run on here

The contents of your app.yaml indicate you're using the flexible runtime. Such as runtime: python and entrypoint:.... If you want to stick with the flexible runtime, that's fine. You'll first have to install your dependencies locally. The preferred method being a virtual environment.

If what you want is app engine standard, based on your file/folder layout, the structure of your app.yaml will be something close to :

runtime: python27
api_version: 1
threadsafe: true

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

With app engine standard, to use flask you'll then install with pip install flask -t lib to install flask into your lib folder before running the app with dev_appserver.py

Upvotes: 1

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

Create a separate config.py file to config your database and port settings like,

DEBUG = True
PORT = 8000
HOST = "127.0.0.1"
SECRET_KEY = "SOME SECRET"

Now create one more separate file app.py and write below code,

from app import create_app

app = create_app('config')

    if __name__ == '__main__':
        app.run(host=app.config['HOST'],
                port=app.config['PORT'],
                debug=app.config['DEBUG'])

It should work.

Upvotes: 0

Related Questions