How to Create Python Development Environment In Google Cloud Platform

How to Create Python Development Environment In Google Cloud Platform

In this post, you will learn how to create python application development environment in google cloud platform

In this post, you will learn how to create python application development environment in google cloud platform

Set up a Python development environment on Google Cloud Platform, using Google Compute Engine to create a virtual machine (VM) and installing software libraries for software development.

You perform the following tasks,

  • Provision a Google Compute Engine instance.
  • Connect to the instance using SSH.
  • Install a Python library on the instance.
  • Verify the software installation.

Prerequisites

  • Google cloud account

Follow the below steps to create a Python Application development environment in Google Cloud Platform.

Step 1

Login https://console.cloud.google.com/.

Step 2

Create and connect to a virtual machine,

  1. In the Console, click Navigation menu > Compute Engine > VM Instances.

  1. On the VM Instances page, click Create.

  2. On the Create an instance page, for Name type dev-instance, and select a Region as us-central1 (Iowa) and Zone as us-central1-a.

  3. In the Identity and API access section, select Allow full access to all Cloud APIs.

  4. In the Firewall section, enable Allow HTTP traffic.

  5. Leave the remaining settings as their defaults, and click Create**.**

It takes about 20 seconds for the VM to be provisioned and started

On the VM instances page, in the dev-instance row, click SSH.

This launches a browser-hosted SSH session. If you have a popup blocker, you may need to click twice.

There's no need to configure or manage SSH keys.

Step 3

Install software on the VM instance

In the SSH session, to update the Debian package list, execute the following command,

sudo apt-get update

To install Git, execute the following command,

sudo apt-get install git

When prompted, enter Y to continue, accepting the use of additional disk space.

To install Python, execute the following command,

sudo apt-get install python-setuptools python-dev build-essential

Again, when prompted, enter Y to continue, accepting the use of additional disk space.

To install pip, execute the following command,

_sudo easy_install pip

Step 4

Configure the VM to Run Application Software.

In this section, you verify the software installation on your VM and run some sample code.

Verify Python installation

Still in the SSH window, verify the installation by checking the Python and pip version,

python --version
pip --version

The output provides the version of Python and pip that you installed.

Clone the class repository,

git clone

Change the working directory,

cd ~/training-data-analyst/courses/developingapps/python/devenv/

Run a simple web server,

sudo python server.py

Step 5

Return to the Cloud Console VM instances list (Navigation menu > Compute Engine > Virtual Instances), and click on the External IP address for the dev-instance.

A browser opens and displays a Hello GCP dev! message from Python.

Step 6

  1. Return to the SSH window, and stop the application by pressing Ctrl+c.

  2. Install the Python packages needed to enumerate Google Compute Engine VM instances,

sudo pip install -r requirements.txt

Step 7

Now list your instance in Cloud Shell. Enter the following command to run a simple Python application that lists Compute Engine instances. Replace with your GCP Project ID and is the region you specified when you created your VM. Find these values on the VM instances page of the console,

python list-gce-instances.py <PROJECT_ID> --zone=<YOUR_VM_ZONE>

Your instance name should appear in the SSH terminal window.

Output

Summary

I hope you understood how to create a Python Application development environment in Google Cloud Platform. Stay tuned for more GCP environment articles.

Getting Started with Python working on Google Cloud Functions

Getting Started with Python working on Google Cloud Functions

Writing Python cloud functions is easy and fun, as I will now demonstrate. To follow this tutorial you’ll need a Google Cloud Project, a local Python development environment and the gcloud SDK.

I’ve been a fan of Cloud Functions for a while and a fan of Python since forever, so when support for Python functions was finally announced at Google NEXT’18 I did a small dance of celebration (or at least, I fist-bumped the air when no one was looking). I’m sure you know what Python is, but if you’re new to the serverless world we’re now living in, a quick re-cap on Cloud Functions:

Cloud Functions are small pieces of code that execute in an event-driven manner. They do one small thing very efficiently in reaction to a trigger — usually an HTTP request. The neat thing is you manage zero infrastructure and usually only pay for the execution of your function and a few seconds of compute. You may have heard of other Functions-as-a-Service offerings such as AWS Lambda.

Writing Python cloud functions is easy and fun, as I will now demonstrate. To follow this tutorial you’ll need a Google Cloud Project (you can sign up and get free credits here), a local Python development environment and the gcloud SDK.

Hello, World

We can create a simple function by creating a main.py with just 2 lines:

def hello_world(request):
    return 'Hello, World!\n'

You can deploy this as a cloud Function with the following command. Note that the cloud function name matches the name of the function we defined in code: hello_world

gcloud beta functions deploy hello_world --runtime python37 --trigger-http

