Setting Up a Python Development Environment

This tutorial shows how to prepare your local machine for Python development, including developing Python applications that run on Google Cloud Platform (GCP).

If you already have a development environment setup, see Python and Google Cloud Platform to get an overview of how to run Python apps on GCP.

Objectives

  • Install the latest versions of Python 2 and 3
  • Install and use virtualenv
  • Install an editor (optional)
  • Install the Google Cloud SDK (optional)
  • Install the Google Cloud Client Library for Python (optional)
  • Install other useful tools

Installing Python

Python's installation instructions vary by operating system. Follow the guide for the operating system you're using on your local development machine.

Mac OS X

While OS X includes a version of Python 2 by default, we don't recommend that you use this version for development. The operating system uses that Python installation for its own purposes, so using it for development can often lead to problems. It also tends to lag behind the upstream Python version. For these reasons, it's better to perform a separate install of the latest versions of Python 2 and Python 3.

Our recommended way of installing Python 2 and 3 is to use homebrew.

You will need a compiler in order to use homebrew and install Python and Python packages. You can get this by installing XCode's Command Line Tools. The easiest way to do this is to run the following in a terminal:

xcode-select --install

Install homebrew by following the instructions on the homebrew homepage, then use homebrew to install both Python 2 and Python 3:

brew install python python3

After the installations are complete, you should have both Python 2 and Python 3 installed available as python2 and python3 respectively. pip2 (for Python 2) and pip3 (for Python 3) will also be installed as package management tools. You can verify this by running the following:

$ python2 --version
2.7.13
$ python3 --version
3.6.1
$ pip2 --version
9.0.1
$ pip3 --version
9.0.1

Your versions of Python might be newer than the output shown here.

Continue on to installing and using virtualenv.

Windows

Download the installers for the latest versions of both Python 3 and Python 2 from the Python website.

After you have both installed, you should have access to python and pip from within the command prompt or Powershell:

> python --version
Python 2.7.13
> pip --version
pip 9.0.1 from  c:\python27\lib\site-packages (Python 2.7.13)

Your versions of Python and pip might be newer than the output shown here.

Continue on to installing and using virtualenv.

Linux

Most Linux distributions include recent versions of Python. Install the appropriate packages for your distribution. For Debian and Ubuntu, these packages are python, python-dev, python3, and python3-dev:

sudo apt update
sudo apt install python python-dev python3 python3-dev

You will also need pip. While Debian and most other distributions include a python-pip package, we recommend that you install pip yourself to get the latest version:

wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py

Afterwards, you should have pip:

$ pip --version
pip 9.0.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)

Your version of pip might be newer than the output shown here.

Continue on to installing and using virtualenv.

Installing and using virtualenv

virtualenv ("virtual environment") is a tool that creates isolated Python environments. These isolated environments can have their own separate versions of Python packages, which allows you to isolate your projects dependencies from the dependencies of other projects. We recommend that you always use a per- project virtualenv when developing locally with Python.

You will need to install virtualenv globally before you can use it. It doesn't matter if you install it with Python 2 or Python 3:

pip install --upgrade virtualenv

After you install virtualenv, you can create a virtualenv in your project. Use the --python flag to tell virtualenv which Python version to use:

cd your-project
virtualenv --python python3 env

If you're on Windows, you may need to specify the full path to the Python installation directory:

virtualenv --python "c:\python36\python.exe" env

virtualenv will create a virtual copy of the entire Python installation in the env folder.

After the copy is created, you'll need to "activate" the virtualenv. Activating the virtualenv tells your shell to use the virtualenv's paths for Python.

For Mac OS X and Linux:

source env/bin/activate

For Windows:

.\env\Scripts\activate

Now you can install packages and be confident that they won't affect other projects or your global Python installation:

pip install google-cloud-storage

If you want to stop using the virtualenv and go back to your global Python, you can deactivate it:

deactivate

You can read more about virtualenv on the Python Guide or the virtualenv docs

Install an editor

Popular editors (in no particular order) used to develop Python applications include, but are not limited to:

Install the Google Cloud SDK

The Google Cloud SDK is a set of tools for Google Cloud Platform. It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google BigQuery, and other products and services from the command line. You can run these tools interactively or in your automated scripts.

Install the Google Cloud Client Library for Python

The Google Cloud Client Library for Python is the idiomatic way for Python developers to integrate with Google Cloud Platform services, like Cloud Datastore and Cloud Storage. You can install the package for an individual API, like Cloud Storage for example:

pip install --upgrade google-cloud-storage

Next Steps

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...