pythonfirebasegoogle-cloud-functionsfirebase-tools

How to import a library in Python for Firebase functions?


Hello StackOverflow community. I am trying to deploy Firebase functions written in Python from a React-Native project.

My code snippet looks like this:

from firebase_functions import firestore_fn, https_fn
import fitz
import re
import requests
import io
from datetime import datetime
# The Firebase Admin SDK to access Cloud Firestore.
from firebase_admin import initialize_app, firestore, credentials, storage 
import google.cloud.firestore
from google.cloud import storage as Storage
from google.oauth2 import service_account
from uuid import uuid4

cred = credentials.Certificate("link_to_my_certificate.json")
app = initialize_app(cred,{'storageBucket':'my_project_name.appspot.com'})

@https_fn.on_request()
def printHello(req: https_fn.Request) -> https_fn.Response:
    https_fn.Response("Hello from Firebase functions", status=200)

I tested them:

tsc --watch

firebase emulators:start --only functions

It works perfectly fine, and after that, I'm trying to deploy them and I get the output like this:

knswrw@MacBook functions % firebase deploy --only functions

=== Deploying to 'my_project_name'...

i  deploying functions
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
i  functions: Loading and analyzing source code for codebase default to determine what to deploy
 * Serving Flask app 'serving'
 * Debug mode: off

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:8082

Press CTRL+C to quit

127.0.0.1 - - [14/Sep/2023 12:34:11] "GET /__/functions.yaml HTTP/1.1" 200 -

127.0.0.1 - - [14/Sep/2023 12:34:11] "GET /__/quitquitquit HTTP/1.1" 200 -

/bin/sh: line 1: 75256 Terminated: 15          python3.11 "/Users/knswrw/Desktop/Project/firebase/functions/venv/lib/python3.11/site-packages/firebase_functions/private/serving.py"

i  functions: preparing functions directory for uploading...
i  functions: packaged /Users/knswrw/Desktop/Project/firebase/functions (14.13 KB) for uploading
i  functions: ensuring required API run.googleapis.com is enabled...
i  functions: ensuring required API eventarc.googleapis.com is enabled...
i  functions: ensuring required API pubsub.googleapis.com is enabled...
i  functions: ensuring required API storage.googleapis.com is enabled...
✔  functions: required API pubsub.googleapis.com is enabled
✔  functions: required API run.googleapis.com is enabled
✔  functions: required API eventarc.googleapis.com is enabled
✔  functions: required API storage.googleapis.com is enabled
i  functions: generating the service identity for pubsub.googleapis.com...
i  functions: generating the service identity for eventarc.googleapis.com...
✔  functions: functions folder uploaded successfully
i  functions: creating Python 3.11 (2nd Gen) function printHello(us-central1)...
Could not create or update Cloud Run service printhello, Container Healthcheck failed. Revision 'printhello-00001-wev' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.

Logs URL: https://console.cloud.google.com/logs/viewer?project=my_project_name&resource=cloud_run_revision/service_name/printhello/revision_name/printhello-00001-wev&advancedFilter=resource.type%3D%22cloud_run_revision%22%0Aresource.labels.service_name%3D%22printhello%22%0Aresource.labels.revision_name%3D%22printhello-00001-wev%22 
For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start

Functions deploy had errors with the following functions:
    printHello(us-central1)
i  functions: cleaning up build files...

Error: There was an error deploying functions

I tried to search about this topic on the internet, but I didn't find an answer to my question.

I would welcome any answer, any explanation and any advice.

Thank you.

EDITED: There's a trouble in the fitz library from Pymupdf (I'm using it in the import above).

The question is next, now modified:

How to import a library in Python for Firebase functions? How to install it in the right way? And how to deploy the functions?


Solution

  • I found the solution to my problem when I checked the firebase-debug.log. It appears that there was an issue with the "fitz" library from Pymupdf, which I had imported in my code.

    There's a trouble in fitz library from Pymupdf (I'm using it in the import above)

    I had initially added the library to my Python Firebase functions using the following command:

    pip3 install -t _directoryName _moduleName
    

    However, it turns out that this was not the correct approach.

    I encountered deployment issues and received an error message like this:

    For more troubleshooting guidance, see https://cloud.google.com/run/docs/troubleshooting#container-failed-to-start Could not create or update Cloud Run service FunctionName, Container Healthcheck failed. Revision 'FunctionName-00001-wus' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable. Logs for this revision might contain more information.

    Upon examining the logs, I noticed that the issue was related to the "import fitz" statement in my code.

    Import fitz

    To resolve this, I followed these steps:

    Delete the "venv" directory.

    Add the libraries you want to use to the "requirements.txt" file.

    Recreate the virtual environment with Python 3.11 using the following commands:

    python3.11 -m venv venv
    source venv/bin/activate
    pip3 install --upgrade pip
    python3.11 -m pip install -r requirements.txt
     
    

    Write your Firebase functions.

    Finally, follow these steps for deployment:

    tsc —watch
    firebase emulators:start --only functions
    firebase deploy
    

    And that's it! Your deployment should now work smoothly.