Install Python dependencies

Cloud Composer 3 | Cloud Composer 2 | Cloud Composer 1

This page describes how to install Python packages for yourCloud Composer environment.

About packages in Cloud Composer

This section explains how PyPI packages work in Cloud Composer.

Preinstalled and custom PyPI packages in Cloud Composer images

Cloud Composer images contains both preinstalled and custom PyPIpackages.

  • Preinstalled PyPI packages are packages that are included inthe Cloud Composer image of your environment. EachCloud Composer image containsPyPI packages that are specific for your version ofCloud Composer and Airflow.

  • Custom PyPI packages are packages that you can install in your environmentin addition to preinstalled packages.

Note: If you plan to upgrade your environment, then you can do anupgrade check. This check reports if custom PyPI packagesin your environment have any conflicts with preinstalled packages in a laterAirflow build.

Options to manage PyPI packages for Cloud Composer environments

OptionUse if
Install from PyPIThe default way to install packages in your environment
Install from a repository with a public IP addressThe package is hosted in a package repository other than PyPI.This repository has a public IP address
Install from an Artifact Registry repositoryThe package is hosted in an Artifact Registry repository
Install from a repository in your project's networkYour environment does not have access to public internet. The packageis hosted in a package repository in your project's network.
Install as a local Python libraryThe package cannot be found in PyPI, and the librarydoes not have any external dependencies, such asdist-packages.
Install a pluginThe package provides plugin-specific functionality, such as modifyingthe Airflow web interface.
PythonVirtualenvOperatorYou don't want the package to be installed for all Airflow workers, orthe dependency conflicts with preinstalled packages. The packagecan be found in the PyPI and has no external dependencies.
KubernetesPodOperator andGKE operatorsYou require external dependencies that cannot be installed frompip,such asdist-packages, or are on an internal pip server. Thisoption requires more setup and maintenance. Consider it only if otheroptions don't work.

Before you begin

View the list of PyPI packages

You can get the list of packages for your environment in several formats.

View preinstalled packages

To view the list of preinstalled packages for your environment, seethe list of packages for theCloud Composer image of your environment.

View all packages

To view all packages (both preinstalled and custom) in your environment:

gcloud

The following gcloud CLI command returns the result ofthepython -m pip list command for an Airflow worker in your environment.You can use the--tree argument to get the result of thepython -m pipdeptree --warn command.

gcloudbetacomposerenvironmentslist-packages\ENVIRONMENT_NAME\--locationLOCATION

Replace:

  • ENVIRONMENT_NAME with the name of the environment.
  • LOCATION with the region where the environment is located.

View custom PyPI packages

Console

  1. In Google Cloud console, go to theEnvironments page.

    Go to Environments

  2. In the list of environments, click the name of your environment.TheEnvironment details page opens.

  3. Go to thePyPI Packages tab.

gcloud

gcloudcomposerenvironmentsdescribeENVIRONMENT_NAME\--locationLOCATION\--format="value(config.softwareConfig.pypiPackages)"

Replace:

  • ENVIRONMENT_NAME with the name of the environment.
  • LOCATION with the region where the environment is located.

Install custom packages in a Cloud Composer environment

This section describes different methods for installing custom packages in yourenvironment.

Install packages from PyPI

A package can be installed fromPython Package Index if it has no externaldependencies or conflicts with preinstalled packages.

Note: You can loosen version constraints for installed custom PyPI packages.For example, instead of specifying a version as==1.0.1, specify itas>=1.0.1.

To add, update, or delete the Python dependencies for your environment:

Console

  1. In Google Cloud console, go to theEnvironments page.

    Go to Environments

  2. In the list of environments, click the name of your environment.TheEnvironment details page opens.

  3. Go to thePyPI packages tab.

  4. ClickEdit

  5. ClickAdd package.

  6. In thePyPI packages section, specify package names, with optionalversion specifiers and extras.

    For example:

    • scikit-learn
    • scipy,>=0.13.3
    • nltk,[machine_learning]
  7. ClickSave.

gcloud

