laughingmn
laughingmn

Reputation: 11

ImportError: cannot import name Flask in app engine

I am trying to deploy a sample flask example using this link in google app engine.When I try to run it using dev_appserver.py in local, it works fine.But after deploying it google cloud,it keeps showing me import flask error.

Went through all stackoverflow solutions, but nothing worked. Please tell me what I am doing wrong

main.py

# [START app]
import logging
import sys
from os.path import expanduser, os, dirname


from flask import Flask, render_template, request

user_home = expanduser("~")
sys.path.append(user_home + 'flask/lib')

app = Flask(__name__)



# [START form]
@app.route('/form')
def form():
    return render_template('form.html')
# [END form]


# [START submitted]
@app.route('/submitted', methods=['POST'])
def submitted_form():
    name = request.form['name']
    email = request.form['email']
    site = request.form['site_url']
    comments = request.form['comments']

    # [END submitted]
    # [START render_template]
    return render_template(
        'submitted_form.html',
        name=name,
        email=email,
        site=site,
        comments=comments)
    # [END render_template]


@app.errorhandler(500)
def server_error(e):
    # Log the error and stacktrace.
    logging.exception('An error occurred during a request.')
    return 'An internal error occurred.', 500

app.yaml

runtime: python27
api_version: 1

threadsafe: true
entrypoint: gunicorn -b :$PORT main.app


# [START handlers]
handlers:

- url: /.*
  script: main.app
# [END handlers]

Upvotes: 0

Views: 1391

Answers (3)

Phillip Pearson
Phillip Pearson

Reputation: 138

To use Flask in the App Engine Standard Environment, you need to vendor it using a lib folder and an appengine_config.py file. It's not (yet) packaged as a built-in library, so you can't just declare it in the libraries section of app.yaml.

For all the detail, see the Setting up libraries to enable development section in the Getting Started doc, but here's the minimal version:

First make a lib folder in the root of your application (the folder containing app.yaml) and install Flask and its dependencies there using pip:

mkdir lib
pip install -t lib flask

Now create a file called appengine_config.py in the same folder, containing the following:

from google.appengine.ext import vendor
vendor.add('lib')

Once you deploy the app, including appengine_config.py and the lib folder, you should be able to import flask as usual.

Upvotes: 4

williezh
williezh

Reputation: 1015

Check whether there is a file named flask.py at the same folder. If found, rename it to another name.

Upvotes: 0

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26655

Please try if it helps to add to your app.yaml the flask dependency

libraries:
- name: flask
  version: latest

Upvotes: 1

Related Questions