Connecting to Google Datastore using gcloud
Question: I am trying to connect to my Google Datastore instance but I can't find any
sample code for how to initialize a connection!

Gcloud's main [documentation](https://googlecloudplatform.github.io/gcloud-
python/latest/datastore-api.html) immediately starts with how to assign an
Entity or a Query but skips the crucial connection bit. The demo section seems
specific to the demo in that it uses test environment variables which is quite
confusing.

Most importantly, I am interested in what environment variables I need to
define and if it is possible to define the service email and path to key from
within the python program.

Highly appreciate it if anyone can provide a sample code of how to commit
something to a datastore instance.

Thanks!


Answer: After looking around, I finally found the environment variables I need define.

To connect to your google datastore from within Python:

    
    
    import os
    
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = <path to private key>
    os.environ['GCLOUD_DATASET_ID'] = <dataset_id, also known as project id>
    
    # Fetching queries should work now
    query = datastore.Query(kind=<kind>, namespace=<namespace>)
    for result in query.fetch():
        print result
    

Google assumes you are using App Engine with Datastore that is why it is
trickier to find these variables if you first introduction to Google Cloud is
its Datastore service.