gcloud CLI has several arguments for working with custom PyPIpackages:

  • --update-pypi-packages-from-file replaces all existing customPyPI packages with the specified packages. Packages that you don'tspecify are removed.
  • --update-pypi-package updates or installs one package.
  • --remove-pypi-packages removes specified packages.
  • --clear-pypi-packages removes all packages.

Installing requirements from a file

Therequirements.txt file must have eachrequirement specifier on a separate line.

For example:

scipy>=0.13.3scikit-learnnltk[machine_learning]

Update your environment, and specify therequirements.txt file inthe--update-pypi-packages-from-file argument.

gcloudcomposerenvironmentsupdateENVIRONMENT_NAME\--locationLOCATION\--update-pypi-packages-from-filerequirements.txt

Replace:

  • ENVIRONMENT_NAME with the name of the environment.
  • LOCATION with the region where the environment is located.

Installing one package

Update your environment, and specify the package, version, and extras inthe--update-pypi-package argument.

gcloudcomposerenvironmentsupdateENVIRONMENT_NAME\--locationLOCATION\--update-pypi-packagePACKAGE_NAMEEXTRAS_AND_VERSION

Replace:

  • ENVIRONMENT_NAME with the name of the environment.
  • LOCATION with the region where the environment is located.
  • PACKAGE_NAME with the name of the package.
  • EXTRAS_AND_VERSION with the optional version and extras specifier. Toomit versions and extras, specify an empty value.
Note: Depending on your execution environment, you might need to escapespecial characters such as>. To avoid potential issues, putthe package name and version in quotes, for example:"scipy>=0.13.0".

Example:

gcloudcomposerenvironmentsupdateexample-environment\--locationus-central1\--update-pypi-package"scipy>=0.13.3"

Removing packages

Update your environment, and specify the packages that you want to delete in the--remove-pypi-packages argument:

gcloudcomposerenvironmentsupdateENVIRONMENT_NAME\--locationLOCATION\--remove-pypi-packagesPACKAGE_NAMES

Replace:

  • ENVIRONMENT_NAME with the name of the environment.
  • LOCATION with the region where the environment is located.
  • PACKAGE_NAMES with a comma-separated list of packages.

Example:

gcloudcomposerenvironmentsupdateexample-environment\--locationus-central1\--remove-pypi-packagesscipy,scikit-learn

API

Construct anenvironments.patch API request.

In this request:

  1. In theupdateMask parameter, specify the mask:

    • Useconfig.softwareConfig.pypiPackages mask to replace all existingpackages with the specified packages. Packages that you don'tspecify are deleted.
    • Useconfig.softwareConfig.envVariables.PACKAGE_NAME to add orupdate a specific package. To add or update several packages,specify several masks with commas.
  2. In the request body, specify packages and values for versions and extras:

    {"config":{"softwareConfig":{"pypiPackages":{"PACKAGE_NAME":"EXTRAS_AND_VERSION"}}}}

    Replace:

    • PACKAGE_NAME with the name of the package.
    • EXTRAS_AND_VERSION with the optional version and extras specifier. Toomit versions and extras, specify an empty value.
    • To add more than one package, add extra entries for packagestopypiPackages.

Example:

// PATCH https://composer.googleapis.com/v1/projects/example-project/// locations/us-central1/environments/example-environment?updateMask=// config.softwareConfig.pypiPackages.EXAMPLE_PACKAGE,// config.softwareConfig.pypiPackages.ANOTHER_PACKAGE{"config":{"softwareConfig":{"pypiPackages":{"EXAMPLE_PACKAGE":"","ANOTHER_PACKAGE":">=1.10.3"}}}}

Terraform

Thepypi_packages block in thesoftware_config block specifiespackages.

resource"google_composer_environment""example"{name="ENVIRONMENT_NAME"region="LOCATION"config{software_config{pypi_packages={PACKAGE_NAME="EXTRAS_AND_VERSION"}}}}

Replace:

  • ENVIRONMENT_NAME with the name of the environment.
  • LOCATION with the region where the environment is located.
  • PACKAGE_NAME with the name of the package.
  • EXTRAS_AND_VERSION with the optional version and extras specifier. Toomit versions and extras, specify an empty value.
  • To add more than one package, add extra entries for packagestopypi_packages.

