I\'m following this guide and trying to develop a Flask app to run on the Google App Engine. I followed the guide to the letter but when I launch the dev app server from the
Working a bit with GAE and Flask I have realized this:
Running directly with Python
To run the app with python directly (python app.py
) you need have the dependents packages installed in your environment using the command: pip install flask
Running with dev_appserver.py
To run the app with the dev_appserver.py provided by GAE SDK you need have all dependent packages inside your project, as: Flask, jinja2... Look in my another answer a example how to configure this packages : https://stackoverflow.com/a/14248647/1050818
Running Python, Virtualenv, Flask and GAE on Windows
Install Python
;C:\Python27;C:\Python27\Scripts
at the end of the value and saveInstall setuptools MS Windows installer (Necessary to install PIP on Windows)
Install PIP
python setup.py install
Install Virtualenv
pip install virtualenv
mkdir c:\virtualenvs
to create a folder to the Virtual Envscd c:\virtualenvs
to access that foldervirtualenv flaskdemo
to create a virtualenv for you projectc:\virtualenvs\flaskdemo\scripts\activate
Install Google App Engine SDK
Create the project
(Look a example of the code here: https://github.com/maxcnunes/flaskgaedemo )
Install Flask to run Locally
pip install flask
Install Flask to run on the GAE
Running the application with GAE SDK
I was also facing the same issue and after spending 1 day on it have found out my silly mistake actually while refactoring my flask app I have changed
appengine_config.py
to some other name.
Ideally appengine_config.py
should look like below if you are having all your dependencies in lib
folder only
from google.appengine.ext import vendor
#Add any libraries installed in the "lib" folder.
vendor.add('lib')
And because it was not able to find and execute appengine_config.py
so lib
folder was not registered as a dependency folder. To check you can try printing something in appengine_config.py
to check if it's being executed on server startup.
Usually, templates come with a requirements.txt
. If not, add your dependencies there and then run pip install -t lib -r requirements.txt
to force the libraries to be saved in the lib
folder.
Make sure you've added lib
to appengine_config.py
with vendor.add('lib')
if it's not already there.
Do you have Extra Libraries component for Python installed? It can be installed with
gcloud components install app-engine-python-extras
After installing this extra library you should be able to use built-in flask library without a problem. For more information, refer to this page
Source
tldr: use appengine_config.py and copy your virtualenv to a folder called lib, then make SURE you are running the app via dev_appserver.py
(the below is via bash in ubuntu) SO after a long battle, I find that virtual env and gcloud dont play nice -
I copied everything from my virtual env dir
.../.virtualenvs/nimble/local/lib/python2.7/site-packages
into
[projectdir]/lib
and my appengine_config.py finally worked locally like it does in the cloud, but I absolutely HAVE to run
dev_appserver.py [my proj dir here]
or the google.appengine module wont load. did not know I should be using dev server. I feel very dumb.
for reference, heres the appengine_config.py
"""`appengine_config` gets loaded when starting a new application instance."""
print 'running app config yaya!'
from google.appengine.ext import vendor
vendor.add('lib')
print 'I am the line after adding lib, it should have worked'
import os
print os.getcwd()