Apache Beam Python SDK Quickstart

This guide shows you how to set up your Python development environment, get the Apache Beam SDK for Python, and run an example pipeline.

If you’re interested in contributing to the Apache Beam Python codebase, see the Contribution Guide.

The Python SDK supports Python 3.6, 3.7, and 3.8. Beam 2.24.0 was the last release with support for Python 2.7 and 3.5.

Set up your environment

Check your Python version

The Beam SDK requires Python users to use Python version 3.6 or higher. Check your version by running:

python --version

Install pip

Install pip, Python’s package manager. Check that you have version 7.0.0 or newer by running:

pip --version

If you do not have pip version 7.0.0 or newer, run the following command to install it. This command might require administrative privileges.

pip install --upgrade pip
PS> python -m pip install --upgrade pip

Install Python virtual environment

It is recommended that you install a Python virtual environment for initial experiments. If you do not have virtualenv version 13.1.0 or newer, run the following command to install it. This command might require administrative privileges.

pip install --upgrade virtualenv
PS> python -m pip install --upgrade virtualenv

If you do not want to use a Python virtual environment (not recommended), ensure setuptools is installed on your machine. If you do not have setuptools version 17.1 or newer, run the following command to install it.

pip install --upgrade setuptools
PS> python -m pip install --upgrade setuptools

Get Apache Beam

Create and activate a virtual environment

A virtual environment is a directory tree containing its own Python distribution. To create a virtual environment, create a directory and run:

virtualenv /path/to/directory
PS> virtualenv C:\path\to\directory

A virtual environment needs to be activated for each shell that is to use it. Activating it sets some environment variables that point to the virtual environment’s directories.

To activate a virtual environment in Bash, run:

. /path/to/directory/bin/activate
PS> C:\path\to\directory\Scripts\activate.ps1

That is, execute the activate script under the virtual environment directory you created.

For instructions using other shells, see the virtualenv documentation.

Download and install

Install the latest Python SDK from PyPI:

pip install apache-beam
PS> python -m pip install apache-beam

Extra requirements

The above installation will not install all the extra dependencies for using features like the Google Cloud Dataflow runner. Information on what extra packages are required for different features are highlighted below. It is possible to install multiple extra requirements using something like pip install apache-beam[feature1,feature2].

Execute a pipeline

The Apache Beam examples directory has many examples. All examples can be run locally by passing the required arguments described in the example script.

For example, run wordcount.py with the following command:

python -m apache_beam.examples.wordcount --input /path/to/inputfile --output /path/to/write/counts
python -m apache_beam.examples.wordcount --input /path/to/inputfile \
                                         --output /path/to/write/counts \
                                         --runner FlinkRunner
python -m apache_beam.examples.wordcount --input /path/to/inputfile \
                                         --output /path/to/write/counts \
                                         --runner SparkRunner
# As part of the initial setup, install Google Cloud Platform specific extra components. Make sure you
# complete the setup steps at /documentation/runners/dataflow/#setup
pip install apache-beam[gcp]
python -m apache_beam.examples.wordcount --input gs://dataflow-samples/shakespeare/kinglear.txt \
                                         --output gs://<your-gcs-bucket>/counts \
                                         --runner DataflowRunner \
                                         --project your-gcp-project \
                                         --region your-gcp-region \
                                         --temp_location gs://<your-gcs-bucket>/tmp/
This runner is not yet available for the Python SDK.

After the pipeline completes, you can view the output files at your specified output path. For example, if you specify /dir1/counts for the --output parameter, the pipeline writes the files to /dir1/ and names the files sequentially in the format counts-0000-of-0001.

Next Steps

Please don’t hesitate to reach out if you encounter any issues!