4

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 Launcher and go to http://localhost:8080/, I get a HTTP 500 error.

I check the logs and it says No module named flask. Then I check the interactive console in the admin console by running import flask and I get the same error message. I can import flask in any other python file without error.

Is there a way to fix this?

Kylee
  • 1,625
  • 1
  • 31
  • 54
  • 1
    In the interactice console, you can do a "import sys; print sys.path" and check if flask is somewhere on your PYTHONPATH. It sounds like it isn't though. – schuppe Apr 14 '12 at 18:54
  • Have you included flask in your app's source tree? – Nick Johnson Apr 18 '12 at 00:03
  • @NickJohnson That worked. For some reason it didn't occur to me and I some how missed that in the guide. Makes sense though. Thank you. – Kylee Apr 18 '12 at 01:33

5 Answers5

9

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

UPDATED

Running Python, Virtualenv, Flask and GAE on Windows

Install Python

  1. Install Python http://www.python.org/ftp/python/2.7.2/python-2.7.2.msi
  2. Click in Windows Start button and search by "Edit the system environment" and open
  3. Go to the tab Advanced and click on button "Environment Variables…"
  4. When the Environment Variables window opens, choose Path from the System variables list and click Edit…
  5. Add this ;C:\Python27;C:\Python27\Scripts at the end of the value and save

Install setuptools MS Windows installer (Necessary to install PIP on Windows)

  1. Choose the correct installer for you in this page http://pypi.python.org/pypi/setuptools#files( I used this one: http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11.win32-py2.7.exe#md5=57e1e64f6b7c7f1d2eddfc9746bbaf20)
  2. Download the installar and Install that

Install PIP

  1. Download PIP http://pypi.python.org/pypi/pip#downloads
  2. Extract it to any folder
  3. From that directory, type python setup.py install

Install Virtualenv

  1. Execute pip install virtualenv
  2. Execute this mkdir c:\virtualenvs to create a folder to the Virtual Envs
  3. Execute this cd c:\virtualenvs to access that folder
  4. Execute virtualenv flaskdemo to create a virtualenv for you project
  5. Active the virtualenv c:\virtualenvs\flaskdemo\scripts\activate

Install Google App Engine SDK

  1. Install the SDK https://developers.google.com/appengine/downloads

Create the project

  1. Create a directory for your project
  2. Create the main of your application https://github.com/maxcnunes/flaskgaedemo/blob/master/main.py
  3. Create the configuration of your appliction for Google App Engine https://github.com/maxcnunes/flaskgaedemo/blob/master/app.yaml
  4. Create a file to let GAE initialize your application https://github.com/maxcnunes/flaskgaedemo/blob/master/initialize_gae.py

(Look a example of the code here: https://github.com/maxcnunes/flaskgaedemo )

Install Flask to run Locally

  1. Execute pip install flask

Install Flask to run on the GAE

  1. Download Flask https://github.com/mitsuhiko/flask/archive/0.9.zip and extract the folder flask inside your project
  2. Download Werkzeug https://github.com/mitsuhiko/werkzeug/archive/0.8.3.zip and extract the folder werkzeug inside your project
  3. Download Jinja2 https://github.com/mitsuhiko/jinja2/archive/2.6.zip and extract the folder jinja2 inside your project
  4. Download Simple Json https://github.com/simplejson/simplejson/archive/v3.0.5.zip and extract the folder simplejson inside your project

Running the application with GAE SDK

  1. Open Google App Engine Launcher
  2. Add a new application
  3. Run the application
  4. Click in Browse button to open your application on browser
  5. Finally click on Deploy button to deploy your application
Community
  • 1
  • 1
maxcnunes
  • 2,927
  • 1
  • 20
  • 26
  • 1
    I'm following the same tutorial mentioned in the original post and I'm getting the same error. In my virtual environment flask is listed, which means it is there. I'm using windows, do you have suggestions on what could be causing the problem? – lv10 Jan 11 '13 at 06:02
  • My flask app has a long list of dependencies. Your step at "Install Flask to run on the GAE" seems to be implying I need to install them all manually now? I have complete requirements.txt that is pushed and have an appengine_config.py which isnt logging any errors, and flask installs just fine, it just dies at flask_sqlalchemy, which is in my requirements.txt and runs fine locally – Vincent Buscarello Dec 21 '16 at 23:09
4

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.

Gerard
  • 2,832
  • 3
  • 27
  • 39
jackar
  • 724
  • 5
  • 16
  • You mean `pip install -t lib -r requirements.txt`, the requirements.txt arguments needs to come immediately after the `-r` flag – Gerard Dec 16 '16 at 13:56
1

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.

vinit payal
  • 1,201
  • 2
  • 13
  • 27
0

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()
Vincent Buscarello
  • 395
  • 2
  • 7
  • 19
0

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

Garvit Jain
  • 1,862
  • 2
  • 19
  • 27