Example:

resource"google_composer_environment""example"{name="example-environment"region="us-central1"config{software_config{pypi_packages={scipy=">=1.10.3"scikit-learn=""nltk="[machine_learning]"}}}}

Install packages from a public repository

You can install packages hosted in other repositories that have a public IPaddress.

The packages must beproperly configured, so that thedefaultpip tool can install it.

Note: Cloud Composer does not supportpip customization and doesnot resolve package dependencies and conflicts outside of the resolutionmechanisms that the defaultpip tool provides.

To install from a package repository that has a public address:

  1. Create apip.conf file and include the followinginformation in the file, if applicable:

    • URL of the repository (in theindex-url parameter)
    • Access credentials for the repository
    • Non-defaultpip installation options

    Example:

    [global]index-url=https://example.com/
  2. (Optional) In some cases, you might want to fetch packages from multiplerepositories, such as when the public repository contains some specificpackages that you want to install, and you want to install all otherpackages from PyPI:

    1. Configure anArtifact Registry virtual repository.
    2. Add configuration for multiple repositories (including PyPI, if needed) and define the order in whichpip searches the repositories.
    3. Specify the virtual repository's URL in theindex-url parameter.
  3. Determine the URI of your environment's bucket.

  4. Upload the pip.conf file to the/config/pip/folder in your environment's bucket.

  5. Install packages usingone of the available methods.

Install packages from an Artifact Registry repository

You canstore packages in an Artifact Registry repositoryin your project, and configure your environment to install from it.

Note: If you use VPC Service Controls, then you cancreate Artifact Registry PyPI repository in VPC mode. Inthis case, follow theprocedure for private repositories when assigningpermissions.

Configure roles and permissions:

  1. Make sure that the Cloud Build service account haspermissions to read from your Artifact Registry repository.

  2. If your environment has restricted access to other services in yourproject, for example, if you use VPC Service Controls:

    1. Assign permissions to access your Artifact Registry repository tothe environment's service account instead of theCloud Build service account.

    2. Make sure that connectivity to the Artifact Registry repository isconfigured in your project.

To install custom PyPI packages from an Artifact Registry repository:

  1. Create apip.conf file and include the following information in the file, if applicable:

    • URL of the Artifact Registry repository (in theindex-url parameter)
    • Access credentials for the repository
    • Non-defaultpip installation options

    For an Artifact Registry repository, append/simple/ to the repositoryURL:

    [global]index-url = https://us-central1-python.pkg.dev/example-project/example-repository/simple/
  2. (Optional) In some cases, you might want to fetch packages from multiplerepositories, such as when your Artifact Registry repository containssome specific packages that you want to install, and you want to installall other packages from PyPI:

    1. Configure anArtifact Registry virtual repository.
    2. Add configuration for multiple repositories (including PyPI, if needed) and define the order in whichpip searches the repositories.
    3. Specify the virtual repository's URL in theindex-url parameter.
  3. Upload this pip.conf file to the/config/pip/folder in your environment's bucket. For example:gs://us-central1-example-bucket/config/pip/pip.conf.

  4. Install packages usingone of the available methods.

Install packages from a private repository

You can host a private repository in your project's network and configure yourenvironment to install Python packages from it.

Configure roles and permissions:

  1. If you install custom PyPI packages from a repository in your project'snetwork, and this repository does not have a public IP address:

    1. Assign permissions to access this repository to the environment'sservice account.

    2. Make sure that connectivity to this repository is configured in yourproject.

