How To Set up a Development Environment in Python in Google Cloud

In this blog, you 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.

Compute Engine is just one resource provided on Google Cloud Platform.

Google Cloud Platform

Google Cloud Platform (GCP) consists of a set of physical assets, such as computers and hard disk drives, and virtual resources, such as virtual machines (VMs), that are contained in Google’s data centers around the globe. Each data center location is in a global region. Regions include Central US, Western Europe, and East Asia. Each region is a collection of zones, which are isolated from each other within the region. Each zone is identified by a name that combines a letter identifier with the name of the region. For example, zone a in the East Asia region is named asia-east1-a.

This distribution of resources provides several benefits, including redundancy in case of failure and reduced latency by locating resources closer to clients. This distribution also introduces some rules about how resources can be used together.

Projects

Any GCP resources that you allocate and use must belong to a project. You can think of a project as the organizing entity for what you’re building. A project is made up of the settings, permissions, and other metadata that describe your applications. Resources within a single project can work together easily, for example by communicating through an internal network, subject to the regions-and-zones rules. The resources that each project contains remain separate across project boundaries; you can only interconnect them through an external network connection.

Each GCP project has:

A project name, which you provide. A project ID, which you can provide or GCP can provide for you. A project number, which GCP provides. As you work with GCP, you’ll use these identifiers in certain command lines and API calls. The following screenshot shows a project name, its ID, and number:

The Google Cloud Platform Console displays project ID and name

In this example:

Example Project is the project name. example-id is the project ID. 123456789012 is the project number. Each project ID is unique across GCP. Once you have created a project, you can delete the project but its ID can never be used again.

When billing is enabled, each project is associated with one billing account. Multiple projects can have their resource usage billed to the same account.

A project serves as a namespace. This means every resource within each project must have a unique name, but you can usually reuse resource names if they are in separate projects. Some resource names must be globally unique. Refer to the documentation for the resource for details.

In this post, you provision a Google Compute Engine virtual machine (VM) and install software libraries for Python software development on Google Cloud Platform (GCP).

Ways to interact with the services

GCP gives you three basic ways to interact with the services and resources.

  • Google Cloud Platform Console: a web-based, graphical user interface that you can use to manage your GCP projects and resources.
  • Command-line interface
    • Google Cloud SDK: provides the gcloud command-line tool, which gives you access to the commands you need.
    • Cloud Shell: a browser-based, interactive shell environment for GCP. You can access Cloud Shell from the GCP console. If you prefer to work in a terminal window, the Google Cloud SDK provides the gcloud command-line tool, which gives you access to the commands you need. The gcloud tool can be used to manage both your development workflow and your GCP resources. See the gcloud reference for the complete list of available commands.
  • Client libraries: The Cloud SDK includes client libraries that enable you to easily create and manage resources. GCP client libraries expose APIs to provide access to services and resource management functions. You also can use the Google API client libraries to access APIs for products such as Google Maps, Google Drive, and YouTube.

let’s log in into responsible accounts:

Choose an account

TO view menu visit:

Cloud Console Menu

Create a Compute Engine Virtual Machine Instance

In this section, you use the GCP Console to provision a new Google Compute Engine (VM) instance.

Create and connect to a virtual machine

  1. In the Console, click Navigation menu > Compute Engine > VM Instances.
61ec7e30bc5889e9.png
  1. On the VM Instances page, click Create.
  2. On the Create an instance page, for Name type dev-instance, and select a Regionas us-central1 (Iowa) and Zone as us-central1-a.GCP Regions and Zones: GCP offers products and services in multiple distinct geographic locations, called regions. Each region has multiple distinct zones. Each zone is isolated from other zones in terms of power and internet connectivity.
  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
3814304993882070.png

Install software on the VM instance

  1. In the SSH session, to update the Debian package list, execute the following command:sudo apt-get update
  2. 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.
  3. 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.
  4. To install pip, execute the following command:sudo easy_install pip

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

  1. 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.
  2. Clone the class repository:git clone https://github.com/GoogleCloudPlatform/training-data-analyst
  3. Change the working directory:cd ~/training-data-analyst/courses/developingapps/python/devenv/
  4. Run a simple web server:sudo python server.py
  5. Return to the Cloud Console VM instances list (Navigation menu > Compute EngineVirtual Instances), and click on the External IP address for the dev-instance.A browser opens and displays a Hello GCP dev! message from Python.
  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
  3. Now list your instance in Cloud Shell. Enter the following command to run a simple Python application that lists Compute Engine instances. Replace <PROJECT_ID> with your GCP Project ID and <YOUR_VM_ZONE> 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.Example output: 
instance_output.png
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s