2

I've been experimenting with GAE, and struggling to get Google's flask example working with dev_appserver.py. Despite using the simple code given, I was receiving the dreaded 'ImportError: No module named flask' error. (It did work via the flask server, if needed, tho' obviously this didn't help.)

This is despite having flask (and all related dependencies) installed in the /lib sub-folder of my project folder, and despite the (supposedly) correct code in appengine_config.py (see all below). I also tried multiple small variations of these files, to no avail.


THEN... I saw this post, and tried just copying all of the contents of projectfolder/ lib/python/site-packages into projectfolder/ ... and now it seems to work perfectly.

Can anyone see what am I missing (as I'd prefer to have the neat folder structure as advertised)? Or is this a GAE bug, as it seems that vendor.add just isn't doing what it should?


app.yaml

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app

main.py

import logging
from flask import Flask, render_template, request

app = Flask('test')

@app.route('/')
def hello():
    return 'Hello World!'

appengine_config.py

from google.appengine.ext import vendor

print "*** appengine_config ***"     # Just to see if/when it's being run

# Add any libraries installed in the "lib" folder.
vendor.add('lib')
Community
  • 1
  • 1
Tom M
  • 387
  • 1
  • 3
  • 9

2 Answers2

1

Make sure all your libraries are installed using pip into app/lib (assuming app is your project name), delete the sub-folder app/lib/lib and re-run.

Explanation:

If you look at the vendor.py code from Google here, there is a hint: https://cloud.google.com/appengine/docs/standard/python/refdocs/modules/google/appengine/ext/vendor

The relevant portion is this:

venv_path = os.path.join(path, 'lib', PYTHON_VERSION, 'site-packages')
if os.path.isdir(venv_path):
    site_dir = venv_path
elif os.path.isdir(path):
    site_dir = path

So the appengine_config.py file first tries to look in project_folder/lib/python2.7/site-packages. That is the state of your current configuration. For a cleaner code base you want to be hitting the elif branch of that code.

I had this problem a while ago. I was using vendor.add('lib') in the appengine_config.py file and storing all my necessary 3rd party libraries there. Then at some point I did a pip install using an incorrect installation prefix and it had ended up creating the folder lib/python2.7/site-packages inside my app/lib folder. So the App Engine environment ended up finding all the libraries that I had installed with the incorrect pip installation, but none of the libraries which were in the app/lib folder itself. I just had to delete the folder app/lib/lib, re-do the pip install for the incorrectly installed libraries, and everything worked again.

Hope this helps solve your problem.

ShayakB
  • 178
  • 8
  • Partly - and it helped me figure out the problem - so thanks! The problem was that the configuration of the path within the main app folder was actually `lib/lib/python/site-packages`, when it should have been `lib/lib/python2.7/site-packages`. It's not ideal to have to manually change it to `python2.7` as the original path was the one made by PIP when installing the local copies of the packages. But at least it works. – Tom M Apr 25 '17 at 20:41
  • Or, of course, `vendor.add('lib/lib/python/site-packages')` points it right to the correct original folder. – Tom M Apr 25 '17 at 20:49
0

I found the answer for myself here : https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#vendoring

I had to replace vendor.add('lib') with this more explicit version and it resolved the issue immediately :

vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
samaspin
  • 2,342
  • 1
  • 26
  • 31