To install packages from a private repository hosted in your project's network:

  1. Create apip.conf file and include the following information in the file, if applicable:

    • IP address of the repository in your project's network
    • Access credentials for the repository
    • Non-defaultpip installation options

    Example:

    [global]index-url=https://192.0.2.10/
  2. (Optional) In some cases, you might want to fetch packages from multiplerepositories, such as when the private repository contains some specificpackages that you want to install, and you want to install all otherpackages from PyPI:

    1. Configure anArtifact Registry virtual repository.
    2. Add configuration for multiple repositories (including PyPI, if needed) and define the order in whichpip searches the repositories.
    3. Specify the virtual repository's URL in theindex-url parameter.
  3. (Optional)Youcan use a custom certificate when installing packages from your privaterepository. To do so:

    1. Upload the certificate file to the/config/pip/ folder in your environment's bucket.

      Caution:Don't upload private keys. Upload only a publiccertificate bundle in the PEM format. Verify that the certificatebundle contains the correct root CA.
    2. In pip.conf, specify the name of the certificate file in thecertparameter. Don't change the/etc/pip/ folder.

      Example:

      [global]cert =/etc/pip/example-certificate.pem
  4. Upload the pip.conf file to the/config/pip/folder in your environment's bucket. For example:gs://us-central1-example-bucket/config/pip/pip.conf.

  5. Install packages usingone of the available methods.

Install a local Python library

To install an in-house or local Python library:

  1. Place the dependencies within a subdirectory in thedags/ folder in your environment's bucket. To import a module from asubdirectory, each subdirectory in the module's path must containan__init__.py package marker file.

    In the following example, the dependency iscoin_module.py:

    dags/  use_local_deps.py  # A DAG file.  dependencies/    __init__.py    coin_module.py
  2. Import the dependency from the DAG definition file.

    For example:

fromdependenciesimportcoin_module
Note: An import error occurs if an__init__.py file is missing. Directory andfile names must be valid Python identifiers.

Use packages that depend on shared object libraries

Certain PyPI packages depend on system-level libraries.While Cloud Composer does not support system libraries, you can usethe following options:

  • Use theKubernetesPodOperator. Set the Operator image to a custombuild image. If you experience packages that fail during installation dueto an unmet system dependency, use this option.

  • Upload the shared object libraries to your environment's bucket. If your PyPIpackages have installed successfully but fail at runtime, use this option.

    1. Manually find the shared object libraries for the PyPI dependency(an .so file).
    2. Upload the shared object libraries to the/plugins folder in yourenvironment's bucket.
    3. Set the following environment variable:LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/airflow/gcs/plugins
Note: Files in theplugins/ foldersare synced to the local file system. Uploading large .sofiles can affect the performance of your environment and the Airflow web server.

Install packages in private IP environments

Depending on how you configure your project, your environment might not haveaccess to the public internet.

By default, your Cloud Composer 3 environmentcan access the internet wheninstalling PyPI packages. This works in all environment's networkingtypes (Private and Public IP). For example, you can install packages fromPython Package Index in a Private IP environment.

For more information, seeConfigure access to the internet when installing PyPI packages.

Private IP environment with public internet access

If your private IP environment can access public internet, then you caninstall packages using options for public IP environments:

Private IP environment without internet access

If your private IP environment does not have access to public internet, thenyou can install packages using one of the following ways:

  • Use aprivate PyPI repository hosted in your project'snetwork.
  • Use aproxy server VM in your project's network to connectto a PyPI repository on the public internet. Specify the proxy address inthe/config/pip/pip.conf file in your environment's bucket.
  • Use an Artifact Registry repository as the only sourceof packages. To do so, redefine theindex-url parameter, as described.
  • If your security policy permits access to external IP addresses from yourVPC network, you can enable the installation of packages from repositorieson the public internet by configuringCloud NAT.
  • Put Python dependencies into the/dags folder in your environment's buckettoinstall them as local libraries. This might not be agood option if the dependency tree is large.

Install to a private IP environment under resource location restrictions

Keeping your project in line with Resource Location Restrictionrequirements prohibits the use of some tools. In particular, Cloud Buildcannot be used for package installation, preventing direct access torepositories on the public internet.

To install Python dependencies in such an environment, follow the guidance fora private IP environmentswithout internet access.

Install a Python dependency to a private IP environment in a VPC Service Controls perimeter

Protecting your project with aVPC Service Controls perimeterresults in further security restrictions. In particular, Cloud Buildcannot be used for package installation, preventing direct access torepositories on the public internet.

To install Python dependencies for a private IP environment inside a perimeter,follow the guidance for private IP environmentswithout internet access.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.