Google App Engine

Common Tasks and How to Do Them in Python

Can I do common MySQL-like queries on the datastore?

Google's App Engine datastore uses a non-relational object model to store information, which allows you to create fast, scalable applications. Because this method of storing data differs from relational databases, such as MySQL, not all functionalities used with relational databases are available with the system. For instance, the Google App Engine datastore does not have the ability to 'join' tables that is available in a relational database.

We have full CRUD operation (create, retrieve, update and delete) support, and allow you to query against our datastore using GQL, our query language. You may also use our Query class, which allows you to use filters and ordering when retrieving the data, as well as perform ancestors queries for an entity.

Datastore filters allow you to restrict the data retrieved from the datastore by using common comparison operators: < <= = >= >

The ordering operations allow you to organize how the data is returned from the query. You can view the ordering operations for each type available with the datastore here.

Lastly, when retrieving the data from the datastore, you can specify both a limit and an offset to the results returned. The limit is maximum number of results returned by the query. The fetch offset specifies the number of results to skip when returning the data from the datastore.

What third party libraries can I use in my application?

You can use any pure Python third party libraries in your Google App Engine application. In order to use a third party library, simply include the files in your application's directory, and they will be uploaded with your application when you deploy it to our system. You can import the files as you would any other Python files with your application.

Can I use Django with Google App Engine?

Absolutely. We've put together an introductory article explaining how to get started with Django and Google App Engine.

Can I import my own version of a module that is included with Google App Engine?

If you would like to import your own version of a module included with Google App Engine, you will need to follow a few simple steps to get your import working correctly. First, include the files for the module that you wish to import in your application's directory.

Before importing the modules in your application's code, you must delete all references to the existing module in sys.modules, and then move your application's directory to the front of the sys.path so that your version gets imported. Below, we give an example of how you would import a different version of Django than the one included with Google App Engine:

# Delete the preloaded copy of Django
for key in [key for key in sys.modules if key.startswith('django')]:
  del sys.modules[key]

To move your application's directory to the front of the sys.path:

# Force sys.path to have our own directory first, so we can import from it
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

How do I profile my app's performance?

To profile the RPC (Remote Procedure Call) performance of your application, App Engine provides a tool called Appstats. Appstats will allow you to see exactly what API calls your app is making for a given request and how long they take. To use Appstats, follow the instructions for Python or Java.

To profile your Python application using cProfile, rename your application's main() function to real_main(). Then, add a new main function to your application, named profile_main()such as the one below:

def profile_main():
    # This is the main function for profiling
    # We've renamed our original main() above to real_main()
    import cProfile, pstats
    prof = cProfile.Profile()
    prof = prof.runctx("real_main()", globals(), locals())
    print "<pre>"
    stats = pstats.Stats(prof)
    stats.sort_stats("time")  # Or cumulative
    stats.print_stats(80)  # 80 = how many to print
    # The rest is optional.
    # stats.print_callees()
    # stats.print_callers()
    print "</pre>"

This function will append the profile output to your HTML response. If instead you prefer to log the output, you can modify the profiler's main function as such:

def profile_main():
    # This is the main function for profiling
    # We've renamed our original main() above to real_main()
    import cProfile, pstats, StringIO
    prof = cProfile.Profile()
    prof = prof.runctx("real_main()", globals(), locals())
    stream = StringIO.StringIO()
    stats = pstats.Stats(prof, stream=stream)
    stats.sort_stats("time")  # Or cumulative
    stats.print_stats(80)  # 80 = how many to print
    # The rest is optional.
    # stats.print_callees()
    # stats.print_callers()
    logging.info("Profile data:\n%s", stream.getvalue())

The log message will be rather verbose, and viewable in your Admin Console. For more information on logs, please see our article located here.

To enable the profiling with your application, set main = profile_main. To run your application as normal, simply set main = real_main.

(Note that cProfile was added in Python 2.5, which I recommend using since this is the version that App Engine uses to run your application. If you are using an earlier version of Python, you will not be able to do profiling locally.)

When I run Django, I'm getting an error stating that ROOT_URLCONF is not found.

Since Google App Engine's webapp framework uses Django templates, Django will half-initialize when webapp is loaded. This causes the initialization of the rest of Django's setting to be skipped. If you are getting this error, you need to explicitly force Django to reload your settings:

from django.conf import settings
settings._target = None

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.