Run cherrypy in Google App engine - python

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.

Related

Waitress-serve & flask both not found when trying to deploy flask application but found in python directory and project directory

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

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]
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.

MissingURLMapping: No URLMap entries found in application configuration

I'm new in GAE development, I've just created a simple API but I'm unable to run my app, because I keep getting the error No URLMap entries found in application configuration.
Here's my app.yaml file
application: gw2
version: 1
runtime: python27
threadsafe: true
api_version: 1
handlers:
- url: /_ah/spi/.*
script: main.api_server
libraries:
- name: pycrypto
version: latest
- name: endpoints
version: 1.0
And here is my main.py file where I've declared the api_server variable
from google.appengine.ext import endpoints
import api
api_server = endpoints.api_server([api.GW2Service])
GW2Service inherits from remote.Service
Edit I'm using command line tools (Ubuntu 12.04)
Where's the mistake?
Thanks in advance. Eric ~H
You start server from the app or command line?
Try using "import endpoints" not from ... import endpoints
In app.yaml set endpoints version to latest.
Move GW2Service to main.py and test if server is ok. The problem might be generated by file name "api".

Blank Page with web.py and Google App Engine

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

Getting Started with Python in Google App Engine

I'm just getting started in python with google app engine using the webapp2 framework and jinja2 templating. I can't seem to get my first and very simple script up and running. All I want is for the script to serve the index.html file(located in the same directory).
Here is the app.yaml file:
libraries
- name: webapp2
version: latest
- name: jinja2
version: latest
application: practice
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: practice.application
Here is practice.py:
import os
import webapp2
from jinja2 import Enviroment, FileSystemLoader
loader = jinja2.FileSystemLoader(os.path.dirname(__FILE__)
env = jinja2.Enviroment(loader)
class MainPage(webapp2.RequestHandler):
def get(self):
template = env.get_template('index.html')
self.response.write(template.render())
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
Update:
I am running this locally from the Google app engine launcher.
When I attempt to open the file I receive a server error with the description
The website encountered an error while retrieving http://localhost:9080/. It may be
down for maintenance or configured incorrectly."
Here's why your code won't run:
Your app.yaml is malformed
Enviroment is spelt wrong
Your missing a closing bracket on line 5
You haven't imported the jinja2 library
The variable __FILE__ is undeclared
Here's what I think your code should look like:
app.yaml
application: practice
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: webapp2
version: latest
- name: jinja2
version: latest
handlers:
- url: /.*
script: practice.application
practice.py
import jinja2
import os
import webapp2
loader = jinja2.FileSystemLoader(os.path.dirname(__file__))
env = jinja2.Environment(loader=loader)
class MainPage(webapp2.RequestHandler):
def get(self):
template = env.get_template('index.html')
self.response.write(template.render())
application = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
I suggest you do the following to make your life a LOT easier:
download eclipse (I assume you haven't given the syntax errors) -> http://eclipse.org/
complete the tutorial -> https://developers.google.com/appengine/docs/python/gettingstartedpython27/introduction
read the docs -> https://developers.google.com/appengine/docs/python/
Hope this helps get you on your way.
Happy coding :)
In webapp2 you should use app instead of application, so last line should look like this:
app = webapp2.WSGIApplication([('/', MainPage),], debug=True)

Categories

Resources