skip to Main Content

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.

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search