PYTHON ON GOOGLE CLOUD PLATFORM

Try It Free View Documentation

Build, deploy, and monitor Python apps at scale. Use Google's APIs to get actionable insights from your data.

  • check Dynamically scale capacity up or down according to traffic
  • check Build, deploy, and manage containerized applications
  • check Debug and fix issues quickly
  • check Provision custom virtual machines or go serverless
  • check Perform data analysis or build machine learning models using powerful APIs
A Broad Set Of Python APIs and Libraries for Both Developers and Data Scientists
Store and retrieve data from Cloud Storage
Query public data using BigQuery
Analyze images with Cloud Vision API
Extract meaning from text using Cloud Natural Language API
Store and retrieve data from Cloud Storage
1
Install
pip install google-cloud-storage
2
Set up a Cloud Platform Console project
  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a GCP Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Cloud Storage API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the GCP Console.

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

3
Write your code
                 
    import os

    import google.cloud.storage

    # Create a storage client.
    storage_client = google.cloud.storage.Client()

    # TODO (Developer): Replace this with your Cloud Storage bucket name.
    bucket_name = 'Name of a bucket, for example my-bucket'
    bucket = storage_client.get_bucket(bucket_name)

    # TODO (Developer): Replace this with the name of the local file to upload.
    source_file_name = 'Local file to upload, for example ./file.txt'
    blob = bucket.blob(os.path.basename(source_file_name))

    # Upload the local file to Cloud Storage.
    blob.upload_from_filename(source_file_name)

    print('File {} uploaded to {}.'.format(
        source_file_name,
        bucket))
                
                
Query public data using BigQuery
1
Install
pip install google-cloud-bigquery
2
Set up a Cloud Platform Console project
  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a GCP Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the BigQuery API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the GCP Console.

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

3
Write your code
                   
    import google.cloud.bigquery

    # Create a BigQuery client.
    bigquery_client = google.cloud.bigquery.Client()

    # Query a public dataset.
    query = bigquery_client.query("""
    #standardSQL
    SELECT * FROM publicdata.samples.natality LIMIT 5;
    """)

    # Print out the results.
    for row in query.result():
        print(row)
                 
                
Analyze images with Cloud Vision API
1
Install
pip install google-cloud-vision
2
Set up a Cloud Platform Console project
  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a GCP Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Cloud Vision API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the GCP Console.

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

3
Write your code
                  
    import io
    import os

    import google.cloud.vision

    # Create a Vision client.
    vision_client = google.cloud.vision.ImageAnnotatorClient()

    # TODO (Developer): Replace this with the name of the local image
    # file to analyze.
    image_file_name = 'Local image to analyze, for example ./cat.jpg'
    with io.open(image_file_name, 'rb') as image_file:
        content = image_file.read()

    # Use Vision to label the image based on content.
    image = google.cloud.vision.types.Image(content=content)
    response = vision_client.label_detection(image=image)

    print('Labels:')
    for label in response.label_annotations:
        print(label.description)
                 
                
Extract meaning from text using Cloud Natural Language API
1
Install
pip install google-cloud-language
2
Set up a Cloud Platform Console project
  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a GCP Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Cloud Natural Language API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the GCP Console.

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

3
Write your code
                  
    import google.cloud.language

    # Create a Language client.
    language_client = google.cloud.language.LanguageServiceClient()

    # TODO (Developer): Replace this with the text you want to analyze.
    text = u'Hello, world!'
    document = google.cloud.language.types.Document(
        content=text,
        type=google.cloud.language.enums.Document.Type.PLAIN_TEXT)

    # Use Language to detect the sentiment of the text.
    response = language_client.analyze_sentiment(document=document)
    sentiment = response.document_sentiment

    print(u'Text: {}'.format(text))
    print(u'Sentiment: Score: {}, Magnitude: {}'.format(
        sentiment.score, sentiment.magnitude))
                 
                
PYTHON QUICK STARTS
Quickly find and Debug issues

Google Stackdriver provides powerful monitoring, logging, and diagnostics. It equips you with insight into the health, performance, and availability of cloud-powered applications, enabling you to find and fix issues faster.

Google Stackdriver
Unified monitoring, logging, and diagnostics for applications on Google Cloud Platform and AWS.
Stackdriver Error Reporting
A walk through of getting an error alert and investigating the error in the Google Cloud Console.
Stackdriver Monitor, diagnose, and fix
In this video, Aja Hammerly uses Stackdriver to find and fix some subtle errors in an example app, and you'll learn how to use Stackdriver on your own projects.
Learn more
PYTHON ON GCP COMMUNITIES

Got something to say? Join our community to ask questions, or just chat with the experts at Google who help build the support for Python on Google Cloud Platform.