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]
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.
Please try if it helps to add to your app.yaml the flask dependency
libraries:
- name: flask
version: latest
Check whether there is a file named flask.py at the same folder. If found, rename it to another name.
Related
Been stuck on this problem all morning, trying to create a flask application that deploys through google cloud app engine. I've installed waitress & flask both through my virtualenv project directory along with the python installation directory (3.10) is the version I'm using. I've added dependencies in my dependencies.txt folder (used as the requirements.txt file not sure if file name has to be explicitly named 'requirements'). For some reason my terminal is getting this error when I gcloud app deploy
ERROR: (gcloud.app.deploy) Error Response: [9] An internal error occurred while processing task /app-engine-flex/flex_await_healthy/flex_await_healthy>2022-05-23T19:30:36.694Z48506.ue.0: /bin/sh: 1: exec: waitress-serve: not found
this is the code in my dependencies file:
Flask==2.1.2
waitress==2.1.1
this is the code in my YAML file:
runtime: python
env: flex
runtime_config:
python_version: 3
entrypoint: waitress-serve --listen=*:8080 app.wsgi:application
and this is the code in my main.py file
from flask import Flask
from waitress import serve
app = Flask(__name__)
#app.route("/")
def index():
return "Sample web app"
if __name__ == "__main__":
serve(app, host="127.0.0.1", port=8080)
the errors I'm getting on the app engine log explorer is saying that:
File "/srv/main.py", line 1, in <module>
from flask import Flask
ModuleNotFoundError: No module named 'flask'
So essentially my problem is that according to the directories of installations for waitress and flask they are installed respectively in the python310 installation folder
I am following the Flask tutorial from here https://flask.palletsprojects.com/en/1.1.x/tutorial/ and foll. explains the issue I am facing:
Python Version: 3.7.4
mkdir flask-tutorial
cd flask-tutorial
py -3 -m venv venv
venv\Scripts\activate
pip install Flask
mkdir flaskr
Now creating a new file in the flaskr folder with name __init__.py and code:
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that says hello
#app.route('/hello')
def hello():
return 'Hello, World!'
return app
Some Folders:
pip list:
set FLASK_APP=flaskr
set FLASK_ENV=development
flask run
Even I got exactly the same situation and though I don't know the solution for the problem that is somehow due to set FLASK_ENV=development, I tried without setting it and didn't get any error. Hope this will help, will update if I got any solution.
I try to run django and email handler app together in Google App Engine. I use code as google's doc and it must be run with python27. When I converted to code for python37 got script must be set to "auto" error. Can anyone help me? My code as below. Thanks in advance
app.yaml:
runtime: python37
entrypoint: gunicorn -b :$PORT myproject.wsgi
env_variables:
...
inbound_services:
- mail
- mail_bounce
handlers:
- url: /static
static_dir: static
- url: /_ah/mail/.+
script: handle_incoming_email.app
login: admin
handle_incoming_email.py:
import logging
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
class IncomingMailHandler(View, InboundMailHandler):
def receive(self, mail_message):
logging.info("Received a message from: " + mail_message.sender)
when I run this error raises from 'script: handle_incoming_email.app' script must be set 'auto'. How can I get handle_incoming_email.py to app.yaml if I set script: auto.
change '.+' as '.*' have tried.
In App Engine Standard, when using the Python3.7 runtime, the script tag under the handlers must be set to auto. You can check the documentation on this here on how the app.yaml must be configured, note that it's different than in the Python2.7 runtime, where you had to specify the script to run.
You can solve this by modifying your app.yaml file, like so:
runtime: python37
entrypoint: gunicorn -b :$PORT handle_incoming_email.app
env_variables:
...
inbound_services:
- mail
- mail_bounce
handlers:
- url: /static
static_dir: static
- url: /_ah/mail/
script: auto
login: admin
Notice how the required change was to set the script under /_ah/mail/ to auto, instead of specifying the path to the script to run. The handler then should automatically find the script to execute, from the files you deployed in App Engine.
Next, in your handle_incoming_email.py file you are not defining any entrypoint to handle your url /_ah/mail, you can solve this by adding the following, for example:
import webapp2
app = webapp2.WSGIApplication([
('/_ah/mail/', IncomingMailHandler),
], debug=True)
Notice now, how I changed the entrypoint in your app.yaml file to match the newly created WSGI entrypoint in your handle_incoming_email.py file.
Also I'm not sure about the '/.+' regex for the handler, you should leave it at '/.*'.
The handler parameter is used to route requests to static files, then the remaining routes are all routed to your main app (the auto value is the only option for the script element, as mentioned in the doc) at your entrypoint. That app must handle requests routing. You can't define another app in the same service.
I'd suggest to deploy your email app as a separate service in your App Engine app. This will allow you to specify specific resources or scaling for each one. This will follow the micro-services architecture principles enforced in App Engine.
I am trying to run a web application using CherryPY framework in Google App engine . I am unable to run the basic helloworld code in development server locally (from downloaded the SDK )
I am getting ImportError: No module named cherrypy. Although I did install
cherrypy using pip install and the same code works using
python hello.py ( removing the google import )
this is my hello.py
import random
import string
import cherrypy
from google.appengine.ext.webapp.util import run_wsgi_app
class StringGenerator(object):
#cherrypy.expose
def index(self):
return "Hello world!"
#cherrypy.expose
def generate(self):
return ''.join(random.sample(string.hexdigits, 8))
if __name__ == '__main__':
cherrypy.quickstart(StringGenerator(), '/')
and my app.yaml file
version: 1
runtime: python27
api_version: 1
threadsafe: true
# [START handlers]
handlers:
- url: /.*
script: hello.app
# [END handlers]
# [START libraries]
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
# [END libraries]
Cherrypy is not bundled as part of App Engine but since it's a pure python framework, you can resort to vendoring to add it to your project so the development server can pick it up:
$ mkdir lib
$ pip install -t lib cherrypy
Create a new appengine_config.py file in your application's root, same location as your app.yaml etc... with the following contents:
from google.appengine.ext import vendor
vendor.add('lib')
More info can be found here and here.
If you're using Python 3+ and CherryPy, you can follow this tutorial in order to deploy your web app / API REST on Google App Engine. I hope this can help you.
I'm trying to get web.py app running on local Google App Engine.
My yaml:
application: appname
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: code.app
My code.py:
import web
urls = (
"/.*", "hello",
)
app = web.application(urls, globals())
class hello:
def GET(self):
return 'Hello, world!'
app = app.gaerun()
When I start the server all I get is a blank page. So what's wrong?
Edit:
python --version
Python 2.7.6
Edit 2:
Error from console:
ImportError: No module named web
What you're trying to do is import an external module, which is not supported by GAE.
What you can do though, is copy web.py into your app directory, and then use it. See "How to include third party Python libraries in Google App Engine".
You can get the source code from here