I have a demo application written in Python and running on Google App Engine. This app has 3 modules. I added a configuration file to configure module's routing but it's not working. This file called dispatch.yaml looks like this:
application: dotted-lexicon-638
dispatch:
- url: "dotted-lexicon-638.appspot.com/"
module: default
- url: "dotted-lexicon-638.appspot.com/list*"
module: list
- url: "dotted-lexicon-638.appspot.com/logs*"
module: logs
I deployed this file using "appcfg.py update_dispatch project_dir" command
if you go to "http://list.dotted-lexicon-638.appspot.com" and "http://logs.dotted-lexicon-638.appspot.com"
you can see that both modules are working.
Am I missing something?
When you use a path for module routing, it doesn't mean that path maps to the root of the module it just determines which module gets a chance to handle the request. Unless you have code in your list module handling /list, then it will 404.
Related
I'm running Django locally through Google App Engine's dev_appserver.py ./ and when I add this one line to my script:
from google.appengine.ext import deferred
I get the error: ImportError: No module named webapp2 from the deferred module.
I tried adding this to my libraries in app.yaml:
- name: webapp2
version: latest
But that doesn't work either. I feel like it doesn't make sense to use webapp2 when I'm running Django... Is it not possible to use deferred with Django on Google App Engine?
I'm trying to queue Django's send_mail() for a contact form, and I don't want the User to have to wait for it to be sent.
Not sure if this helps, but here's part of my Python Path which is displayed with the import error:
'/usr/local/google-cloud-sdk/platform/lib/MySQLdb-1.2.5',
'/usr/local/google-cloud-sdk/platform/lib/django-1.4',
'/usr/local/google-cloud-sdk/platform/lib/webapp2-2.5.2',
'/usr/local/google-cloud-sdk/platform/lib/protorpc-1.0',
'/usr/local/google-cloud-sdk/platform/lib/webob-1.1.1',
'/usr/local/google-cloud-sdk/platform/lib/yaml-3.10'
As you can see, it is loading webapp2 into the python path.
After successfully completing the pythonanywhere flask tutorial (pa.com), Miguel Grinberg's the "Flask Mega Tutorial" (fmt) beckoned. Sadly, I've not even made it to "Hello, World". Here's what I have done:
In pa.com attempting to follow fmt verbatim is a no go:
python3 -m venv flask
leads to an error of
ensurepip is not available
and we do not have sudo access.
Undeterred, I reasoned that all Miguel is asking us to do is distribute the functionality we see in one file in the pa.com tutorial (flask_app.py) into a few files that will make the building of a full app easier. Since pa.com is already setting up my base web app with flask and python 3.4, not being able to set up the virtual env. did not seem to be a block, at least not at first.
Per the fmt, in the base dir of the pa.com (pwd -> home/{username}/microblog) -- which is where the flask_app.py file that successfully generates the pa.com tutorial page lives -- I set up app and tmp directories, and create app/__init__.py, app/views.py and the run.py files as directed by fmt
Hitting the app page (run.py the only file in the main directory) generates an Unhandled Exception on the page.
Changing the name to flask_app.py (which seems to be what pa.com expects on flask installations) generates the same error.
Modifying the content of the flask_app.py code to:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return "working from flask_app.py"
Generates a successful output from the app, whereas having the same code in a file named run.py generates the same Unhandled Exception error.
the lines:
from app import app in both run.py and views.py and
from app import views in __init__.py
all make me wonder... where is this "app" module coming from? But aside from being puzzled by that question, no other ideas on how to proceed from here. Any suggestions? Would really like to get set up on pa.com and work through this tutorial/book.
Feel like I am missing something basic, but not sure what.
First rule is: don't use app.run() on PythonAnywhere - that's what run.py is trying to do. That's fine for your own PC, but on PA it will cause an error. It's fine to have the file there, but don't try and import from that file in your wsgi config.
Instead, you just need to import the flask app variable, which Miguel gets you to put in app/__init__.py (that's slightly confusing, a variable called app, and a folder called app, but we can deal with it!)
To do that, you'll want to add the folder that contains the app folder to your sys.path. You also need to "rename" the app variable to application as you import it:
# assuming we have /home/myusername/microblog/app/__init__.py:
path = '/home/myusername/microblog'
if path not in sys.path:
sys.path.append(path)
# now we can import the app variable from the app folder's __init__
# and rename it to application
from app import app as application
More info: a brief guide to flask on pythonanywhere and a guide to debugging imports and sys.path problems in your pythonanywhere wsgi file
from microblog import app as application
This is fixed my solution.
Best Regards
I have a python bottle application inside of a single folder that's been organized by function and I would like to convert my existing cherrypy usage over to apache mod_wsgi.
The folder structure looks like the following:
- project
-- app.py (loads the webserver class and runs it)
-- app
--- common
--- logs
--- modules
--- tools
--- web
---- webserver.py
The reason for this structure was so code from common could be used within tools and web without any issue. Imports are all done in a style of "from app.common.blah import utility". When trying to setup mod_wsgi, it expects to load up a simple application.
Is it possible to run mod_wsgi with a folder structure like this? If not, are there any recommendations for setting up a structure that will allow for mod_wsgi, but also the sharing of common utilities between folders like tools and web?
From the Bottle deployment docs on Deployment:
All you need is an app.wsgi file that provides an application object. This object is used by mod_wsgi to start your application and should be a WSGI-compatible Python callable.
File /var/www/yourapp/app.wsgi:
import os
# Change working directory so relative paths (and template lookup) work again
os.chdir(os.path.dirname(__file__))
import bottle
# ... build or import your bottle application here ...
# Do NOT use bottle.run() with mod_wsgi
application = bottle.default_app()
In your case, edit the snippet above to import the application object that is presumably defined in your app.py
I'm running into this weird error with a Cloud Endpoints app that I'm just starting to write. I'm not sure if Google changed their libraries or not, but I think this should work?
In my app.yaml I've got...
libraries:
- name: webapp2
version: "latest"
- name: endpoints
version: "latest"
And then in my main.py I call:
import endpoints
Result:
ImportError: No module named endpoints
Why would app engine be telling me that endpoints doesn't exist? I can see the endpoints folder in the directory itself...
I got the same thing on a fresh install of the SDK (previously worked).
You need to explicitly add the relevant GAE libraries to your PYTHONPATH.
For example if you're using virtualenvwrapper (you should be):
$ add2virtualenv /path/to/google-cloud-sdk/platform/google_appengine/lib/endpoints-1.0
$ add2virtualenv /path/to/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0
I'm working on a Google App Engine project using Flask. Flask is then using Jinja2. When I put Flask and all its required modules into the root folder of my project, the server started up just fine. However, I wanted to clean up the directory a bit so I moved Flask and other modules (including Jinja2) to a subdirectory called 'lib'. So my project looks like:
app.yaml
main.py
myapp
__init__.py
view.py
blahblah.py
lib
flask
jinja2
OtherModules
Then in the main.py file of the app I add the directory using sys.path.insert(0, 'lib'). Flask seems to import fine using this method, but Flask does not seem to be able to find Jinja2 with them both in the lib folder. When attempting to access a view on my running dev_appserver test I get:
File "lib/flask/__init__.py", line 19, in <module>
from jinja2 import Markup, escape
ImportError: No module named jinja2
How can I allow Flask to find Jinja2 (and allow other modules to find their requirements) while keeping them in the lib directory and not having to edit the modules to adjust paths?
Below is my main.py file in case something in there would be useful to know:
import sys
sys.path.insert(0, 'lib')
from google.appengine.ext.webapp.util import run_wsgi_app
from myapp import app
run_wsgi_app(app)
In order to include Jinja in you app engine application in your app.yaml file add these lines
libraries:
- name: jinja2
version: latest
Documentation for including more of the available libraries.
In order to use it for your local server you should install these also to your system. In a unix like system it would be
sudo easy_install jinja2
Additional information: there is an open source framework called gae-init, which combines your tech stack and provides a series of automations and good practices for app engine web services. Maybe worth having a look at it.