2

My App used python libraries provided by the AppEngine Standard Environment. For including the library in my Local Development I followed the instructions on https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27.

# appengine_config.py
from google.appengine.ext import vendor

# Add any libraries install in the "lib" folder.
vendor.add('lib')

I pip installed the libraries to a folder 'lib' and in appengine_config.py added vendor.add('lib') I would like "vendor.add('lib')" to be effective/run only when the application is in Local and not in Google Cloud.

What is the right way to identify the environment? How about the below in appengine_config.py?

if 'localhost' in os.environ['SERVER_NAME']:
    vendor.add('lib')
user362953
  • 435
  • 2
  • 13

3 Answers3

2

If your app uses a GAE provided library then you're not including it properly (you're vendoring it in, which is what you do with libraries not provided by GAE). From Requesting a library:

You can request a library by using the libraries: directive in app.yaml.

libraries:
- name: PIL
  version: "1.1.7"
- name: webob
  version: "1.1.1"

Note that: The library must be one of the supported runtime-provided third-party libraries.

When deployed, App Engine will provide the requested libraries to the runtime environment. Some libraries must be installed locally.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • Thanks Dan. When using "libraries" directive, when run locally, would the libraries be installed? Or is the library expected to be installed? in the standard path? In this case, the library is django. I am using "libraries" directive. django is installed on my machine. However, the App fails to run with django import error. I tried looking into dev_appserver.py, update_manager.py to find code that installs packages when run locally. I see that install check is done for 'app-engine-python' 'component'. But, couldn't find anything that looks into "libraries" directive. – user362953 Jan 22 '17 at 18:03
  • A few libs need special attention, see the link in the quote. Django is one of them https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#django – Dan Cornilescu Jan 24 '17 at 18:44
  • I got to get back to this only now. A. The link seems to refer to only the below libs and django isn't in the list. Do you agree? lxml matplotlib mysqldb numpy PIL crcmod pycrypto B. Assuming django is in the list, is the suggestion to install django as third-party lib and vendor it? – user362953 Feb 06 '17 at 22:16
  • I must add that, when I looked into a different environment of the same application, where google-cloud-sdk and google-cloud-sdk-app-engine-python are installed, google-cloud-sdk/platform/google_appengine/lib/django-1.4 is in sys.path of my webapp2 handlers. wrapper_util.py has a class where this path is configured under v2_extra_paths. – user362953 Feb 06 '17 at 22:47
  • I failed to notice the question in your Jan22 comment: GAE-provided libraries should be part of the GAE/Cloud SDK - inline with the path you just mentioned. This also reminds me - all django libs were missing in some recent Cloud SDK version, see http://stackoverflow.com/questions/41506854/pycharm-cant-create-app-engine-application-using-django – Dan Cornilescu Feb 07 '17 at 02:17
1

According to the official documentation, you should probably look in the SERVER_SOFTWARE environment variable:

To find out whether your code is running in production or in the local development server, check if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'). When this is True, you're running in production; otherwise, you're running in the local development server.

mgilson
  • 300,191
  • 65
  • 633
  • 696
1

Similar to the answer given by @mgilson I tend to use

DEBUG = os.environ.get('SERVER_SOFTWARE','').startswith('Dev')

If DEBUG is True then you're running in the local environment, otherwise it's live.

Aaron
  • 801
  • 1
  • 7
  • 12