dsx
dsx

Reputation: 301

Installing Python libraries and modules in a Google Cloud Function

Situation

I have an existing Python app in Google Colab that calls the Twitter API and sends the response to Cloud Storage.

I'm trying to automate the Twitter API call in GCP, and am wondering how I install the requests library for the API call, and install os for authentication.

I tried doing the following library installs in a Cloud Function:

import requests
import os

Result

That produced a resulting error message:

Deployment failure: Function failed on loading user code.

Do I need to install those libraries in a Cloud Function? I'm trying to understand this within the context of my Colab python app, but am not clear if the library installs are necessary.

Thank you for any input.

Upvotes: 1

Views: 2169

Answers (2)

hansrajswapnil
hansrajswapnil

Reputation: 639

creating a new python environment for your project might help and would be a good start for any project

it is easy to create.

## for unix-based systems
## create a python environment
python3 -m venv venv

## activate your environment
## in linux-based systems
. ./venv/bin/activate

if you are using google colab, add "!" before these commands, they should work fine.

Upvotes: -2

Vishal Bulbule
Vishal Bulbule

Reputation: 309

when you create your cloud function source code , there are two files.

  1. main.py
  2. requirements.txt

Add packages in requirements.txt as below

#Function dependencies, for example:

requests==2.20.0

Upvotes: 2

Related Questions