Setting up a Python development environment

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

If you already have a development environment set up, see Python and GCP 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 Cloud SDK (optional).
  • Install the Cloud Client Libraries 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 running in your development environment, MacOS, Windows, or Linux.

macOS

macOS includes a version of Python 2 by default and uses it for its own purposes. To avoid interfering with macOS, we recommend creating a separate development environment and installing the latest versions of Python 2 and Python 3. To install Python 2 and 3, use homebrew.

  1. To use homebrew to install Python and Python packages, you need a compiler, which you can get by installing Xcode's command-line tools.

    xcode-select --install
    
  2. Install homebrew by following the instructions on the homebrew homepage, and then use homebrew to install both Python 2 and Python 3 as follows:

    brew install python@2 python
    
  3. After the installations are complete, verify that both Python 2 and Python 3 are available as python2 and python3 respectively, and that pip2 (for Python 2) and pip3 (for Python 3) are also installed as package management tools.

    To verify that Python 2 is available, run the following command:

    $ python2 --version
    

    The output shows the version. You can learn about the latest version of Python 2 homebrew in the Homebrew Python@2 Formulae page.

    To verify that Python 3 is available, run the following command:

    $ python3 --version
    

    The output shows the version. You can learn about the latest version of Python 3 homebrew in the Homebrew Python Formulae page.

    To verify that pip2 and pip3 are available, run the following command:

    $ pip2 --version
    
    $ pip3 --version
    

    The outputs show the versions. You can learn about the latest version of pip in the pip Release Notes.

    Homebrew installs automatically the latest versions of Python 2 and Python 3 available on your platform via Homebrew. The version numbers in the outputs may be different from the latest official releases of Python.

Windows

  1. To install Python 2 and 3 in a Windows environment, download the installers for the latest versions of both Python 2 and Python 3 from the Python website.

    • When you install each version, make sure you select the Add Python to PATH option for both versions. If you didn't do this, you need to add Python's installation directory and the Scripts folder to your path, for example: C:\Python27\;C:\Python27\Scripts\.

    • When you install Python 3, make sure to install the Python launcher for Windows, which is enabled by default.

  2. To access your version of Python, use Python launcher for Windows.

    To start the latest version of Python you installed, run the following command:

    py
    

    To start the latest version of Python 2 you installed, run the following command:

    py -2
    

    To start the latest version of Python 3 you installed, run the following command:

    py -3
    

    To verify the version of pip that is available, run the following command:

    pip --version
    

    The output shows the version from C:\python27\lib\site-packages.

    To verify that pip3 is available, run the following command:

    pip3 --version
    

    The output shows the version from C:\users\[USERNAME]\appdata\local\programs\python\python36-32\lib\site-packages.

    You can learn about the latest version of pip in the pip Release Notes.

Linux

Most Linux distributions include recent versions of Python.

  1. To install Python 2 and 3 in a Linux environment, install the appropriate packages for your distribution. For Debian and Ubuntu, these packages are python, python-dev, python3, and python3-dev.

    Install these packages using the following commands:

    sudo apt update
    sudo apt install python python-dev python3 python3-dev
    
  2. You also need to install 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
    
  3. After the installations are complete, verify that you have pip installed:

    $ pip --version
    

    The output shows the version from /usr/local/lib/python2.7/dist-packages. You can learn about the latest version of pip in the pip Release Notes.

Installing and using the virtualenv tool

virtualenv 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 one project's dependencies from the dependencies of other projects. We recommend that you always use a per-project virtual environment when developing locally with Python.

  1. Install virtualenv globally.

    macOS

    Use pip2 install --upgrade virtualenv or pip3 install --upgrade virtualenv.

    Windows

    To install pip with Python 2 or Python 3, use pip install --upgrade virtualenv.

    Linux

    To install pip with Python 2 or Python 3, use pip install --upgrade virtualenv.

  2. After you install virtualenv, you can create a virtual environment in your project. virtualenv creates a virtual copy of the entire Python installation in the env folder.

    macOS

    Use the --python flag to tell virtualenv which Python version to use:

    cd your-project
    virtualenv --python python3 env
    

    Windows

    Use the --python flag to tell virtualenv which Python version to use:

    cd your-project
    virtualenv --python python3 env
    

    You might need to specify the full path to the Python installation directory:

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

    Linux

    Use the --python flag to tell virtualenv which Python version to use:

    cd your-project
    virtualenv --python python3 env
    
  3. After the copy is created, set your shell to use the virtualenv paths for Python by activating the virtual environment as follows.

    macOS

    source env/bin/activate
    

    Windows

    .\env\Scripts\activate
    

    Linux

    source env/bin/activate
    
  4. Now you can install packages without affecting other projects or your global Python installation:

    pip install google-cloud-storage
    

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

    deactivate
    

You can read more about virtualenv in the Python guide or virtualenv docs.

Installing an editor

To develop Python apps, you need an editor. Here are a few of the more popular editors (in no particular order):

Installing the Cloud SDK

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

Installing the Cloud Client Libraries for Python

The Cloud Client Libraries for Python is how Python developers integrate with GCP services like Cloud Datastore and Cloud Storage. To install the package for an individual API like Cloud Storage, use a command similar to the following:

pip install --upgrade google-cloud-storage

What's next

Trang này có hữu ích không? Hãy cho chúng tôi biết đánh giá của bạn:

Gửi phản hồi về...