After a couple of minutes, the command will return the httpsTrigger or URL for your function. Try accessing this URL and you’ll see the message “Hello, World!” returned. You can also see your function in the Cloud Console UI:

But this is pretty boring stuff, let’s make the function do something a bit more interactive:

def hello_world(request):
    request_json = request.get_json()
    if request_json and 'name' in request_json:
        name = request_json['name']
    else:
        name = 'World'
    return 'Hello, {}!\n'.format(name)

You can use the same gcloud beta functions deploy command as before to update your function. After a couple of minutes, try accessing the URL again and you’ll get the same message. But now try sending it a POST with some data:

curl -X POST <your function URL> -H "Content-Type:application/json" -d '{"name": "Derek"}'

And you should receive a custom message in return! The runtime for Cloud Functions uses the Flask library and provides us with a Request object that we can manipulate however we like.

A More Useful Example

This is all great fun, but now let’s do something useful to show a real world example of where you might employ a Cloud Function. As we’ve seen, Functions are typically triggered by HTTPS endpoints, however you can also make use of Background triggers. Functions deployed this way have no HTTPS endpoint, and are effectively hidden from the Internet, but will instead execute in response to an event such as a file being uploaded to a storage bucket, or a message being sent to a Pub/Sub topic.

Let’s say we have a web service that writes images to a Cloud Storage Bucket. Every time an image is uploaded, we’d like to create a corresponding thumbnail image. Arguably this could be part of our main application code, but for the sake of argument let’s say we want to run this as a separate function, because there may be other ways that images end up in the bucket (maybe an external service sends them to us in batches).

Let’s write a function that reacts when a new file is written to a Cloud Storage bucket. You’ll need 2 GCS buckets for this: You’ll upload images into the first one, and your function will write thumbnails into the second one. In a new directory, create a new main.py file:

from wand.image import Image
from google.cloud import storage

client = storage.Client()

THUMBNAIL_BUCKET = '<your thumbnail bucket>'

def make_thumbnail(data, context):
    # Get the file that has been uploaded to GCS
    bucket = client.get_bucket(data['bucket'])
    blob = bucket.get_blob(data['name'])
    imagedata = blob.download_as_string()
    # Create a new image object and resample it
    newimage = Image(blob=imagedata)
    newimage.sample(200,200)
    # Upload the resampled image to the other bucket
    bucket = client.get_bucket(THUMBNAIL_BUCKET)
    newblob = bucket.blob('thumbnail-' + data['name'])     
    newblob.upload_from_string(newimage.make_blob())

Notice that we’re importing modules, just like we would do with any other Python application. Cloud Functions uses pip, which means we can specify the dependencies we need in a requirements.txt file:

google-cloud-storage
Wand

Just keep your requirements.txt file in the same location as your main.py when you deploy your function.

Don’t forget to replace in the above code with the name of your own thumbnail bucket, and with your source bucket name in the following example. You can now deploy the cloud function like this:

gcloud beta functions deploy make_thumbnail --runtime python37 --trigger-resource <your source bucket>  --trigger-event google.storage.object.finalize

Note: You can’t use the same bucket for both. Why? Because every time the function writes a thumbnail, it would trigger a new invocation of itself!

When you deploy this function you’ll notice we don’t get given a URL. Instead we’ve told the function to run when the google.storage.object.finalize event occurs in the bucket we specified. Let’s upload a picture of Derek the Dog to the source bucket:

Check the thumbnails bucket, and you should see that the function has done its job and created a thumbnail for us!

You can inspect the magic under the hood by revisiting the Cloud Functions part of Cloud Console, where you can track the invocations of your function and even watch its logs in real-time.

Cloud Functions are perfect when you need small pieces of code to tie together larger pieces of a stack. Now that Google has added Python support, we have a huge and diverse ecosystem of functionality available to us to write functions. For more information, read about the Python runtime or follow all of Google’s How To guides.

Thanks for reading!

Top Python Development Companies | Hire Python Developers

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When you look for the developer in hurry you may forget to take note of review and ratings of the company's aspects, but we at TopDevelopers have done a clear analysis of these top reviewed Python development companies listed here and have picked the best ones for you.

List of Best Python Web Development Companies & Expert Python Programmers.

Google Cloud Bigtable vs Google Cloud Datastore

What is the difference between&nbsp;<a href="http://googlecloudplatform.blogspot.ro/2015/05/introducing-Google-Cloud-Bigtable.html" target="_blank">Google Cloud Bigtable</a>&nbsp;and Google Cloud Datastore / App Engine datastore, and what are the main practical advantages/disadvantages? AFAIK Cloud Datastore is build on top of Bigtable.

What is the difference between Google Cloud Bigtable and Google Cloud Datastore / App Engine datastore, and what are the main practical advantages/disadvantages? AFAIK Cloud Datastore is build on top of Bigtable.